PHP 日期和时间函数

PHP strtotime() 函数用于将日期字符串时间转换为 Unix 时间戳。

它是php日期转时间戳的函数。时间戳转日期可以使用 date() 函数。

语法

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

参数

参数描述必须/可选
time指定日期/时间字符串必须
now计算相对日期的时间戳。可选

返回值

成功时返回时间戳,失败返回 FALSE。

例子1

开发中,我们经常使用的日期格式。
<?php
echo strtotime("2023-03-28 11:11:11"), "\n"; 
echo strtotime("2023-3-28 11:11:11"), "\n"; 

?> 
输出:
1679973071
1679973071

例子2

加或减的例子
<?php
echo(strtotime("now") . "\n");
echo(strtotime("3 October 2005") . "\n");
echo(strtotime("+5 hours") . "\n");
echo(strtotime("+1 week") . "\n");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "\n");
echo(strtotime("next Monday") . "\n");
echo(strtotime("last Sunday"));
?> 

输出:

1679104222
1128268800
1679122222
1679709022
1679993427
1679241600
1678550400

例子3

<?php
echo strtotime("next sunday"), "\n"; 
echo date("Y-m-d", strtotime("next sunday"))."\n";
?> 

输出:

1679155200
2023-03-19

例子4

<?php
echo strtotime("next friday"), "\n"; 
echo date("Y-m-d", strtotime("next friday"))."\n";
?> 

输出:

1679587200
2023-03-24