PHP Tutorials: Date and Time

One of the most used functions in PHP is date() and time(). The date() function converts a timestamp into a logical date format which we can understand. The timestamp is a digital record of the time of occurrence of a particular event. Do afraid of this definition. It is just an unsigned integer number.

Our PC stores the time in UNIX format, it measures the time as a number of seconds since the starting of the UNIX epoch i.e. 1st January of 1970 Midnight.

The date() function

No doubt, we can read the UNIX timestamp. But imagine what if we have to divide the number every time to get the current time. Huh! a lot of work. But PHP is too smart. The PHP date() function to that for us.

date( format, timestamp );

In the date() function there are 2 parameters.

  1. format, it specifies the output format of the date. There are specific formatting characters for the format parameters. Formats are explained below.
  2. Another parameter is the timestamp. It’s an optional parameter and can be left null or blank. If left blank the date() will consider it to be the current date and time.

Date-related formatting characters:

  • d – Represent day of the month; two digits with leading zeros (01 or 31)
  • D – Represent day of the week in the 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)

Time-related formatting characters:

  • h – Represent hour in 12-hour format with leading zeros (01 to 12)
  • H – Represent hour 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)
Hyphen (-), Dots (.) Slashes (/), Colons (:) etc can be added to seprated the formats.
echo date("d/m/Y") . "<br>"; // 10/11/2019
echo date("d-m-Y") . "<br>"; // 10-11-2019
echo date("d.m.Y"); . "<br>"; // 10.11.2019

echo date("D-M-Y H:i:s") . "<br>"; // 10-Nov-2019 22:30:23
echo date("D-M-Y h:i a") . "<br>"; // 10-Nov-2019 10:30 pm

echo date("h:i a") . "<br>"; // 10:30 pm
echo date("H:i") . "<br>"; // 22:30 pm

The time() function

The time() function is very similar to the date function. The difference is only the value it returns which is the current time instead of the current date. It also calculates the time since 1st January of the year 1970 00:00:00 GMT.

$current_timestamp = time();
echo $current_timestamp; // Output varies on the date and time you rund the code. 

The $current_timestamp variable can be used as an input for the date function.

$current_timestamp = time();
echo 'Current Timestamp: '.$current_timestamp.'<br>'; // Output varies on the date and time you rund the code. 
echo 'Current Date: '. date( 'd-M-Y h:i:s a', $current_timestamp );

Leave a Reply

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