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

How to convert time format to timestamp in php

青灯夜游
青灯夜游Original
2021-03-22 18:35:583238browse

How to convert timestamps in php time format: 1. Use the strtotime() function to convert the date represented by the English text string into a timestamp; 2. Use the mktime() function to return a date UNIX timestamp.

How to convert time format to timestamp in php

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

PHP provides functions that can easily combine various The date in the form is converted into a timestamp. This type of function is mainly:

  • strtotime(): Parses the date and time description of any English text into a timestamp.

  • mktime(): Get timestamp from date.

strtotime()

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

int strtotime ( string time [, int now] )
  • time Required. Specifies a date/time string representing a date according to the GNU date input format.

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

Return value: Returns timestamp if successful, returns FALSE if failed.

Example:

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

Output:

1256112010
1220976000
1616496304

mktime()

mktime() function is used to get a timestamp from a date , returns the timestamp successfully, otherwise returns FALSE. Syntax:

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

How to convert time format to timestamp in php

Example:

<?php
echo mktime(21, 50, 55, 07, 14, 2021);//输出“1626270655”
?>

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

Recommended learning: "PHP Video Tutorial"

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