PHP Date and Time

PHP makes it easy to work with date and time using built-in PHP date(), time(), mktime(), and strtotime() functions.

When you need a particular date and time format, PHP date() is the most important function. PHP date() supports ‘format’ parameter that provides a wide range of formatting options according to your requirement.

PHP date() function:

Syntax: date(format, timestamp)

formatRequired. This specifies the format of date and time that you want to return.
timestampOptional. Specify UNIX timestamp required date/time. By default, the current date and time are taken.

Example: Display current date

$today = date("d-m-Y");
echo $today;

We did not provide any timestamp in the second parameter. Therefore, it will take the current timestamp (current date and time) and display it according to the format "d-m-Y". For example: 20-06-2020.

Note: You can pass time format characters as well if need to display time in the same function. See more details about the format parameters in next table.

Common date and time format characters:

CharacterDescription (Format)
dDay of the month with 2 digits (leading zeros) format (01 to 31)
DDay name of the week in three letters (Mon to Sun)
mMonth in numbers with 2 digits (01 or 12)
MMonth in three letters (Jan to Dec)
FMonth full textual representation (January to December)
yYear in 2 digits (03 or 21)
YYear in 4 digits (1995 or 2020)
hHour in 12-hour format with 2 digits (01 to 12)
HHour in 24-hour format with 2 digits (01 to 24)
iMinutes in 2 digits (00 to 59)
sSeconds in 2 digits (00 to 59)
gHour in 12-hour format without leading zeros (1 to 12)
GHour in 24-hour format without leading zeros (1 to 24)
aAnte meridiem and Post meridiem in Lowercase (am or pm)
AAnte meridiem and Post meridiem in Uppercase (AM or PM)

You can use a combination of above characters in the ‘format’ parameter to get desired date and time formatting.

Mostly used date formats in PHP:

PHP date function with useful format parametersOutput
<?php echo date("Y-m-d"); // MySQL date format ?>2020-06-20
<?php echo date("F j, Y, g:i A"); ?>June 20, 2020, 3:22 PM
<?php echo date("l, F jS, Y"); ?>Saturday, June 20th, 2020
<?php echo date("g:i:s a"); // Time only ?>3:22:24 pm
<?php echo date("d/m/Y"); ?>20/06/2020
<?php echo date("Y/m/d g:i a"); ?>2020/06/20 3:22 pm

Try and Run code:

PHP time() Function:

The time() function returns the current time as a Unix timestamp, and we can pass it to date() function to change its format according to requirement.

What is PHP Timestamp?

The Timestamp is the number of seconds calculated between the current time and 1st January 1970 00:00:00 (GMT). This is also called Unix Timestamp.

Example: Using PHP time() Function

First, we will display current timestamp and date. Therefore, calculate next month timestamp and display date.

// Get current timestamp
$current = time();

echo "Current Timestamp: " .$current;

echo "<br/><br/>";

echo 'Current Date: '.date("Y-m-d", $current);

echo "<br/><br/>";

// One month calculation using 30 days; 24 hours; 60 mins; 60 secs
$next_month = time() + (30 * 24 * 60 * 60);

echo "Next Month Timestamp: " .$current;

echo "<br/><br/>";

echo 'Next Month: '. date('Y-m-d', $next_month) ."\n";

Try and Run Code:

PHP mktime() Function:

The mktime() function returns Unix timestamp for given hour, minute, second, month, day and year.

Syntax: mktime( $hour, $minute, $second, $month, $day, $year, $is_dst)

In the following example, we will first make a timestamp by specifying the month, day, and year into mktime(). Then, use date function to display it with the format: Thursday, June 18th, 2020.

// Using mktime() & date() show required format
echo date("l, F jS, Y", mktime(0, 0, 0, 6, 18, 2020)); 

Try and Run Code:

PHP strtotime() Function:

The strtotime() converts English textual date-time description to a UNIX timestamp which can be displayed in different formats using date() function.

Here is the example of commonly used English textual formats:

$current_timestamp = strtotime("now");
$past_date = strtotime("10 September 2000");
$tomorrow = strtotime("+1 day");
$future_timestamp = strtotime("+1 week 2 days 4 hours 2 seconds");

echo date("d/m/Y", $current_timestamp);

echo "<br/><br/>";

echo date("d/m/Y", $past_date);

echo "<br/><br/>";

echo date("d/m/Y", $tomorrow);

echo "<br/><br/>";

echo date("d/m/Y", $future_timestamp);

echo "<br/><br/>";

Try and Run Code:


  • All date and time functions use the default time zone (Mostly UTC) set in the php.ini file.
  • If you want to display your local time zone, you can set it using date_default_timezone_set('Asia/Kolkata'); function.

These are the wide range of useful PHP functions to work with date and time. You can find more details about the parameters and options in official documentation and reference websites given below:

Important Links:

  1. PHP date() function:
  2. PHP time() Function:
  3. PHP mktime() Function:
  4. PHP strtotime() Function:

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.