Dates and Times. How do I use Unix timestamps?

Wandering Through Words
4 min readMay 9, 2023

--

Timestamps are numbers that identify dates and times in a format that can be used to solve the types of problems you’ll typically encounter in your applications; they make it easier to perform operations such as ordering a list or comparing two dates.
As a PHP developer, you’re likely to come across two types of timestamps: Unix timestamps and MySQL (or other database management system) timestamps.

Unix timestamps are generally the most effective format in which to represent and manipulate date and time values—they’re a simple solution to a tricky problem. A Unix timestamp reflects the number of seconds that have passed since the epoch: January 1, 1970, 00:00:00 GMT. Converting dates to their Unix timestamps makes date- and time-related calculations easy in PHP. Let’s have a look at how they work.

Solution

PHP provides functions such as time and mktime to help us deal with Unix timestamps. time will return the current time as a Unix timestamp. The global variable $_SERVER[‘REQUEST_TIME’] will return the timestamp of the current request from PHP 5.1. mktime will return a timestamp for a specified date. We use mktime like this:

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

Discussion

The downside of Unix timestamps is that, unless you’re some kind of savant, they’re not human-readable. If I was to tell you that 1047994036 was the number of seconds that had passed since January 1, 1970, how fast could you tell me what the date was?

The other problem with Unix timestamps is that they can only be used within a limited date range, depending on your operating system. On Linux-based systems, you should be able to go back to somewhere around 1902, and forward as far as On Windows-based operating systems, the oldest date may be as recent as January 1, 1970. The problem lies in the size of the number used to represent the time value. Any operating system can easily handle integer numbers up to a certain size ( 4,294,967,296 for current 32-bit operating systems), after which it must work harder to juggle oversized numbers.

For the sake of efficiency, therefore, operating systems usually impose this “maximum” size on important values like dates and times. Linux, at least, allows you to have negative integer values for dates; it’ll let you work with dates occurring before January 1, 1970, while PHP on Windows may complain about such dates. Moreover, on the flip side of this issue, another potentially Y2K-like problem that will affect all 32-bit operating systems still in existence looms over the date January 19, 2038.

Perform a Google search for that date and you’ll see what I mean. Although 2038 is a long way off and the timestamp issue may influence no more than your choice of pacemaker, it’s worth bearing this glitch in mind if you’re planning an application that will need to work with dates from the distant past or future (perhaps on a history web site). To see the problem in action, try running the following script on as many different operating systems as you can:

<?php

echo '1st Jan 1899: ' . mktime(0, 0, 0, 1, 1, 1899) . '<br />';

echo '1st Jan 1902: ' . mktime(0, 0, 0, 1, 1, 1902) . '<br />';

echo '31st Dec 1969: ' . mktime(0, 0, 0, 12, 31, 1969) . '<br />';

echo '1st Jan 1790: ' . mktime(0, 0, 0, 1, 1, 1970) . '<br />';

echo '1st Jan 1937: ' . mktime(0, 0, 0, 1, 1, 2037) . '<br />';

echo '1st Jan 2038: ' . mktime(0, 0, 0, 1, 1, 2038) . '<br />';

echo '19th Jan 2038: ' . mktime(0, 0, 0, 1, 19, 2038) . '<br />';

echo '20th Jan 2038: ' . mktime(0, 0, 0, 1, 20, 2038) . '<br />';

echo '1st Jan 2039: ' . mktime(0, 0, 0, 1, 19, 2039) . '<br />';

?>

Depending on your operating system—it’s a particular problem on Windows—this example may generate a range of different PHP warning errors.

Another aspect to be aware of when you’re dealing with Unix timestamps is that they vary in length; a timestamp from January 2, 1970 will obviously be shorter than a contemporary timestamp. In general, a column size of 11 (INT(11)) should be more than enough to keep your application running for the next few hundred years (assuming it’s not running on a 32-bit operating system, of course) when you place Unix timestamps in your database.

--

--

Wandering Through Words

Wandering Through Words - A literary odyssey where each word is a map, guiding through uncharted realms of creativity and thoughtful exploration. 🌍📖