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

How to convert time format to timestamp in php?

青灯夜游
青灯夜游 Original
2020-11-06 15:03:53 5652browse

How to convert PHP time format to timestamp: 1. Use mktime() to convert time to timestamp, syntax "mktime(hour, minute, second, month, day, year)"; 2. Use strtotime() converts a date represented by a string into a timestamp, using the syntax "strtotime (date/time string)".

How to convert time format to timestamp in php?

Recommended: "PHP Video Tutorial"

Method 1: Use the mktime() function

mktime() function returns the UNIX timestamp of a date

Syntax

mktime(hour,minute,second,month,day,year,is_dst);

Parameters:

  • hour : Optional. Specified hours.

  • minute: Optional. prescribed points.

  • second: Optional. Specifies seconds.

  • month: Optional. Specified month.

  • day: Optional. Specify days.

  • year: Optional. Specified year.

  • is_dst: Optional. Set to 1 if the time is during daylight saving time, 0 otherwise, or -1 (default) if unknown. If unknown, PHP will try to find it itself (possibly producing unexpected results). Note: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used.

Note: Parameters can be omitted from right to left. Any omitted parameters will be set to the current value of the local date and time.

Example

Method 2: Using the strtotime() function

The strtotime() function is used to convert dates represented by strings is the timestamp, returns the timestamp successfully, otherwise returns FALSE.

Syntax:

int strtotime ( string $time [, int $now = time() ] )

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 seconds), 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 Required. Specifies a date/time string.

  • #now Optional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.

Example:

"; //输出 1285898400 echo strtotime("10 September 2018")."
"; //输出 1536508800 echo strtotime("+1 day"); //输出明天此时的时间戳 ?>

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of How to convert time format 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