English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP Date and Time

In this tutorial, you will learn how to extract or format dates and times in PHP.

PHP Date() function

The PHP date() function converts timestamps into more readable dates and times.

Computers store dates and times in a format called UNIX timestamp, which is measured from the Unix epoch (Greenwich Mean Time1970 years1month1midnight, that is}}1970 years1month1seconds from the start of the day at 00:00:00 GMT)

Since this format is not user-friendly for humans, PHP converts the timestamp to a human-readable format and converts the date from your symbol to a timestamp that the computer can understand. The syntax of the function can be used.

date(format, timestamp)

in the date() function offormatis required to specify the format of the returned date and time. However,timestampis an optional parameter. If it is not included, the current date and time will be used. The following statement displays today's date:

<?php
$today = date("d/m/Y);
echo $today;
?>
Test and see‹/›

Note:The PHP date() function returns the current date and time based on the built-in clock of the web server executing the script.

PHP Formatting Date and Time

The format parameter of the date() function is actually a string that can contain multiple characters, allowing you to generate a date string that includes various components of date and time, such as the day of the week, morning or afternoon, etc. Here are some commonly used formatting characters related to dates in format strings:

  • d - Meaning the day of the month; a two-digit number with leading zero (01 ) 31)

  • D - Meaning abbreviation of the day of the week (Monday to Sunday)

  • m - Meaning month, with leading zero number (01 ) 12)

  • M - Meaning month abbreviation (Jan to Dec)

  • y - Two-digit number representing the year (08 ) 14)

  • Y - Four-digit number representing the year (2008 ) 2014)

You can insert other characters (such as hyphen (-), point (.), or slash (/) or space) to separate the parts of the date, to add other visual formatting.

<?php
echo date("d"/m/Y) . "<br>";
echo date("d"-m-Y) . "<br>";
echo date("d.m.Y");
?>
Test and see‹/›

Tip:You can use the PHP date() function to automatically update the copyright statement on your website, for example: Copyright © 2010-<?php echo date("Y")?>.

Similarly, you can use the following characters to format time strings:

  • h - with12The hour format represents hours, with leading zeros (01to12)

  • H - with24The hour format represents hours, with leading zeros (00 to23)

  • i - Meaning minutes, with leading zeros (00 to59)

  • s - Meaning seconds, with leading zeros (00 to59)

  • a - Meaning lowercase ante meridiem and post meridiem (morning or afternoon)

  • A - Meaning uppercase ante meridiem and post meridiem (morning or afternoon)

The following examples show PHP code displaying dates in different formats:

<?php
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>
Test and see‹/›

PHP time() function

The time() function is used to get the current time as a Unix timestamp (the number of seconds since the Unix epoch:1970 years1month1day 00:00:00 GMT).

<?php
//Execution time:2014-03-05 07:19:18
$timestamp = time();
echo($timestamp);
?>
Test and see‹/›

The above example produces the following output.

1394003958

By passing the timestamp to the previously introduced date() function, we can convert it to a human-readable date.

<?php
$timestamp = 1394003958;
echo(date("Y"-m-d H:i:s", $timestamp));
?>
Test and see‹/›

The above example produces the following output.

2014-03-05 07:19:18

PHP mktime() function

The mktime() function is used to create a timestamp based on a specific date and time. If no date and time are provided, it returns the timestamp of the current date and time.

The syntax of the mktime() function can be given as follows:

mktime(hour, minute, second, month, day, year)

The following example shows the date and time corresponding to2014year5month10PM3:20:12Corresponding timestamp:

<?php
//Create a timestamp for a specific date
echo mktime(15, 20, 12, 5, 10, 2014);
?>
Test and see‹/›

The above example produces the following output.

1399735212

Note:You can retain any number of parameters as needed, while using the values corresponding to the current time. If all parameters are omitted, the mktime() function will return the UNIX timestamp corresponding to the current date and time, like time().

mktime() function can be used to find the name of the working day corresponding to a specific date. To do this, simply use the 'l' (lowercase 'L') character in your timestamp, as shown in the following example, which displays2014year4month1On this day:

<?php
//Get the name of the working day for a specific date
echo date('l', mktime(0, 0, 0 4, 1, 2014));
?>
Test and see‹/›

The above example produces the following output.

Tuesday

mktime() function can also be used to find a specific date in the future after a certain period of time. As shown in the following example, it displays the date30 months later date?

<?php
//Execution date is2014year3month5day
$futureDate = mktime(0, 0, 0, date("m"+30, date("d"), date("Y"));
echo date("d"/m/Y", $futureDate);
?>
Test and see‹/›

The above example produces the following output.

05/09/2016

Complete PHP date reference

Please see PHP date/Reference part of time functions, to get a complete list of all available date and time functions in PHP.