Heim >php教程 >PHP源码 >根据给定的日期格式'YYYY-mm-dd',获取该日期所在周的起始日期

根据给定的日期格式'YYYY-mm-dd',获取该日期所在周的起始日期

PHP中文网
PHP中文网Original
2016-05-25 17:03:201365Durchsuche

php代码

/**
 * 获取一年中所有星期的起始日期
 * @param $year 格式 'YYYY'
 * 返回二维数组 下标 key1值对应一年中某一天所在周的实际年份  key2值对应一年中某一天所在的周数[一年中第几周] 
 * 返回false 日期格式错误
 */
function get_all_weeks($year){
    $week_arr = array();
    $year = intval(trim($year));
    $preg = "/^\d{4,}$/";
    if(!preg_match($preg, $year)){
        return false;
    }   
    $begin_day = $year . '-01-01';
    $end_day = $year . '-12-31';
    //一年第一个星期一
    if(date('w',strtotime($begin_day))!=1){
        $begin_day = date('Y-m-d',strtotime("next monday", strtotime($begin_day)));
    }
    //第一个星期一所在周数
    $begin_week_num = intval(date('W',strtotime($begin_day)));
    //一年最后一个星期天
    if(date('w',strtotime($end_day))!=0){
        $end_day = date('Y-m-d',strtotime("last sunday",strtotime($end_day)));
        //存在跨年的星期,跨年所在周星期一
        $end_day_next = date('Y-m-d',strtotime($end_day)+24*60*60);
        //跨年星期所属年份与所在周数
        $stride_year = date('o',strtotime($end_day_next));
        $stride_weeknum = intval(date('W',strtotime($end_day_next)));
    }
    //最后一个星期天所在周数
    $end_week_num = intval(date('W',strtotime($end_day)));
    //一年第一个周一为一年的第一周或者第二周
    if($begin_week_num!=1){
        $i=2;
    }else{
        $i=1;
    }
    $j = 0;
    for($i;$i<=$end_week_num;$i++){      
        $start_date = date("Y-m-d", strtotime("$begin_day $j week"));
        $end_day = date("Y-m-d", strtotime($start_date . &#39;+6 day&#39;));
        $week_arr[$year][$i] = array(
            $start_date,
            $end_day
        );
        $j++;
    }
    if($end_day_next){
        $week_arr[$stride_year][$stride_weeknum] = array(
            $end_day_next,
            date("Y-m-d", strtotime($end_day_next . &#39;+6 day&#39;))
        );
    }
    return $week_arr;
}
//demo_调用
/*
$year = &#39;2013&#39;;
if(get_all_weeks($year)){
    var_dump(get_all_weeks($year));
}else{
    echo &#39;日期格式出错&#39;;
}
*/
/**
 * 获取某一天所在周的起始日期
 * 依赖 function get_all_weeks
 * @param $day 格式:&#39;YYYY-mm-dd&#39;
 * 返回false 日期格式错误
 * 正确,返回json "{"begin_day":"YYYY-mm-dd","end_day":"YYYY-mm-dd"}"
 */
function get_day_week($day){
    $date_arr = explode(&#39;-&#39;, trim($day));
    if(!checkdate(intval($date_arr[1]), intval($date_arr[2]), intval($date_arr[0]))){
        return false;
    }
    $year = date(&#39;Y&#39;,strtotime($day));
    $weeks = get_all_weeks($year);
    //某一天所属的年份与周数
    $real_year = date(&#39;o&#39;,strtotime($day));
    $week_num = intval(date(&#39;W&#39;,strtotime($day)));
    if(!empty($weeks[$real_year][$week_num][0]))
        $begin_day = $weeks[$real_year][$week_num][0];
    if(!empty($weeks[$real_year][$week_num][1]))
        $end_day = $weeks[$real_year][$week_num][1];
    //跨年周数值存储于上一年中
    if(empty($begin_day)||empty($end_day)){
        $year = date(&#39;Y&#39;,strtotime($day))-1;
        $weeks = get_all_weeks($year);
        $real_year = date(&#39;o&#39;,strtotime($day));
        $week_num = intval(date(&#39;W&#39;,strtotime($day)));
        if(!empty($weeks[$real_year][$week_num][0]))
            $begin_day = $weeks[$real_year][$week_num][0];
        if(!empty($weeks[$real_year][$week_num][1]))
            $end_day = $weeks[$real_year][$week_num][1];        
    }
    $the_day = array(
        &#39;begin_day&#39; => $begin_day,
        &#39;end_day&#39; => $end_day
    );
    $the_day = json_encode($the_day);
    return $the_day;
}
//demo_调用
/*
$day = &#39;2014-01-01&#39;;
if(get_day_week($day)){
    var_dump(get_day_week($day));
}else{
    echo &#39;日期格式出错&#39;;
}
*/
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP位运算 详细说明 Nächster Artikel:使用phantomjs生成网站快照