php

Read Current Date and Time in PHP

The current date and time are required to retrieve for various purposes in programming, such as adding data entry date and time in the database table and checking whether the user’s session period is expired or not. The date and time values are generated based on the timestamp value calculated from 1st January 1970. There are two ways to read the current date and time in PHP. One way is to use the date() function, and another way is to use the DateTime class. The ways of reading the current date and time in different formats have been shown in this tutorial.

Read Current Date and Time Using the date() Function

The date() function is one of the simple ways to read the current date and time value in PHP. The syntax of this function is provided below:

Syntax 

date($format);

This function takes the formatting string in the argument used to print the output. The most commonly used format parameters have been described here.

Uses of date() Function

Different uses of the date() function are shown below:

Example 1: Print the Current Date and Time

Create a PHP file with the following script to print the current date and time in different formats. The first date() function will print the current date with a short month name and a 4-digits year value. The second date() function will print the current date with a hyphen(-) and time with a colon(:). The third date() function will print the current date with the full weekday name and full month name. The fourth idate() function will print the current date with a short weekday and month name and the current time with AM/PM.

<?php

//Print the date in 'd-M-y' format
echo "Current date is : " . date("d M Y"), "</br>";


//Print the date and time in 'd-m-Y; h:m:s' format
echo "Current date and time is : " . date("d-m-Y; h:i:s"), "</br>";

//Print the date in 'l, F d, Y' format
echo "Current date is : " . date("l, F d, Y"), "</br>";

//Print the date and time in 'D, d M Y h:m:i A' format
echo "Current date is : " . date("D, d M Y h:i:s A"), "</br>";
?>

The following output will appear after executing the previous script:

Example 2: Print the Current Date and Time Based on the timezone

Create a PHP file with the following script that will print the current date and time after changing the default timezone value. The date_default_timezone_set() function has been used in this script to change the default timezone to “Asia/Dhaka”. Next, the current date and time have been assigned to a date object by using the DateTime class. The format() method of the DateTime class has been used to print the formatted date.

<?php

//Set the timezone to Asia/Dhaka
date_default_timezone_set('Asia/Dhaka');

//Create a date object
$objDate = new DateTime();

//Print the current date and time
echo "<br><h3>Current date and time is ", $objDate->format('D, d M Y h:i:s A'), "</h3>";
?>

The following output will appear after executing the previous script:

Example 3: Print the Current Date and Time by Using the strtotime() Function

Create a PHP file with the following script that prints the current date and time using the strtotime() function after changing the default timezone value to “Asia/Dhaka”. The strtotime() function has been used here to read the timestamp value of the current date and time. This timestamp value has been used in the date() function to print the current date and time.

<?php

//Set the timezone to Asia/Dhaka
date_default_timezone_set('Asia/Dhaka');

//Read the timestamp of the current time
$timestamp = strtotime("now");


//Print the current date and time based on the timestamp value
echo "<br><h3>Current date and time is ", date("d-m-Y H:i:s A", $timestamp), "</h3>";

?>

The following output will appear after executing the previous script:

Example 4: Print the Current Date and Time by Using the time() Function

Create a PHP file with the following script to print the current date and time using the time() function after changing the default timezone value to “Asia/Dhaka”. The time() function has been used here to read the timestamp value of the current date and time. As in the previous example, this timestamp value has been used in the date() function to print the current date and time.

<?php

//Set the timezone to Asia/Dhaka
date_default_timezone_set('Asia/Dhaka');

//Read the timestamp of the current time
$timestamp = time();

//Print the current date and time based on the timestamp value
echo "<br><h3>Current date and time is ", date("D, d M Y h:i:s A", $timestamp), "</h3>";

?>

The following output will appear after executing the previous script:

Read Current Date and Time Using DateTime Class

Using the DateTime class is another way to read PHP’s current date and time value. The use of this class to read the current date and time has been shown in the following example:

Example 5: Print the Current Date and Time by Using the DateTime Class

Create a PHP file with the following script to print the current date and time by using the DateTime class. The current date and time have been stored into a date object by using the DateTime class. Next, the format() method was used to print the formatted output. The first format() method will print the current date only. The second format() method will print the current time only. The third format() method will print the current date and time values.

<?php

//Create a date object
$objDate = new DateTime();


//Print the current date
echo "Today is ", $objDate->format('D, d M Y'), "<br/>";

//Print the current time
echo "Current time is ", $objDate->format('h:i:s'), "<br/>";

//Print the current date and time
echo "Current date and time is ", $objDate->format('d-m-Y H:i:s A'), "<br/>";

?>

The following output will appear after executing the previous script:

Conclusion

Different ways of retrieving and printing the current date and time have been shown in this tutorial by using different built-in functions of PHP. The new PHP users can use the previously mentioned functions in their script to read the current date and time.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.