PHP Date And Time. Everything You Need To Know

Posted by TotalDC

Last time we talked about GET and POST methods. Now let’s continue our PHP series by talking about date and time in PHP. Here you will learn how to use PHP date and time functions.

The PHP Date() Function

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

Originally computer stores date and time in UNIX Timestamp which measures time as a seconds since the midnight Greenwich Mean Time on January 1, 1970 (1970 00:00:00 GMT) or in other words the beginning of the Unix epoch.

As you can see this is an impractical format for us 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 looks like this:

date(format, timestamp)

The format parameter in the date() function is required which specifies the format of returned date and time. But then timestamp is an optional parameter. If you don’t include it – current date and time will be used. Let’s look at the example of the function displaying today’s date:

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

Formatting The Date And Time With PHP

The format parameter of the date() function can contain multiple characters allowing you to generate string containing various components of the date and time, for example day of the week, AM, PM and more. Here are commonly used date formatting characters:

  • 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 (2019 or 2021)

The parts of the date can be separated bu inserting characters like hyphens (-), dots (.), slashes(/) or spaces to add visual formatting. Here are the example:

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

Here are list of characters you can use to format 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)

Here are the example of different time formatting:

<?php
print date("h:i:s") . "<br>";
print date("F d, Y h:i:s A") . "<br>";
print date("h:i a");
?>

Time() Function In PHP

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

<?php

$timestamp = time();
print($timestamp);
?>

Result would be:

1639860765

Now we can convert this timestamp to a readable date by passing it the date() function.

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

Result would be:

Sat 18 2021 20:52:45

Mktime() Function in PHP

The mktime() function works opposite to date() function and is used to create timestamp based on a specific date and time. And if no date and time is given, you will get timestamp for current date and time.

The syntax of the mktime() function looks like this:

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

The following example displays the timestamp corresponding to 3:20:12 pm on December 18, 2021:

<?php

print mktime(15, 20, 12, 12, 18, 2014);
?>

The mktime() function can be used to find the weekday name of the particular date. To do this you can simply use the ‘I’ (lowercase L) character whit your timestamp.

<?php

print date('l', mktime(0, 0, 0, 4, 1, 2021));
?>

Result of that is:

Thursday

This function can also be used to find date after specific time period. As in the following example date after 5 month from now:

<?php

$futureDate = mktime(0, 0, 0, date("m")+5, date("d"), date("Y"));
print date("d/m/Y", $futureDate);
?>

Result:

18/05/2022

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: