Unit 9: Handling Dates and Times

4th semester

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

syntax of the PHP date() function can be given with.


HERE,

  • “date(…)” is the function that returns the current time on the server.
  • “format” is the general format which we want our output to be i.e.;
    • “Y-m-d” for PHP date format YYYY-MM-DD
    • “Y” to display the current year
    • “[timestamp]” is optional. If no timestamp has been provided, PHP will get the use the php current date time on the server.

Let’s look at a basic example that displays the current year.

Output :- 2019

Formatting options available in date() function: The format parameter of the date() function is a string that can contain multiple characters allowing to generate dates in various formats.
Date-related formatting characters that are commonly used in format string:

  • d – Represents day of the month; two digits with leading zeros (01 or 31).
  • D – Represents day of the week in text as an abbreviation (Mon to Sun).
  • m – Represents month in numbers with leading zeros (01 or 12).
  • M – Represents month in text, abbreviated (Jan to Dec).
  • y – Represents year in two digits (08 or 14).
  • Y – Represents 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.

What is a TimeStamp?

A timestamp is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT).
The value returned by the time function depends on the default time zone.
The default time zone is set in the php.ini file.
It can also be set programmatically using date_default_timezone_set function.
The code below displays the current time stamp

Assuming you saved the file timestamp.php in phptuts folder, browse to the URL http://localhost/phptuts/timestamp.php
PHP Date Function

PHP 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).
The following characters can be used to format the time string:

  • h – Represents hour in 12-hour format with leading zeros (01 to 12).
  • H – Represents hour in in 24-hour format with leading zeros (00 to 23).
  • i – Represents minutes with leading zeros (00 to 59).
  • s – Represents seconds with leading zeros (00 to 59).
  • a – Represents lowercase ante meridian and post meridian (am or pm).
  • A – Represents uppercase ante meridian and post meridian (AM or PM).

Below program explains usage of time() function in PHP:

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.
The syntax of the mktime() function can be given with:

mktime(hourminutesecondmonthdayyear)
The following example displays the timestamp corresponding to 3:20:12 pm on May 10, 2014:

The above example produce the following output.

1399735212