PHP Date & Time

Date and Time are integral parts of our life. PHP Date and time built-in function provide the capability to convert the date and time in human-readable format.

The PHP Date function

The PHP Date() function is a built-in function that converts a timestamp into a more readable format.

The computer stores date and time in Unix style which represents data and time as number of seconds elapsed since midnight GMT on January 1, 1970. This moment is called Unix epoch, and the number seconds elapsed from that moment is called timestamp.

PHP Date Syntax



  • “date” is the function that returns the current date and time of the system.
  •  “format” is the general format in which we want to see the output.
    • “Y-m-d” represents PHP date format YYYY-MM-DD
    • “Y” to display the current year
    • “[timestamp]” is optional. If no timestamp has been provided, PHP will take the current date-time on the server.

Let’s take an example to get the date in format YYYY-MM-DD.


echo date("Y-m-d");



The following table contains the list of codes that format string can contain in PHP date() function.

Format Description Example
d Day of month, a number with leading zeroes 25
D Day of week (three letters) Wed
F Month name April
h Hour (12-hour format – leading zeroes) 10
H Hour (24-hour format – leading zeroes) 21
g Hour (12-hour format – no leading zeroes) 12
G Hour (24-hour format – no leading zeroes) 23
a am’ or ‘pm’ lowercase am
A AM’ or ‘PM’ uppercase PM
i Minutes ( 0 – 59 ) 28
j Day of the month (no leading zeroes 19
l Day of the week Friday
L Leap year (‘1’ for yes, ‘0’ for no) 1
m Month of year (number – leading zeroes) 2
M Month of year (three letters) Apr
r The RFC 2822 formatted date Thu, 28 Apr 2020 16:01:07 +0200
n Month of year (number – no leading zeroes) 2
s Seconds of hour 20
U Time stamp 1588074242
y Year (two digits) 20
Y Year (four digits) 2020
z Day of year (0 – 365) 207
Z Offset in seconds from GMT 6

The PHP time() Function

The time() function returns 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).


echo time();

The above example  gives the output as below.


We can convert this timestamp to human readable date and time using the previously discussed date() function.


$timestamp = 1588074242;
echo(date("F d, Y h:i:s", $timestamp));


April 28, 2020 01:44:02

PHP mktime() Function

The mktime() function used to return the timestamp in a Unix format.


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


  • “mktime(…)” stands for make php timestamp function
  • “hour” is optional, it is the number of hour
  • “minute” is optional, it is the number of minutes
  • “second” is optional, it is the number of seconds
  • “month” is optional, it is the number of the month
  • “day” is optional, it is the number of the day
  • “year” is optional, it is the number of the year


Let’s create a timestamp for date 23/08/2022 5:10:32 using mktime() function.

// Create the timestamp for a particular date
echo mktime(17, 10, 32, 8, 23, 2022);




  • “17,10,32” is the hour, minute, and seconds respectively.
  • “08” is the month of the year
  • “23” is the day of the month
  • “2022” is the year


Please get connected & share!