PHP Datetime:如何获取一天中的第一个小时和最后一个小时?
在 PHP \DateTime
或 Carbon
中,很容易执行诸如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
时间可以与相对日期表达式一起编写。
Times can be written together with relative date expressions.