85

我有一个像这样的 Unix 时间戳:

$timestamp=1330581600

我如何获得该时间戳的一天的开始和一天的结束?

e.g.
$beginOfDay = Start of Timestamp's Day
$endOfDay = End of Timestamp's Day

我试过这个:

$endOfDay = $timestamp + (60 * 60 * 23);

但我认为它不会起作用,因为时间戳本身并不是一天的确切开始。

4

12 回答 12

188

strtotime 可用于快速切断小时/分钟/秒

$beginOfDay = strtotime("today", $timestamp);
$endOfDay   = strtotime("tomorrow", $beginOfDay) - 1;

也可以使用 DateTime,但需要一些额外的步骤才能从长时间戳中获取

$dtNow = new DateTime();
// Set a non-default timezone if needed
$dtNow->setTimezone(new DateTimeZone('Pacific/Chatham'));
$dtNow->setTimestamp($timestamp);

$beginOfDay = clone $dtNow;
$beginOfDay->modify('today');

$endOfDay = clone $beginOfDay;
$endOfDay->modify('tomorrow');
// adjust from the start of next day to the end of the day,
// per original question
// Decremented the second as a long timestamp rather than the
// DateTime object, due to oddities around modifying
// into skipped hours of day-lights-saving.
$endOfDateTimestamp = $endOfDay->getTimestamp();
$endOfDay->setTimestamp($endOfDateTimestamp - 1);

var_dump(
    array(
        'time ' => $dtNow->format('Y-m-d H:i:s e'),
        'start' => $beginOfDay->format('Y-m-d H:i:s e'),
        'end  ' => $endOfDay->format('Y-m-d H:i:s e'),
    )
);

随着 PHP7 中增加的时间延长,如果使用$now <= $end此检查,可能会错过一秒钟。$now < $nextStart除了在 PHP 的时间处理中减去秒数和夏令时的奇怪之处外,使用检查可以避免这种差距。

于 2012-04-17T19:39:23.213 回答
16

只是日期时间

$beginOfDay = DateTime::createFromFormat('Y-m-d H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Y-m-d 00:00:00'))->getTimestamp();
$endOfDay = DateTime::createFromFormat('Y-m-d H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Y-m-d 23:59:59'))->getTimestamp();

首先创建一个 DateTime 对象,并将时间戳设置为所需的时间戳。然后对象被格式化为字符串,将小时/分钟/秒设置为一天的开始或结束。最后,从该字符串创建一个新的 DateTime 对象并检索时间戳。

可读

$dateTimeObject = new DateTime();
$dateTimeObject->setTimestamp($timestamp);
$beginOfDayString = $dateTimeObject->format('Y-m-d 00:00:00');
$beginOfDayObject = DateTime::createFromFormat('Y-m-d H:i:s', $beginOfDayString);
$beginOfDay = $beginOfDayObject->getTimestamp();

我们可以使用这个更长的版本以另一种方式结束一天:

$endOfDayObject = clone $beginOfDayOject(); // Cloning because add() and sub() modify the object
$endOfDayObject->add(new DateInterval('P1D'))->sub(new DateInterval('PT1S'));
$endOfDay = $endOfDayOject->getTimestamp();

时区

也可以通过在格式中添加时间戳指示符来设置时区,例如O在创建 DateTime 对象后指定时间戳:

$beginOfDay = DateTime::createFromFormat('Y-m-d H:i:s O', (new DateTime())->setTimezone(new DateTimeZone('America/Los_Angeles'))->setTimestamp($timestamp)->format('Y-m-d 00:00:00 O'))->getTimestamp();

DateTime 的灵活性

我们还可以通过更改指定的第二种格式来获取其他信息,例如月份的开始/结束或小时的开始/结束。对于月份:'Y-m-01 00:00:00''Y-m-t 23:59:59'。小时:'Y-m-d H:00:00''Y-m-d H:59:59'

将各种格式与 add()/sub() 和 DateInterval 对象结合使用,我们可以获得任何时期的开始或结束,尽管需要注意正确处理闰年。

相关链接

来自 PHP 文档:

于 2016-01-04T18:07:59.563 回答
9

您可以使用date()和的组合mktime()

list($y,$m,$d) = explode('-', date('Y-m-d', $ts));
$start = mktime(0,0,0,$m,$d,$y);
$end = mktime(0,0,0,$m,$d+1,$y);

mktime()在指定月份之外的一天(1 月 32 日将是 2 月 1 日等)时,足够聪明地包装几个月/年

于 2012-04-17T19:38:54.750 回答
4

您可以将时间转换为当前数据,然后使用 strtotime 函数查找一天的开始,然后简单地添加 24 小时来查找一天的结束。

您还可以使用余数运算符 (%) 来查找最近的日期。例如:

$start_of_day = time() - 86400 + (time() % 86400);
$end_of_day = $start_of_day + 86400;
于 2012-04-17T19:36:29.153 回答
4

不幸的是,由于在非常特定的情况下发生的 php 错误,已接受的答案中断了。我将讨论这些场景,但首先使用 DateTime 来回答。这与接受的答案之间的唯一区别出现在以下// IMPORTANT行之后:

$dtNow = new DateTime();
// Set a non-default timezone if needed
$dtNow->setTimezone(new DateTimeZone('America/Havana'));
$dtNow->setTimestamp($timestamp);

$beginOfDay = clone $dtNow;

// Go to midnight.  ->modify('midnight') does not do this for some reason
$beginOfDay->modify('today');

// now get the beginning of the next day
$endOfDay = clone $beginOfDay;
$endOfDay->modify('tomorrow');

