PYTHON strtotime

is this article helpful?
Python replacement for PHP's strtotime [ edit | history ]
#  Alterntive 1:  use dateutil.parser

>>> import dateutil.parser
>>> dateutil.parser.parse("Tue May 08 15:14:45 +0800 2012")
datetime.datetime(2012, 5, 8, 15, 14, 45, tzinfo=tzoffset(None, 28800))

#  dateutil.parser is available via the Debian package python3-dateutil.

#  Alternative 2:  use time.strptime

#  the closest approximation i have been able to come up with is:

import time
def strtotime(string, format_string = "%m/%d/%Y %H:%M"):
    tuple = time.strptime(string, format_string)
    return int(time.mktime(tuple))

print strtotime("01/01/2000 12:00")
print strtotime("01/01/2000", "%m/%d/%Y")

#  the above code is not ideal as you can only use it on date strings
#  and cannot do such things as "+1 hour" and you also have to
#  specify the string format.

#  Note:  the datetime class also has a (similar?) strptime method.

#  Alternative 3:  use timelime

from timelib import strtotime

#  It uses the timelib package, which can be found through pip...

#  Alternative 4:  use datetime

import datetime
import time

#  Note: the above does not appear to parse a time string.
#  Perhaps this example is on the wrong page?

PHP strtotime

PHP original manual for strtotime [ show | ]


(PHP 4, PHP 5)

strtotimeParse about any English textual datetime description into a Unix timestamp


int strtotime ( string $time [, int $now ] )

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now , or the current time if now is not supplied.

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

Note: If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.



The string to parse. Before PHP 5.0.0, microseconds weren't allowed in the time, since PHP 5.0.0 they are allowed but ignored.


The timestamp used to calculate the returned value.

Return Values

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.


Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT message if using the system settings or the TZ environment variable. See also date_default_timezone_set()


Version Description
5.1.0 It now returns FALSE on failure, instead of -1.

Now issues the E_STRICT and E_NOTICE time zone errors.


Example #1 A strtotime() example

echo strtotime("now"), "\n";
strtotime("10 September 2000"), "\n";
strtotime("+1 day"), "\n";
strtotime("+1 week"), "\n";
strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
strtotime("next Thursday"), "\n";
strtotime("last Monday"), "\n";

Example #2 Checking for failure

'Not Good';

// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp strtotime($str)) === false) {
"The string ($str) is bogus";
} else {
"$str == " date('l dS \o\f F Y h:i:s A'$timestamp);



In PHP 5 up to 5.0.2, "now" and other relative times are wrongly computed from today's midnight. It differs from other versions where it is correctly computed from current time.


In PHP versions prior to 4.4.0, "next" is incorrectly computed as +2. A typical solution to this is to use "+1".

Note: 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.

See Also

  • strptime() - Parse a time/date generated with strftime