Converting from a string

int strtotime ( string time [, int now])

Converting a date from user input to a Unix timestamp can be very tricky, because some people use DD/MM/YY (two-digit day, two-digit month, two-digit year), some use YY-MM-DD, some use DDDD-MM-YYYY, and others use even crazier variants!

PHP provides a function to help you convert strings to a timestamp, however, and it generally does a good job. Strtotime(), the function in question, takes two parameters: the string time to convert, and a second optional parameter that can be a relative timestamp.

Parameter one is important; we will come back to parameter two shortly.

Consider this script:

    print strtotime("22nd December 1979");
    print strtotime("22 Dec. 1979 17:30");
    print strtotime("1979/12/22");

Here we have three ways of representing the same date, with the second option also including a time. If you try running that script, you will see PHP outputs an integer for each one, with the first and third being the same, and the second one being slightly higher. These numbers, as you will see later, are the Unix timestamps for the dates we passed into strtotime() - it successfully managed to convert them.

Note that strtotime() uses American-style dates, which means that if it finds a date like 10/11/2003, it will consider it to be October the 11th as opposed to November the 10th.

If PHP is unable to convert your string into a timestamp, it will return -1. You might want to use code along the lines of this next example to test whether your date conversion worked or not:

    $mydate = strtotime("Christmas 1979");
    if ($mydate == -1) {
        print "Date conversion failed!";
    } else {
        print "Date conversion succeeded!";

Now, strtotime() has an optional second parameter, which is a timestamp to provide relative dates from. The reason for this is because the first parameter to strtotime(), the date string, can include relative dates such as "Next Sunday", "2 days", or "1 year ago". In this situation, PHP needs to know what to base these relative times on, and this is where the second parameter comes in - you can provide any timestamp in there you want, and PHP will calculate "Next Sunday" from that timestamp. If you do not provide parameter two and use a relative time for parameter one, PHP assumes you are referring to the current time.

Here are some examples of use:

    print strtotime("Next Sunday");
    print strtotime("2 days", time() - (86400 * 2));
    print strtotime("1 year ago", 123456789);

The first line will print the timestamp for the next Sunday (not the upcoming Sunday, but the one after). The second line actually uses time() minus two days as its second parameter, and "2 days" for its first parameter, which means it returns the current timestamp (two days time from now minus two days is now!). The final example simply subtracts a year from a given timestamp, and works as expected.

Converting textual dates to usable dates is not very easy, but it is as easy as it could workably get, thanks to the built-in functions of PHP. You should try experimenting with various string dates to see what you can get to work and what you cannot.

Author's Note: Be wary of dates such as this one: August 25, 2003, 10:26am. Although this may look perfectly well-formed, strtotime() is not able to handle it because it has commas in there - yes, they make it much more readable for us, but strtotime() gets confused handling them. If you have dates with commas in, be sure to strip them out using str_replace().


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Converting to a string >>

Previous chapter: Reading the current time

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.