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

How to convert time format to timestamp in php?

青灯夜游
Release: 2023-03-07 12:58:01
Original
5758 people have browsed it

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);
Copy after login

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

<?php
echo mktime(21, 50, 55, 07, 14, 2010);
//输出“1279115455”
?>
Copy after login

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() ] )
Copy after login

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:

<?php
echo strtotime("2010-10-1 10:00:00")."<br>";
//输出 1285898400
echo strtotime("10 September 2018")."<br>";
//输出 1536508800
echo strtotime("+1 day");
//输出明天此时的时间戳
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template