PHP Formatting Date and Time Functions

In this article we can discuss about PHP Formatting Date and Time Functions.

PHP Formatting Date and Time Functions

The PHP Formatting 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 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.

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

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

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

Formatting Date Example

<?php
echo date("d/m/Y") . "<br>";
echo date("d-m-Y") . "<br>";
echo date("d.m.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)

Formatting Date Example

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

PHP Formatting 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

echo time();

?>

Formatting time() Example

<?php
// Executed at June 16, 2021 07:19:18
$timestamp = time();
echo($timestamp);
?>

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.

Syntax

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

Example

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

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 mktime() function can also be used to find a particular date in the future after a specific time period. As in the following example, which displays the date which falls on after 30 months from the current date?

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

Output

output

Also Check out PHP 7 Date and Time Functions

Related Posts

Leave a Reply

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