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

HP实现计算一年多少个星期,返回一个星期的开始时间和结束时间(可选返回时

来源:互联网 收集:自由互联 发布时间:2021-07-03
function getWeekStartAndEnd ($year,$week=1) { header("Content-type:text/html;charset=utf-8"); date_default_timezone_set("Asia/Shanghai"); $year = (int)$year; $week = (int)$week; //按给定的年份计算本年周总数 $date = new DateTime
function getWeekStartAndEnd ($year,$week=1) {
    header("Content-type:text/html;charset=utf-8");
    date_default_timezone_set("Asia/Shanghai");
    $year = (int)$year;
    $week = (int)$week;
    //按给定的年份计算本年周总数
    $date = new DateTime;
    $date->setISODate($year, 53);
    $weeks = max($date->format("W"),52);
    //如果给定的周数大于周总数或小于等于0
    if($week>$weeks || $week<=0){
        return false;
    }
    //如果周数小于10
    if($week<10){
        $week = '0'.$week;
    }
    //当周起止时间戳
    $timestamp['start'] = strtotime($year.'W'.$week);
    $timestamp['end'] = strtotime('+1 week -1 day',$timestamp['start']);
    //当周起止日期
    $timeymd['start'] = date("Y-m-d",$timestamp['start']);
    $timeymd['end'] = date("Y-m-d",$timestamp['end']);    
    //返回起始时间戳
    return $timestamp;
    //返回日期形式
    //return $timeymd;
}
网友评论