PHP time and Other Related Functions

PHP

In PHP, the time() function is used to display the current Unix timestamp in milliseconds. Of course, a whole lot more can be achieved with this and other related functions. You can use the time function to find out the unix timestamp. This can later be formatted using the date() function.

Example:

<?php
echo time();
?>
Output:

1414305327

If you need to display the Unix time in milliseconds, use the microseconds() function instead.

Example:

<?php
echo microtime();
?>
Output:

0.91860500 1414305698
Note: microtime() displays the microseconds in decimals along with the regular Unix Epoch time.

mktime

This function is used to find the Unix time for a particular day/hour/minute/second. You use the function as mktime(hour, minute, second, month, day, year).

Example:

<?php
$date = mktime(10, 20, 30, 10, 24, 2014);
echo $date;
echo "<br />";
echo date('M d, y - g:i:s',1414164030);
?>
Output:
1414164030
Oct 24, 14 - 10:20:30
Note: The example shows the use of mktime to find out the epoch date for a particular day created by mktime. The epoch day is calculated as 1414164030. The same epoch date has been manually entered to be formatted using date() function.

strtotime

A pretty useful function for parsing human readable textual dates into Unix timestamp. This one simplifies finding the epoch date for certain days.

For example:

<?php
echo strtotime("next Monday");
echo strtotime("-1 day");
echo strtotime("+1 day");
echo strtotime("10/26/2014"); //mm/dd/yyyy when using slash (/)
echo strtotime("26-10-2014"); //dd-mm-yyyy when using dash (-)
?>
Output:
1414386000
1414220912
1414393712
1414299600
1414299600
Note: You can use the date function to format these timestamps after this.