time()

PHP’s time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer.

The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970. This moment is known as the UNIX epoch, and the number of seconds that have elapsed since then is referred to as a timestamp.

<?php

   print time();

?>

This will produce the following result −

1480930103

Converting a Timestamp with getdate()

The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().

Following table lists the elements contained in the array returned by getdate().

Sr.NoKey & DescriptionExample
1secondsSeconds past the minutes (0-59)20
2minutesMinutes past the hour (0 – 59)29
3hoursHours of the day (0 – 23)22
4mdayDay of the month (1 – 31)11
5wdayDay of the week (0 – 6)4

Example

<?php

   $date_array = getdate();

   $formated_date  = “Today’s date: “;

   $formated_date .= $date_array[‘mday’] . “/”;

   $formated_date .= $date_array[‘mon’] . “/”;

   $formated_date .= $date_array[‘year’];

   print $formated_date;

?>

This will produce following result −

Today’s date: 5/12/2016

Converting a Time Stamp with date()

date()

The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument that you must pass to it.

date(format,timestamp)

The date() optionally accepts a time stamp if omitted then current date and time will be used. Any other data you include in the format string passed to date() will be included in the return value.

Following table lists the codes that a format string can contain −

Sr.NoFormat & DescriptionExample
1a’am’ or ‘pm’ lowercasepm
2A’AM’ or ‘PM’ uppercasePM
3dDay of month, a number with leading zeroes20
4DDay of week (three letters)Thu
5FMonth nameJanuary
6hHour (12-hour format – leading zeros)12
7HHour (24-hour format – leading zeros)22
8gHour (12-hour format – no leading zeroes)12
9GHour (24-hour format – no leading zeroes)22
10iMinutes ( 0 – 59 )23

Example

<?php

   print date(“m/d/y G.i:s<br>”, time());

   echo “<br>”;

   print “Today is “;

   print date(“j of F Y, \a\\t g.i a”, time());

?>

This will produce following result −

12/05/16 9:29:47

Today is 5 2016f December 2016 at 9:29 am