PHP | date() Function

PHP date() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date() Function.

The PHP date() function accepts a format string as parameter, then formats the local date/time in the specified format and then returns the result.

Syntax

Following below is the syntax to use this function -

date($format, $timestamp)


Parameter Details

Sr.NoParameter & Description
1

format (Mandatory)

This is a format string specifying the format in which you want the output date string to be.

2

timestamp (Optional)

This is an integer value representing the timestamp of the required date


Return Value

This PHP function returns the current local date and time in the specified format.

PHP Version

This function was first introduced as part of core PHP version 4 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP date() function -

<?php
   $date = date("D M d Y");
   print("Date: ".$date);
?>

Output

When the above code is executed, it will produce the following result -

Date: Thu Nov 19 2020

Example2

You can escape characters in a date format as shown below -

<?php
   $date = date("jS F l \t");
   print("Date: ".$date);
?>

Output

When the above code is executed, it will produce the following result -

Date: 18th May Monday

Example3

The following example below invokes the date() function by passing the timestamp parameter -

<?php
   $ts = 1022555568;
   $date = date("D M d Y", $ts);
   print($date);
?>

Output

When the above code is executed, it will produce the following result -

Tue May 28 2002

Example4

Try the following example -

<?php
   date_default_timezone_set('UTC');
   
   echo date("l");
   echo "
"; echo date('l dS \of F Y h:i:s A'); echo "
"; ?>

Output

When the above code is executed, it will produce the following result -

Monday
Monday 05th of December 2016 10:27:13 AM


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP getdate() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.