从特定时间戳获取午夜时间戳值


Getting midnight timestamp value from specific timestamp

我只想计算时间戳的午夜值。例如1461043742,这是今天的时间戳,从中我想要午夜12点的时间戳值。这是1461130928明天的时间戳,我想显示明天午夜12点的时间戳值。我该怎么做,有没有可能在PHP中实现这一点?

您可以使用strtotime:

// Input:
$now = 1461043742;
// Calculate:
$midnight = strtotime("midnight", $now);
$midnight_tomorrow = strtotime("midnight tomorrow", $now);
// Test
print(date('c', $midnight));
print(date('c', $midnight_tomorrow));

您需要将小时和分钟更改为午夜。

$date = new DateTime() // By default gets the current time
$date.setTime(0,0,0) // Midnight hour
$stamp = date.getTimestamp()

您可以使用strtotime

$date = '2016-04-18 00:00:00';
$timestamp = strtotime($date);
$date = date('Y-m-d 00:00:00');
$timestamp = strtotime($date);