PHP Date and Time


PHP provides various functions to get the date and time, and also to manipulate it. Some of them are as follows :

PHP date() Function :

The PHP date() function is used to format a date or time. The syntax is as follows :
 date(format, timestamp);
Where format specifies the format of the timestamp. Now lets see an example :
<?php

  echo "Day : " . date("d") . "<br/>";
  echo "Month : " . date("m") . "<br/>";
  echo "Year : " . date("Y") . "<br/>";

  echo "Today's Date : " . date("Y-m-d") . "<br/>";
  echo "Today's Date : " . date("Y/m/d") . "<br/>";
  echo "Today's Date : " . date("Y.m.d") . "<br/>";
  echo "Today's Date : " . date("Y:m:d") . "<br/>";

?>
Output :

Day : 05
Month : 07
Year : 2018
Today's Date : 2018-07-05
Today's Date : 2018/07/05
Today's Date : 2018.07.05
Today's Date : 2018:07:05

At the above example some characters are used to represent the dates :

d : for day of the month.
m : for month.
Y : for year.

With the above characters, some other characters (- / . :) are used for additional formatting between characters.

With date function we can also get the time. For example :
<?php
  
  echo "Seconds : " . date("s") . "<br/>";
  echo "hours : " . date("h") . "<br/>";
  echo "Minuts : " . date("i") . "<br/>";
  echo "AM/PM : " . date("a") . "<br/>";

  echo "Current Date : " . date("h-i-s a") . "<br/>";
  echo "Today Date : " . date("h/i/s a") . "<br/>";
  echo "Today's Date : " . date("h.i.s a") . "<br/>";
  echo "Today's Date : " . date("h:i:sa") . "<br/>";

?>
Output :

Seconds : 54
hours : 11
Minuts : 37
AM/PM : am
Current Date : 11-37-54 am
Today Date : 11/37/54 am
Today's Date : 11.37.54 am
Today's Date : 11:37:54 am

As we can see in the above example, the characters were used are :

h : for hours.
i : for minutes.
s : for seconds.
a : for Ante meridiem (am) and Post meridiem (pm).

mktime() Function :

The mktime() function is used to returns the unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. The syntax of mktime() function is as follows :
 mktime(hour, minute, second, month, day, year);
<?php

  $d = mktime(4, 45, 10, 6, 14, 2018);
  echo "Date is : " . date("Y-m-d h:i:s a", $d);

?>
Output :

Date is : 2018-06-14 04:45:10 am

There are also a lot of date and time function available for use. Some of them are as follows :

Function Name Description
checkdate() Validates a Gregorian date.
date_add() Adds an amount of days, months, years, hours, minutes and seconds to a date.
date_create_from_format() Returns a new DateTime object formatted according to the specified format.
date_create() Returns new DateTime object.
date_date_set() Sets a new date.
date_default_timezone_get() Returns the default timezone used by all date/time functions in a script.
date_default_timezone_set() Sets the default timezone used by all date/time functions in a script.
date_diff() Returns the difference between two dates.
date_format() Returns a date formatted according to a specified format.
date_get_last_errors() Returns the warnings and errors found while parsing a date/time string.
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string.
date_interval_format() Formats the interval.
date_isodate_set() Set a date according to the ISO 8601 standard.
date_modify() Modifies the timestamp.
date_offset_get() Returns the timezone offset.
date_parse_from_format() Returns an associative array with detailed info about given date formatted according to the specified format.
date_parse() Returns associative array with detailed info about a specified date.
date_sub() Subtracts an amount of days, months, years, hours, minutes and seconds from a date.
date_sun_info() Returns an array with information about sunset/sunrise and twilight begin/end for a specified day and location.
date_sunrise() Returns time of sunrise for a given day and location.
date_sunset() Returns time of sunset for a given day and location.
date_time_set() Sets the time.
date_timestamp_get() Returns the Unix timestamp representing the date.
date_timestamp_set() Sets the date and time based on an Unix timestamp.
date_timezone_get() Return time zone relative to given DateTime.
date_timezone_set() Sets the time zone for the DateTime object.
date() Formats a local date and time.
getdate() Returns date/time information of the timestamp or the current local date/time.
gettimeofday() Returns the current time.
gmdate() Formats a GMT/UTC date and time.
gmmktime() Get Unix timestamp for a GMT date.
gmstrftime() Formats a GMT/UTC date and time according to locale settings.
idate() Formats a local time/date as integer.
localtime() Returns the local time.
microtime() Return the current Unix timestamp with microseconds.
mktime() Returns the Unix timestamp for a date.
strftime() Formats a local time/date according to locale settings.
strptime() Parses a time/date generated with strftime().
strtotime() Parses an English textual datetime into a Unix timestamp.
time() Returns the current time as a Unix timestamp.
timezone_abbreviations_list() Returns associative array containing dst, offset and the timezone name.
timezone_identifiers_list() Returns an indexed array containing all defined timezone identifiers
timezone_location_get() Returns the location information for a specified timezone.
timezone_name_from_abbr() Returns the timezone name from abbreviation.
timezone_name_get() Returns the name of the timezone.
timezone_offset_get() Returns the timezone offset from GMT.
timezone_open() Creates new DateTimeZone object.
timezone_transitions_get() Returns all transitions for the timezone.
timezone_version_get() Returns the current version of the timezonedb.

Next Topic :