phptimestampstrtotime

What is a Unix timestamp and why use it?


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().


Solution

  • 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():

    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.

    Here are some good articles on DateTime:

    And a book for the shelf: