PHP BASIC
PHP ADVANCED
PHP & MySQL DATABASE
PHP EXAMPLES
PHP REFERENCE

PHP Date and Time

In this tutorial you will learn how to extract or format the date and time in PHP.

The PHP Date() Function

The PHP date() function convert a timestamp to a more readable date and time.

The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ).

Since this is an impractical format for humans to read, PHP converts a timestamp to a format that is readable to humans and dates from your notation into a timestamp the computer understands. The syntax of the PHP date() function can be given with.

date(format, timestamp)

The format parameter in the date() function is required which specifies the format of returned date and time. However the timestamp is an optional parameter, if not included then current date and time will be used. The following statement displays today's date:

<?php
$today = date("d/m/Y");
echo $today;
?>

Note: The PHP date() function return the current date and time according to the built-in clock of the web server on which the script has been executed.


Formatting the Dates and Times with PHP

The format parameter of the date() function is in fact a string that can contain multiple characters allowing you to generate a date string containing various components of the date and time, like day of the week, AM or PM, etc. Here are some the date-related formatting characters that are commonly used in format string:

  • d - Represent day of the month; two digits with leading zeros (01 or 31)
  • D - Represent day of the week in text as an abbreviation (Mon to Sun)
  • m - Represent month in numbers with leading zeros (01 or 12)
  • M - Represent month in text, abbreviated (Jan to Dec)
  • y - Represent year in two digits (08 or 14)
  • Y - Represent year in four digits (2008 or 2014)

The parts of the date can be separated by inserting other characters, like hyphens (-), dots (.), slashes (/), or spaces to add additional visual formatting.

<?php
echo date("d/m/Y") . "<br>";
echo date("d-m-Y") . "<br>";
echo date("d.m.Y");
?>

Tip: You can use the PHP date() function to automatically update the copyright duration on your website, like: Copyright &copy; 2010-<?php echo date("Y")?>.

Similarly you can use the following characters to format the time string:

  • h - Represent hour in 12-hour format with leading zeros (01 to 12)
  • H - Represent hour in in 24-hour format with leading zeros (00 to 23)
  • i - Represent minutes with leading zeros (00 to 59)
  • s - Represent seconds with leading zeros (00 to 59)
  • a - Represent lowercase ante meridiem and post meridiem (am or pm)
  • A - Represent uppercase Ante meridiem and Post meridiem (AM or PM)

The PHP code in the following example displays the date 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");
?>

Please check out the PHP date() function reference for a complete list of all the characters that can be used inside the format parameter.


The PHP time() Function

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

<?php
// Executed at March 05, 2014 07:19:18
$timestamp = time();
echo($timestamp);
?>

The above example produce the following output.

1394003958

We can convert this timestamp to a human readable date through passing it to the previously introduce date() function.

<?php
$timestamp = 1394003958;
echo(date("F d, Y h:i:s", $timestamp));
?>

The above example produce the following output.

March 05, 2014 07:19:18

The PHP mktime() Function

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

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

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

The following example displays the timestamp corresponding to 3:20:12 pm on May 10, 2014:

<?php
// Create the timestamp for a particular date
echo mktime(15, 20, 12, 5, 10, 2014);
?>

The above example produce the following output.

1399735212

Note: You can leave out as many arguments as you like, and the value corresponding to the current time will be used instead. If you omit all the arguments, the mktime() function will return the UNIX timestamp corresponding to the current date and time, just like time().

The mktime() function can be used to find the weekday name corresponding to a particular date. To do this, simply use the 'l' (lowercase 'L') character with your timestamp, as in the following example, which displays the day that falls on April 1, 2014:

<?php
// Get the weekday name of a particular date
echo date('l', mktime(0, 0, 0, 4, 1, 2014));
?>

The above example produce the following output.

Tuesday

The mktime() function can also be used to find a particular date in future after a specific time period. As in the following example, which displays the date which falls on after 30 month from the current date?

<?php
// Executed at March 05, 2014
$futureDate = mktime(0, 0, 0, date("m")+30, date("d"), date("Y"));
echo date("d/m/Y", $futureDate);
?>

The above example produce the following output.

05/09/2016

Complete PHP Date Reference

Please check out the PHP Date/Time Functions reference section for a complete list of all the useful date and time functions available in PHP.

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties