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

How to convert php time to timestamp

藏色散人
藏色散人Original
2020-11-19 09:30:539872browse

How to convert php time to timestamp: First create a PHP sample file; then use the strtotime function to convert the date represented by the text string into a timestamp, or use the mktime function to obtain the timestamp from the date.

How to convert php time to timestamp

The operating environment of this tutorial: Windows 7 system, PHP version 5.6. This method is suitable for all brands of computers.

Recommended: "PHP Video Tutorial"

PHP date format and timestamp conversion

Date conversion to unix timestamp

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

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

Print the timestamp at this time yesterday strtotime("-1 day")

Print the timestamp at this time next week strtotime(" 1 week")

Print the timestamp at this time last week Timestamp strtotime("-1 week")

Get the timestamp of zero o'clock today. To get the unix timestamp of zero o'clock, you can use

$todaytime=strtotime(“today”),

2, mktime()

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

int mktime(hour, minute, second, month, day, year)

Example:

Convert unix timestamp to date

date("Y-m-d H:i:s",time()), "Y-m-d H:i:s" is the converted date format, and time() is the timestamp to obtain the current time. If it is date("Y-m-d H:i:s", time()), the hours, minutes and seconds will be displayed together; if it is

date("Y-m-d ", time()), only the year, month and day will be displayed

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