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

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;;
}
*/
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.