根据日期获取是星期几

php

浏览数:395

2019-1-8

/**
     * 获取星期几
     * @param $date
     * @return
     */
    public static function getWeekDay($date) {
        //强制转换日期格式
        $dateStr=date('Y-m-d',strtotime($date));
        //封装成数组
        $arr=explode("-", $dateStr);
        //参数赋值
        //年
        $year=$arr[0];

        //月,输出2位整型,不够2位右对齐
        $month=sprintf('%02d',$arr[1]);
        //日,输出2位整型,不够2位右对齐
        $day=sprintf('%02d',$arr[2]);

        //时分秒默认赋值为0;
        $hour = $minute = $second = 0;

        //转换成时间戳
        $strap = mktime($hour,$minute,$second,$month,$day,$year);

        //获取数字型星期几
        $numberWk=date("w",$strap);

        //自定义星期数组
        $weekArr=array(7,1,2,3,4,5,6);

        //获取数字对应的星期
        return $weekArr[$numberWk];
    }