PHP converts a standard string format time into a UNIX timestamp _strtotime

Source: Internet
Author: User
Tags string format

PHP converts a standard string format time to a UNIX timestamp function as: strtotime function (PHP 4, PHP 5).

Strtotime function Detailed reference:

strtotime-resolves the datetime description of any English text to a Unix timestamp.

Function Format Description:

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

This function expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (the number of seconds from January 1 1970 00:00:00 GMT) whose value is relative to the time given by the now parameter, if this parameter is not provided, the current time of the system.

This function uses the TZ environment variable, if any, to calculate the timestamp. Since PHP 5.1.0 There is an easier way to define a time zone for all date/time functions. This procedure is described in the Date_default_timezone_get () function page.

Parameter description:

Time

Parsed string, formatted according to GNU? The syntax for the date input format. Before PHP 5.0, there were no milliseconds allowed in time, but could be omitted from PHP 5.0.

Now

The timestamp used to calculate the return value.

The return value is interpreted as:

Success returns a timestamp, otherwise FALSE. This function returns 1 on failure before PHP 5.1.0.

Attention:

1) If the given year is a two-digit format, its value of 0-69 means that 2000-2069,70-100 represents 1970-2000.

2) return FALSE on Failure in PHP 5.1.0, no longer is-1.

3) before PHP 5 to 5.0.2, "now" and other relative times are calculated incorrectly from today's midnight. This differs from the other versions that are correctly calculated from the current time.

4) Valid timestamps are usually from Fri, Dec 1901 20:45:54 GMT to Tue, Jan 2038 03:14:07 GMT (corresponds to the minimum and maximum values of 32-bit signed integers). Not all platforms support negative timestamps, so the journal scope is limited to not earlier than the Unix era. This means that dates before January 1, 1970 will not be available in Windows, some Linux versions, and several other operating systems. However, PHP 5.1.0 and newer versions overcome this limitation.

code example:

1). Code Snippet 1:

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

2). Code Snippet 2:

1 2 3 4 5 6 7 8 9 <?php $str‘Not Good‘; // previous to PHP 5.1.0 you would compare with -1, instead of false if(($timestampstrtotime($str)) === false) {     echo"The string ($str) is bogus" ; else{     echo"$str == "date(‘l dS of F Y h:i:s A‘$timestamp); } ?>

3). Code Snippet 3:

1 2 3 4 <?php $time     =     "2011-03-17 23:59:00"     ; $unixtime     =     strtotime     ( & nbsp   $time     ); ?




    • 169IT Station articles in addition to the original, are reproduced, collated or collected from the network. Welcome any kind of reprint, reprint please indicate the source.
      Reprint Please specify: The article is reproduced from: [169it-latest and most comprehensive it information]
      Title: PHP converts standard string format time to UNIX timestamp _strtotime


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.