PHP strtotime function

 The function of the PHP strtotime function is to convert any date in English format into a Unix timestamp ( timestamp ). There are two parameters for strtotime. If not specifically set, the execution effect of the strtotime function is similar to running time and mktime directly , although time And mktime can also convert simple English time formats to Unix timestamps. For example, "10 September 2014" is a relatively simple English time format. Both time and mktime can actually be converted to Unix timestamps, but the more complex time formats are such as " The English description of "last Monday" and "next Thursday" must be handled by the PHP strtotime function. The strtotime function can support a lot of time formats. The function can be said to be very powerful. It is the PHP designer who is doing date conversion. And a good tool for calculations.


Basic syntax of PHP strtotime function
int strtotime ( string $time , int $now = time() )
The first parameter string $time of strtotime is a string format, which can accept a variety of date string formats. Please refer to the date format mentioned in this " Supported Date and Time Formats " on the official PHP website. The second The parameter is an optional item to calculate the returned timestamp. If it is not set, strtotime will automatically calculate it based on the current timestamp, but this is considered an advanced function and is not used in normal situations. If PHP strtotime successfully converts the date string to a Unix timestamp, it will return the timestamp result. If it fails, it will return FALSE. In versions before PHP 5.1.0, the conversion failure will return -1 instead of FALSE. Please pay more attention when using it. The host PHP version used. PHP strtotime function example 1. Basic conversion



<?php
  $NowTime=date("Y-m-d H:i:s");
  echo strtotime("$NowTime,now"), "<br>";
  echo strtotime("now"), "<br>";
  echo strtotime("10 September 2014"), "<br>";
  echo strtotime("+1 day"), "<br>";
  echo strtotime("+1 week"), "<br>";
  echo strtotime("+2 week 3 days 2 hours 5 seconds"), "<br>";
  echo strtotime("next Thursday"), "<br>";
  echo strtotime("last Monday"), "<br>";
?>
Output result
1412738569
1412738569
1410307200
1412824969
1413343369
1414214574
1412812800
1412553600
Example 1 provides a variety of English date formats for the strtotime function to process. In principle, these formats are accepted by the strtotime function. Pay special attention to the beginning of +1 day. You can clearly see the power of PHP strtotime. Will calculate the time difference we assign by itself, whether it is +1 day, +1 week, or even + weeks, days, hours, and seconds, strtotime can be successfully calculated and converted to the Unix timestamp we want. What other formats are available? Please refer to the PHP official website mentioned in the previous paragraph.

PHP strtotime function example two, back and forth conversion
<?php
  $t='2014-10-18 11:17:23';
  echo $t.'<br>';
  echo strtotime($t).'<br>';
  echo date("Y-m-d H:i:s",strtotime($t)).'<br><br>';
 
  $t2='October 18 2014 11:17:23AM';
  echo $t2.'<br>';
  echo strtotime($t2).'<br>';
  echo date("Y-m-d H:i:s",strtotime($t2));
?>
Output result
2014-10-18 11:17:23
1413631043
2014-10-18 11:17:23

October 18 2014 11:17:23 AM
1413631043
2014-10-18 11:17:23
In order for you to clearly see the result of the conversion of the PHP strtotime function, we used two different time formats to convert it. The first is a more common time format, first converted to a Unix timestamp, and then used the PHP date function Converted back, the result is the same. Then the time format of the second group is more special. It uses the American time format. After the strtotime function is processed, the same Unix timestamp is obtained, and then converted back with the PHP date function, the same time as the first group can be obtained. The PHP strtotime function can be successfully converted to the correct Unix timestamp when processing different time formats (must be in an acceptable format).

Post a Comment

0 Comments