美文网首页
php DateTime类使用

php DateTime类使用

作者: 旅行者xy | 来源:发表于2019-04-03 22:02 被阅读0次
  • 获取一个月的时间范围
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];
}

相关文章

网友评论

      本文标题:php DateTime类使用

      本文链接:https://www.haomeiwen.com/subject/ouitiqtx.html