// IMPORTANT
// get the timestamp
$ts = $endOfDay->getTimestamp();
// subtract one from that timestamp
$tsEndOfDay = $ts - 1;

// we now have the timestamp at the end of the day. we can now use that timestamp
// to set our end of day DateTime
$endOfDay->setTimestamp($tsEndOfDay);

所以你会注意到,我们不是使用->modify('1 second ago');我们而是获取时间戳并减去一个。接受的答案使用modify 应该可以工作,但在非常特定的场景中会因为 php 错误而中断。此错误发生在更改夏令时午夜的时区中,即一年中时钟“向前”移动的那一天。这是一个可用于验证该错误的示例。

错误示例代码

// a time zone, Cuba, that changes their clocks forward exactly at midnight. on
// the day before they make that change. there are other time zones which do this
$timezone = 'America/Santiago';
$dateString = "2020-09-05";

echo 'the start of the day:<br>';
$dtStartOfDay = clone $dtToday;
$dtStartOfDay->modify('today');
echo $dtStartOfDay->format('Y-m-d H:i:s');
echo ', '.$dtStartOfDay->getTimestamp();

echo '<br><br>the start of the *next* day:<br>';
$dtEndOfDay = clone $dtToday;
$dtEndOfDay->modify('tomorrow');
echo $dtEndOfDay->format('Y-m-d H:i:s');
echo ', '.$dtEndOfDay->getTimestamp();

echo '<br><br>the end of the day, this is incorrect. notice that with ->modify("-1 second") the second does not decrement the timestamp by 1:<br>';
$dtEndOfDayMinusOne = clone $dtEndOfDay;
$dtEndOfDayMinusOne->modify('1 second ago');
echo $dtEndOfDayMinusOne->format('Y-m-d H:i:s');
echo ', '.$dtEndOfDayMinusOne->getTimestamp();

echo '<br><br>the end of the day, this is correct:<br>';
$dtx = clone $dtEndOfDay;
$tsx = $dtx->getTimestamp() - 1;
$dty = clone $dtEndOfDay;
$dty->setTimestamp($tsx);
echo $dty->format('Y-m-d H:i:s');
echo ', '.$tsx;

错误示例代码输出

the start of the day:
2020-03-26 00:00:00, 1585173600

the start of the *next* day:
2020-03-27 01:00:00, 1585260000

the end of the day, this is incorrect. notice that with ->modify("1 second ago") the
second does not decrement the timestamp by 1:
2020-03-27 01:59:59, 1585263599

the end of the day, this is correct:
2020-03-26 23:59:59, 1585259999
于 2019-10-18T14:41:13.290 回答
3

今天开始日期时间戳。简单的

$stamp = mktime(0, 0, 0);
echo date('m-d-Y H:i:s',$stamp);
于 2016-05-19T06:56:37.260 回答
1
$start_of_day = floor (time() / 86400) * 86400;
$end_of_day = ceil (time() / 86400) * 86400;

如果您需要在同一个脚本中的两个值。对于其中一个变量来说,+/- 86400 秒比同时触发 floor 和 ceil 更快。例如:

$start_of_day = floor (time() / 86400) * 86400;
$end_of_day = $start_of_day + 86400;
于 2016-03-22T22:56:23.653 回答
0

对于将来有此问题的任何人:

任何一天的代码

<?php
$date = "2015-04-12 09:20:00";

$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);

$diff = $now - $midnight;
echo $diff;
?>

当日代码

<?php
$midnight = strtotime("midnight");
$now = date('U');

$diff = $now - $midnight;
echo $diff;
?>
于 2015-03-30T09:29:19.553 回答
0
$date = (new \DateTime())->setTimestamp(1330581600);

echo $date->modify('today')->format('Y-m-d H:i:s'); // 2012-02-29 00:00:00
echo PHP_EOL;
echo $date->modify('tomorrow - 1 second')->format('Y-m-d H:i:s'); // 2012-02-29 23:59:59
于 2021-04-29T13:39:33.740 回答
0
    $startOfDay = new \DateTime('tomorrow');
    $startOfDay->modify('-1 day');

这对我有用:)

于 2021-05-17T13:21:14.810 回答
0

聚会有点晚了,但这是实现您正在寻找的另一种简单方法:

$timestamp=1330581600;
$format = DATE_ATOM;

$date = (new DateTime())->setTimestamp($timestamp);
// Here's your initial date, created from the timestamp above
// 2012-03-01T06:00:00+00:00
$dateFromTimestamp = $date->format($format);

// This is the beginning of the day
// 2012-03-01T00:00:00+00:00
$startOfDay = $date->setTime(0,0);

// This is the beginning of the next day
// 2012-03-02T00:00:00+00:00
$startOfNextDay = $startOfDay->modify('+1 day');

除非绝对必要,否则我个人会避免使用一天结束。当然,您可以使用23:59:59,但这不是一天的实际结束(还剩 1 秒)。我所做的是使用第二天的开始作为我的结束边界,例如:

$start = new DateTime('2021-11-09 00:00:00');
$end = new DateTime('2021-11-10 00:00:00');
    
if ($someDateTime >= $start && $someDateTime < $end) {
    // do something
}

如果我必须使用一天的结束,我会计算第二天的开始,然后从中减去 1微秒

于 2021-11-09T15:55:20.663 回答
0
$beginOfDay = (new DateTime('today', new DateTimeZone('Asia/Tehran')))->getTimestamp();
$endOfDay   = $beginOfDay + 86399;

您可以通过替换“亚洲/德黑兰”来设置时区。一天是86400秒,不要问我为什么是86399,是我脑海里的一个耳语说是86399,所以我什至不想去想它的真相。

于 2022-02-18T20:47:42.357 回答