PHP and the UNIX Timestamp
       by Sam Kellett (a.k.a Sammo) : 23 August 2005

This is a guide, you will not have a stand-alone effect or file by the end of this guide, but you will - hopefully - have an understanding of the Unix timestamp that allows you a powerful form of managing your PHP applications.

Note

This guide expects the reader to have an understanding on how mySQL databases work and a knowledge of basic PHP

Unix time, or POSIX time, is a system for describing points in time. It is widely used not only on Unix-like operating systems but in many other computing systems, including the Java programming language. It is an encoding of UTC, and is sufficiently similar to a linear representation of the passage of time that it is frequently mistaken for one.

The Unix timestamp is widely used in PHP. It is the amount of seconds between January 1st 1970 00:00:00 (Unix Epoch) and the present time, to the closest second. As you can imagine, after over 35 years it is now quite a big number!

Unix time is one of the things is handled differently on Windows servers compared to *nix servers. On a Linux server the timestamp can be positive or negative, representing before and after the Unix Epoch. Windows servers however produces -1 instead of a negative timestamp, or in PHP 5.1, it produces false.

PHP has many predefined functions that uses the Unix time, the functions that we'll be using are:

  • date()
  • mktime()
  • strtotime()
  • time()

date() is likely the most-used date function in PHP, it can generate the current date or a selected timestamp in a huge amount of probabilities. A table of all the string determiners is available here

mktime() has parameters, one for each setting for time: second, minute, hour, month, day & year. The 7th parameter is for day light savings however if this setting is left alone PHP will find out the DS hour itself. mktime() returns a timestamp for the parameters made.

strtotime() converts a string into a timestamp, if it can't achieve this it'll return -1 or false.

time() returns the current time to the closest second as a timestamp.

Scenarios
You have a form that enters data into your mySQL database including the date, on a different webpage you need to call back just one of your database rows.

Solution: as the Unix timestamp changes every second they are virtually unique so using you can select a certain row with that timestamp, for example:

$sql = "SELECT * FROM data WHERE timestamp = " . strtotime("Sunday 3rd August 15:20:36");
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<pre>";
print_r($row);
echo "</pre>";

This script makes a query to the database table "data" selecting all of the row where the timestamp is the same as the timestamp of "Sunday 3rd August 15:20:36". The script then displays the array of $row which is all of the data retrieved from the query.

This solves the problem because all of the data for just one row is an array as $row ready to be outputted onto that page. The disadvantages of this solution is that the timestamp is a very exact and if the date called was even a second out it would not select the correct data.

You have a deadline for a piece of work and you want to know how many hours you have left.

Solution: you could use the mktime function to enter in the date of the deadline and subtract that timestamp with the current timestamp to find out how many hours are left.

$day = 1;
$month = 11;
$year = 2005;
$deadline = mktime('', '', '', $month, $day, $year);
$now = time();
$age = $deadline - $now;
$hours = floor($age * 60 * 60);
$age = $hours * 24;
echo "Only $age days ($hours hours) left!";

This script uses mktime() to get the Unix timestamp of November 1st 2005 (the new cssreboot deadline) and the time() function to get the current timestamp. As these two are both numbers, of seconds, we can subtract them. The result is the number of seconds difference, so multiplying that by 60 gives you minutes, 60 again - hours and by 24 to get days.

You have a timestamp and you need to display that as a date in different formats.

echo date("l dS \of F Y",$timestamp);
// Produces something like: Monday 15th of August 2005
 
echo date("r",$timestamp);
// the RSS date format (RFC 2822)
// example: Thu, 21 Dec 2000 16:01:07 +0200
 
echo date("m.d.y");
// short-hand date: 03.10.01

The date function change display countless variations of the current date and when a timestamp is added as the second parameter is formats that date instead of the current one. If you want to write in the date function for eg "of" or "the" the characters that could display a date format have to be escaped with an \. The complete table for the date function is available here.

That's it! I hope that if you followed this guide you have learnt something new about the PHP timestamp and I hope you can use this new knowledge in your next PHP project!

If you have any questions, feel free to post on the kirupaForums.

Toodles!

Sam Kellett
Cannedlaughter

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.