Home  >  Article  >  Backend Development  >  PHP timestamps and how to convert between dates

PHP timestamps and how to convert between dates

不言
不言Original
2018-07-17 11:30:236120browse

The conversion between timestamp and date in PHP is realized through the data() function and strtotime() function. The data function realizes the conversion of timestamp to date, and the strtotime function realizes the conversion of date to timestamp.

Time stamp to date date()

  • time()

<?php
var_dump(time()); //获取当前时间戳 int(1502245603)
  • date(format ,timestamp)

format Required. Specifies the format of the timestamp.
timestamp is optional. Specify timestamp. The default is the current time and date

To convert timestamp to date, you can use date('Y-m-s h:i:s', specific timestamp to achieve).

Y : Year (four digits) uppercase
m: month (two digits, add 0 if the first digit is missing) lowercase
d: day (two digits, add 0 if the first digit is missing) lowercase
H: Hour 24-hour hour format with leading zero
h: hour 12-hour hour format with leading zero
i : minute with leading zero
s: seconds with leading zero (00-59)
a: Lowercase noon and afternoon (am or pm)

The distinction between upper and lower case is very important, such as the following example:

var_dump(date(&#39;Y-m-d H:i:s&#39;, 1502204401)); //H 24小时制 2017-08-08 23:00:01 
var_dump(date(&#39;Y-m-d h:i:s&#39;, 1502204401)); //h 12小时制 2017-08-08 11:00:01

Date to timestamp strtotime()

strotime is a very flexible and smart function that can recognize Chinese, English, and numbers. Let’s experience it

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
echo strtotime("20170808 23:00:01"), "\n";
?>

Related recommendations:

How to get the year, month and day in php Timestamp and date methods

Share the method of outputting time by date() function in PHP

The above is the detailed content of PHP timestamps and how to convert between dates. 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