在 php 中获取今天和昨天的时间戳

2022-08-30 14:22:47

如何在php中使用strtotime()函数获得今天,昨天和前天的12点钟的时间戳?

12点钟是一个变量,将由用户更改。


答案 1
$hour = 12;

$today              = strtotime($hour . ':00:00');
$yesterday          = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);

答案 2

strtotime支持许多可以使用的有趣修饰符

$hour = 12;

$today              = strtotime("today $hour:00");
$yesterday          = strtotime("yesterday $hour:00");
$dayBeforeYesterday = strtotime("yesterday -1 day $hour:00");

echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);

它按预期工作:

2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00

推荐