Php Date and time function

In this tutorial you will learn how to extract or format the date and time in PHP. The Php Date and time are some of the most frequently used operations in PHP. PHP has builtin date and time function. So let get started with the php date and time function

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:


// echo today date
echo date("d/m/Y");

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)

You can find more formate here

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).

// This will return the current timestamp
echo time();

The above example produce the following output.


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

$timestamp = 1566407823;
echo date(“d-m-Y H:i:s”, $timestamp);

The above example produce the following output.

21-08-2019 19:17:03

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 10:30:55 pm on May 02, 2019:

// Create the timestamp for a particular date
echo mktime(10, 30, 55, 2, 05, 2019);

The above example produce the following output.


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 May 05, 2019:

// Get the weekday name of a particular date
echo date('l', mktime(10, 30, 55, 5, 05, 2019));

The above example produce the following output.


If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *