Home  >  Article  >  Backend Development  >  How to convert string to timestamp in php?

How to convert string to timestamp in php?

青灯夜游
青灯夜游Original
2020-07-23 15:44:139155browse

In PHP, you can use the strtotime() function to convert a string into a timestamp. The strtotime() function can convert the date represented by the English text string into a timestamp. It is the inverse function of date() and returns the timestamp successfully, otherwise it returns FALSE.

How to convert string to timestamp in php?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

strtotime() function is used to convert English text characters The date represented by the string is converted into a timestamp, which is the inverse function of date(). It returns the timestamp successfully, otherwise it returns FALSE.

Syntax:

int strtotime ( string time [, int now] )

This function expects a string containing a US English date format and attempts to parse it into a Unix timestamp (since January 1 1970 00: 00:00 GMT), its value is relative to the time given by the now parameter. If this parameter is not provided, the current system time is used.

This function will use the TZ environment variable (if any) to calculate the timestamp. Since PHP 5.1.0 there is an easier way to define the time zone used in all date/time functions. This process is documented on the date_default_timezone_get() function page.

Parameters:

  • #time: is the parsed date/time string, which is a date represented according to the GNU date input format.

  • now: The timestamp used to calculate the return value.

Return value:

Returns the timestamp if successful, otherwise returns FALSE. Prior to PHP 5.1.0, this function returned -1 on failure. [Recommended learning: "PHP Video Tutorial"]

Example:

<?php
echo strtotime("2009-10-21 16:00:10");    //输出 1256112010
echo strtotime("10 September 2008");    //输出 1220976000
echo strtotime("+1 day"), "<br />";    //输出明天此时的时间戳
?>

Recommended learning: PHP Programming from Beginner to Mastery

The above is the detailed content of How to convert string to timestamp in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP class usage problemNext article:PHP class usage problem