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];
}
上一篇 下一篇

猜你喜欢

热点阅读