What is the use of strtotime function in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description. The function returns the time in seconds since the Unix Epoch. We can return the English textual date-time in date format using the date() function.

    Syntax:

    strtotime ($EnglishDateTime, $time_now)

    Parameters: The function accepts two parameters as shown above and described below:

    1. $EnglishDateTime – This parameter specifies the English textual date-time description, which represents the date or time to be returned. The function parses the string and returns us the time in seconds. The parameter is mandatory
    2. $time_now This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.

    Note: Since the time/date is not static, therefore the output will vary.

    Below programs illustrate the strtotime() function.

    Program 1: The below program demonstrates the strtotime()
    function when the english text is “now”.

    echo strtotime("now"), "\n"

    echo date("Y-m-d", strtotime("now"))."\n";

    ?>

    Output:

    1525378260
    2018-05-03
    

    Program 2: The below program demonstrates the strtotime()
    function when the english text is a date.

    echo strtotime("12th february 2017"), "\n"

    echo date("Y-m-d", strtotime("12th february 2017"))."\n";

    ?>

    Output:

    1486857600
    2017-02-12
    

    Program 3: The below program demonstrates the strtotime()
    function when the english text corresponds to any day.

    echo strtotime("next sunday"), "\n"

    echo date("Y-m-d", strtotime("next sunday"))."\n";

    ?>

    Output:

    1525564800
    2018-05-06
    

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    (PHP 4, PHP 5, PHP 7, PHP 8)

    strtotimeParse about any English textual datetime description into a Unix timestamp

    Description

    strtotime(string $datetime, ?int $baseTimestamp = null): int|false

    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 baseTimestamp, or the current time if baseTimestamp is not supplied. The date string parsing is defined in Date and Time Formats, and has several subtle considerations. Reviewing the full details there is strongly recommended.

    Warning

    The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.

    Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

    Parameters

    datetime

    A date/time string. Valid formats are explained in Date and Time Formats.

    baseTimestamp

    The timestamp which is used as a base for the calculation of relative dates.

    Return Values

    Returns a timestamp on success, false otherwise.

    Errors/Exceptions

    Every call to a date/time function will generate a E_WARNING if the time zone is not valid. See also date_default_timezone_set()

    Changelog

    VersionDescription
    8.0.0 baseTimestamp is nullable now.

    Examples

    Example #1 A strtotime() example

    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

    $str 'Not Good';

    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);
    }
    ?>

    Notes

    Note:

    "Relative" date in this case also means that if a particular component of the date/time stamp is not provided, it will be taken verbatim from the baseTimestamp. That is, strtotime('February'), if run on the 31st of May 2022, will be interpreted as 31 February 2022, which will overflow into a timestamp on 3 March. (In a leap year, it would be 2 March.) Using strtotime('1 February') or strtotime('first day of February') would avoid that problem.

    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. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).

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

    For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

    Note:

    Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub().

    See Also

    • DateTimeImmutable
    • DateTimeImmutable::createFromFormat() - Parses a time string according to a specified format
    • Date and Time Formats
    • checkdate() - Validate a Gregorian date
    • strptime() - Parse a time/date generated with strftime

    There are no user contributed notes for this page.

    What does Strtotime return in PHP?

    PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

    How do I change Strtotime?

    Code for converting a string to dateTime $date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

    How do you add hours on Strtotime?

    $timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time ()); I simply want to add three hours and echo it out. I have seen the way where you can do the 60 * 60 * 3 method or the hard code "+ 3 hours" where it understands the words.

    What is Unix timestamp in PHP?

    Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.