Thursday, November 19, 2020

PHP | date() Function

PHP date() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the date() Function in PHP.

PHP date() function accepts a format string as parameter, then formats the local date/time in the specified format and 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 built-in 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 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP getdate() Function.

Do feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.