PHP Datetime:如何获取一天中的第一个小时和最后一个小时?

发布于 2025-01-14 20:23:26 字数 1879 浏览 0 评论 0原文

在 PHP \DateTimeCarbon 中,很容易执行诸如 new \DateTime('first day of last Month'); 或 < code>new \DateTime('上个月的最后一天'),我想知道是否有类似于最后一天的第一个小时最后一天的最后一个小时的东西?月和周的现有代码如下所示,我需要在日级别执行此操作并相应地分配这些变量:

if ($this->monthWeekSelect === 'month') {
    $this->lastMonthFirstDay = new \DateTime('first day of last month');
    $this->lastMonthLastDay = new \DateTime('last day of last month');
    $this->currentMonthFirstDay = new \DateTime('first day of this month');
    $this->currentMonthLastDay = new \DateTime('last day of this month');
    $this->oneMonthFirstDay = new \DateTime('first day of +1 month');
    $this->oneMonthLastDay = new \DateTime('last day of +1 month');
    $this->twoMonthFirstDay = new \DateTime('first day of +2 month');
    $this->twoMonthLastDay = new \DateTime('last day of +2 month');
    $this->threeMonthFirstDay = new \DateTime('first day of +3 month');
    $this->threeMonthLastDay = new \DateTime('last day of +3 month');
} else { // For Week
    $this->lastMonthFirstDay = new \DateTime('monday last week');
    $this->lastMonthLastDay = new \DateTime('sunday last week');
    $this->currentMonthFirstDay = new \DateTime('monday this week');
    $this->currentMonthLastDay = new \DateTime('sunday this week');
    $this->oneMonthFirstDay = (new Carbon('monday this week'))->addWeek(1);
    $this->oneMonthLastDay = (new Carbon('sunday this week'))->addWeek(1);
    $this->twoMonthFirstDay = (new Carbon('monday this week'))->addWeek(2);
    $this->twoMonthLastDay = (new Carbon('sunday this week'))->addWeek(2);
    $this->threeMonthFirstDay = (new Carbon('monday this week'))->addWeek(3);
    $this->threeMonthLastDay = (new Carbon('sunday this week'))->addWeek(3);
}

In PHP \DateTime or Carbon, it is easy to do things like new \DateTime('first day of last month'); or new \DateTime('last day of last month'), I want to know whether there is something similar like first hour of last day and last hour of last day? The existing code for Month and week look like the below, I need to do it at the day level and assign these variable accordingly:

if ($this->monthWeekSelect === 'month') {
    $this->lastMonthFirstDay = new \DateTime('first day of last month');
    $this->lastMonthLastDay = new \DateTime('last day of last month');
    $this->currentMonthFirstDay = new \DateTime('first day of this month');
    $this->currentMonthLastDay = new \DateTime('last day of this month');
    $this->oneMonthFirstDay = new \DateTime('first day of +1 month');
    $this->oneMonthLastDay = new \DateTime('last day of +1 month');
    $this->twoMonthFirstDay = new \DateTime('first day of +2 month');
    $this->twoMonthLastDay = new \DateTime('last day of +2 month');
    $this->threeMonthFirstDay = new \DateTime('first day of +3 month');
    $this->threeMonthLastDay = new \DateTime('last day of +3 month');
} else { // For Week
    $this->lastMonthFirstDay = new \DateTime('monday last week');
    $this->lastMonthLastDay = new \DateTime('sunday last week');
    $this->currentMonthFirstDay = new \DateTime('monday this week');
    $this->currentMonthLastDay = new \DateTime('sunday this week');
    $this->oneMonthFirstDay = (new Carbon('monday this week'))->addWeek(1);
    $this->oneMonthLastDay = (new Carbon('sunday this week'))->addWeek(1);
    $this->twoMonthFirstDay = (new Carbon('monday this week'))->addWeek(2);
    $this->twoMonthLastDay = (new Carbon('sunday this week'))->addWeek(2);
    $this->threeMonthFirstDay = (new Carbon('monday this week'))->addWeek(3);
    $this->threeMonthLastDay = (new Carbon('sunday this week'))->addWeek(3);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2025-01-21 20:23:26

时间可以与相对日期表达式一起编写。

$lastHourCurrentDay = date_create('today 23:00');  //or new DateTime('today 23:00');
$firstHourCurrentDay = date_create('today');  //or 'today 00:00'
$lastHourYesterday = new DateTime('Yesterday 23:00');

Times can be written together with relative date expressions.

$lastHourCurrentDay = date_create('today 23:00');  //or new DateTime('today 23:00');
$firstHourCurrentDay = date_create('today');  //or 'today 00:00'
$lastHourYesterday = new DateTime('Yesterday 23:00');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文