Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Working with Date and Time Wish List | Report Bug | About Me ]

4.5.2     Converting from a string: strtotime()

This is NOT the latest copy of this book; click here for the latest version.

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:

<?php
    
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:

<?php
    $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:

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





<< 4.5.1 Reading the current time: time() and microtime()   4.5.3 Converting to a string: date() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 30 Aug 2008

you guys are crazy. i hate you.

A PHP User - 30 Aug 2008

By crazy, wanna great post damt it!!!

A PHP User - 30 Aug 2008

To vlur,
i have got php5.1.4, and for this function to have been mentioned in this book, it must mean it has been available from at least php5.0. And also i doubt paul updates that regularly and this was only downloaded as the latest binary the other day. So i think you must be mistaken. Try running the phpinfo() function and it will tell you your version number.
Chau

A PHP abuser - 30 Aug 2008

My script also returned false.

I have php 4.3.10, or something like that.

vlur - 30 Aug 2008

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

?>

Does not work with PHP 5.1.0. Now it returns FALSE not -1.
So $mydate == FALSE.

A PHP User - 30 Aug 2008

By "crazy" do you mean MM/DD/YYYY? ;-)



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow