What is a Unix timestamp and why use it?
Asked Answered
T

2

46

What is a Unix timestamp? In PHP, when working with dates, the function strtotime() outputs some integer value -- what is that? I tried to learn about this but I couldn't get satisfactory answer, especially why do we need to convert dates using strtotime().

Tremolite answered 29/12, 2013 at 6:44 Comment(2)
The unix time stamp is the time in seconds from January 1st, 1970 to the very moment you call for the stamp its self. From here, you can abstract the dates in many languages. strtotime() is just so you can convert a normalized date into an actual unix timestamp for cross language/platform compatibility. not much else to it.Sacristan
$date = "2012-12-29"; $date = strtotime($date); echo $date; output is 1356739200.You mean to say from January 1st, 1970 to 2012-12-29 number of second is 1356739200.Tremolite
K
98

What is a Unix Timestamp

Simply put, the Unix timestamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix timestamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client-side. The reason why Unix timestamps are used by many webmasters is that they can represent all time zones at once. For more information, read the Wikipedia article.

What is strtotime() and how is it useful

As the name suggests, strtotime() function is used to convert a date string to a Unix timestamp (str to time).

From the [PHP manual documentation for strtotime()] 2:

strtotime — Parse about any English textual datetime description into a Unix timestamp

For example, say you wanted to get the Unix timestamp for the date 25 December 2013, then you'd use strtotime() like so:

echo strtotime("25 December 2013"), "\n";       // => 1387909800

strtotime() can also handle relative time and date formats. For example, consider the following:

echo strtotime("+1 month"), "\n";               // => 1390980039
echo strtotime("last day of next month"), "\n"; // => 1391152839

These are some basic examples. strtotime() can handle very complex date formats, too. See the documentation for more information.

When should I use a timestamp

A Unix timestamp is interpreted the same regardless of region and is calculated from the same point in time regardless of the time zone. If you have a web application that is used over multiple timezones and you need date/time to reflect individual users' settings, use a timestamp.

In the case of strtotime(), it is mostly used to convert between date formats. Since strtotime() can parse almost any date string, it's used to convert the date string into a timestamp. Once you have the timestamp, you can format it however you wish, using date(), or similar functions.

Limitations of strtotime()

On a 32-bit system, the maximum value of an integer is 2,147,483,647. The furthest time that can be represented this way is 03:14:07 UTC on Tuesday, 19 January 2038. This is also known as Year 2038 problem.

See this note in the PHP manual:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

Use DateTime objects

If you're working with dates outside the 13 Dec 1901 to 19 Jan 2038 range, then consider using PHP's DateTime objects which can work with a much wider range of dates. DateTime can represent approximately 293 billion years in either direction.

The DateTime class is available on PHP versions >= 5.2.0. If you are running a PHP version that's above >= 5.2.0, then you should use DateTime when working with dates and times. It's the best way to go. If you're having an older PHP version, upgrade already. Anything before 5.3.0 is ancient.

  • Use either DateTime::__construct() or DateTime::createFromFormat() to create a DateTime object. Note that DateTime::createFromFormat() is only available on PHP >= 5.3. Using this method, you can parse date and time weird strings that might otherwise not be possible with strtotime()
  • Use DateTime::format() method to convert your DateTime object to any date format you might want to work with

Here are some good articles on DateTime:

And a book for the shelf:

Keelboat answered 29/12, 2013 at 8:19 Comment(1)
Just to let you know, 2 billion is the SIGNED integer limit, unsigned would be 4 billion.Sammons
T
0

a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.

Truitt answered 17/4, 2023 at 17:28 Comment(2)
While factually correct, you did not explain why to use it (which was asked)Puissance
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Exoteric

© 2022 - 2024 — McMap. All rights reserved.