当前位置 : 主页 > 网络编程 > PHP >

给定任意时间算出该周的起止时间

来源:互联网 收集:自由互联 发布时间:2021-07-03
function getWeekDate($timestamp) { //年份 如2015年 $year = date('Y', $timestamp); //本年份第几周 $weeknum = date('W', $timestamp); //年份中的第几天 0 到 365 $day = date('z', $timestamp); //周几 1(表示星期一)到
 
function getWeekDate($timestamp) {
    //年份 如2015年
    $year = date('Y', $timestamp);
    //本年份第几周 
    $weeknum = date('W', $timestamp);
    //年份中的第几天 0 到 365
    $day = date('z', $timestamp);
    //周几 1(表示星期一)到 7(表示星期天) 
    $weekday = date('N', $timestamp);
    //本年的第一天是周几
    $firstDayOfWeek = date('w', mktime(0,0,0,1,1,$year));
    //echo $year.'--'.$weeknum.'--'.$day.'--'.$weekday;
    //本年的第一周
    if ($weeknum == 1) {
        $startdate = date('Y-m-d',mktime(0,0,0,1,1,$year));
        $enddate = date('Y-m-d',mktime(0,0,0,1,7-$firstDayOfWeek+1,$year));
        //最后几天并入下一年第一周的情况,如2014/12/29算2015年第一周 
        //非下一年第一周 (>7表示前一年的最后几天)
        if ($day > 7) {
            //每周的开始时间 $day+1-$weekday+1
            $startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
            $enddate = date('Y-m-d',mktime(0,0,0,12,31,$year));
        }
    } elseif ($weeknum == 53) { //本年最后一周
        $startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
        $enddate = date('Y-m-d',mktime(0,0,0,12,31,$year));
    }else {//其他情况
        $startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
        $enddate = date('Y-m-d',mktime(0,0,0,1,$day+7-$weekday+1,$year));
    }
    return array($startdate,$enddate);     
}

网友评论