Best way to get the current date and time in PHP

Let’s say you are working on a project that requires you to get the current date and/or time in PHP. In this tutorial, you will learn about different methods that can be used to get the current date or time with or without timezones.

On This Page — Get the Current Date Using the date() Function, Get the Timestamp Using $_SERVER['REQUEST_TIME'], Get the Timestamp Using time() Function and Quick Summary.

Get the Current Date Using the date() Function

The date() function in PHP can be used to return the local date and time in a format of your choice.

You can use a lot of characters to specify the format of the outputted date string. If you are only interested in the current date and time, knowing about the following date formatting characters should be sufficient.

  • H — Get the hour in the 24-hour format with leading zeroes (00 to 23).
  • h — Get the hour in the 12-hour format with leading zeroes (01 to 12).
  • G — Get the hour in the 24-hour format without leading zeroes (0 to 23).
  • g — Get the hour in the 12-hour format without leading zeroes (1 to 12).
  • i — Get the minutes with leading zeroes (00 to 59).
  • s — Get the seconds with leading zeroes (00 to 59).
  • m — Get the numeric representation of a month with leading zeroes (01 to 12).
  • M — Get the short textual representation of a month with three letters (Jan to Dec).
  • y — Get two digit representation of a year (like 94 or 17).
  • Y — Get four digit representation of a year (like 1994 or 2017).
  • d — Get the day of the month as 2 digits with leading zeroes (01 to 31).
  • D — Get the short textual representation of a day with three letters (Mon to Sun).

There are many more but these are the ones that you need to know in order to get the current date and time using the date() function.

Here are a few examples that get the current date or time in PHP using the date() function:

PHP

// October 2, 2017, 9:11 PM
$today = date("F j, Y, g:i A");

// 10.02.17
$today = date("m.d.y");

// 09-11-51, 2-10-17, 1131 1151 Monday.
$today = date('h-i-s, j-m-y, it is l.');

// It is the 2nd day of the month.
$today = date('\I\t \i\s \t\h\e jS \d\a\y \o\f \t\h\e \m\o\n\t\h.');

// Date: 2, Month: 10, Year: 2017
$today = date("\D\a\\t\\e: j, \M\o\\n\\t\h: n, \Y\\e\a\\r: Y");

// 21:11:51
$today = date("H:i:s");

// 2017-10-02 21:11:51
$today = date("Y-m-d H:i:s");

In the above example, you can see that I had to escape all the characters in order to display them literally. For instance, it is l. became 1131 1151 Monday. without escaping the characters. This is because i gave the number of minutes with leading zeroes, s gave the number of seconds with leading zeroes and t gave the number of days in the month (31 for October in our case).

With double quotes, we had to use two backslashes for characters like t and n in order to prevent them from rendering tabs and new lines.

You can also get the current date and time for a particular timezone with the help of the date_default_timezone_set() function. Here are a few examples that show the time in different parts of the world.

PHP

$timezone = date_default_timezone_get();

// Output — The current timezone is: Europe/Berlin
echo "The current timezone is: ".$timezone;
// Output — Current date and time in Berlin is: 02 Oct, 17 10:22:43 PM
echo "Current date and time in Berlin is: ".date('d M, y h:i:s A');

date_default_timezone_set('America/Denver');
// Output — Current date and time Denver is: 02 Oct, 17 02:22:43 PM
echo "Current date and time Denver is: ".date('d M, y h:i:s A')."\n";

date_default_timezone_set('Australia/Sydney');
// Output — Current date and time Sydney is: 03 Oct, 17 07:22:43 AM
echo "Current date and time Sydney is: ".date('d M, y h:i:s A')."\n";

date_default_timezone_set('Asia/Kolkata');
// Output — Current date and time Kolkata is: 03 Oct, 17 01:52:43 AM
echo "Current date and time Kolkata is: ".date('d M, y h:i:s A')."\n";

Get the Timestamp Using $_SERVER['REQUEST_TIME']

In PHP, $_SERVER is an array that contains information like headers, paths or script locations. One of the indices of this array is REQUEST_TIME. It returns the timestamp at the start of a request. This index has been available since PHP 5.1.0.

Similarly, you can also use the REQUEST_TIME_FLOAT index. This will also give you the timestamp of the start of a request. However, it will have a microsecond precision and it has been available since PHP 5.4.0.

PHP

$timestamp_a = $_SERVER['REQUEST_TIME'];
$timestamp_b = $_SERVER['REQUEST_TIME_FLOAT'];

// Output — 1506973854
echo $timestamp_a;

// Output — 1506973854.3914
echo $timestamp_b;

Get the Timestamp Using time() Function

Just like $_SERVER['REQUEST_TIME'], you can also get the current UNIX timestamp using the time() function in PHP. It will return the current time measured in number of seconds since Unix epoch (January 1 1970 00:00:00 GMT).

One important difference between time() and $_SERVER['REQUEST_TIME'] is that the latter will return the timestamp at the start of the request. This may or may not be significant depending on the project.

PHP

sleep(1);
$timestamp_a = time();
$timestamp_b = $_SERVER['REQUEST_TIME'];

// Output — 1506974986
echo $timestamp_a."\n";

// Output — 1506974985
echo $timestamp_b."\n";

As you can see, with the addition of sleep() function to delay the execution of the program by 1 second, the timestamp values for $_SERVER['REQUEST_TIME'] and time() changed by one second. This is because time() get the current time stamp value while $_SERVER['REUQEST_TIME'] used the timestamp value it obtained at the start of the request.

One more important thing which I would like is the time timestamp value returned by either $_SERVER['REUQEST_TIME'] or time() is independent of the time zone. It is based on the UTC value.

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. You can get the current date and time using the date() function in PHP. This function accepts different characters to output the date and time in different formats.
  2. If you want the current time as a timestamp, you can use the REQUEST_TIME index of the $_SERVER superglobal array. Timstamp with microsecond precision can be obtained using the REQUEST_TIME_FLOAT index.
  3. The timestamp returned by REQUEST_TIME is obtained at the start of the request. If you want the timestamp value of a later time, you can use the time() function in PHP.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the current date and time in PHP.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top