cheat sheets.

$ cheat php_strtotime
Parse 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 an 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.

Return Values

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

Errors/Exceptions

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

Example #1 A strtotime() example

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

Example #2 Checking for failure

    <?php
    $str = 'Not Good';
    
    // previous to PHP 5.1.0 you would compare with -1, instead of false
    if (($timestamp = strtotime($str)) === false) {
        echo "The string ($str) is bogus";
    } else {
        echo "$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);
    }
    ?>
Version 1, updated 459 days ago.
. o 0 ( edit | history )
( add new | see all )