• 使用PHP循环出本周一到本周日

    2018-10-07 22:10:29

    在PHP中有非常好用的时间函数,比如date和strtotime,结合时间戳不仅给数据库设计带来了便利,也让我们更容易的把控对时间的获取。

    <?php
    date_default_timezone_set('PRC'); //设置中国时区
    function getWeekDate($date = '')
    {
    $date = empty($date)?date('Y-m-d'):$date;
    $Sunday = date("Y-m-d",strtotime("$date Sunday"));
    $Monday = date("Y-m-d",strtotime("$Sunday - 6 days"));
    $Tuesday = date("Y-m-d",strtotime("$Sunday - 5 days"));
    $Wednesday = date("Y-m-d",strtotime("$Sunday - 4 days"));
    $Thursday = date("Y-m-d",strtotime("$Sunday - 3 days"));
    $Friday = date("Y-m-d",strtotime("$Sunday - 2 days"));
    $Saturday = date("Y-m-d",strtotime("$Sunday - 1 days"));

    return [
    '星期一'=>$Monday,
    '星期二'=>$Tuesday,
    '星期三'=>$Wednesday,
    '星期四'=>$Thursday,
    '星期五'=>$Friday,
    '星期六'=>$Saturday,
    '星期天'=>$Sunday
    ];
    }
    $week = getWeekDate();
    foreach ($week as $k => $v){
    echo $k .' => ' .$v.'<br/>';
    }

    strtotime 官方文档参考;

    date  官方文档参考。