php DateTime类使用
2019-04-03 本文已影响0人
旅行者xy
- 获取一个月的时间范围
function month_range($timeZone = 'UTC')
{
$dateTime = new \DateTime('now', new \DateTimeZone($timeZone));
$dateTime->modify('first day of this month');
$start = $dateTime->format('Y-m-d 00:00:00');
$dateTime->modify('last day of this month');
$end = $dateTime->format('Y-m-d 23:59:59');
return [$start, $end];
}
- 获取当前周的时间范围
function week_range($timeZone = 'UTC')
{
$dateTime = new \DateTime('now', new \DateTimeZone($timeZone));
$dateTime->modify('Monday this week');
$start = $dateTime->format('Y-m-d 00:00:00');
$dateTime->modify('Sunday this week');
$end = $dateTime->format('Y-m-d 23:59:59');
return [$start, $end];
}
- 获取某一年的第几周的时间范围
function year_week_range($year, $week, $timeZone = 'UTC')
{
$dateTime = new \DateTime('now', new \DateTimeZone($timeZone));
$dateTime->setISODate($year, $week);
$dateTime->modify('Monday this week');
$start = $dateTime->format('Y-m-d 00:00:00');
$dateTime->modify('Sunday this week');
$end = $dateTime->format('Y-m-d 23:59:59');
return [$start, $end];
}