Home > Backend Development > PHP Tutorial > Explain the relevant content of php to obtain the specified date

Explain the relevant content of php to obtain the specified date

jacklove
Release: 2023-03-31 06:38:02
Original
2409 people have browsed it

Due to work needs, I need to get the start and end dates of the week starting from Thursday, and PHP does not provide a method to get the start and end dates of this week, so I wrote a method for future use.

Calculation method:

<?php/**
 * 计算指定日期的一周开始及结束日期
 * @param  DateTime $date  日期
 * @param  Int      $start 周几作为一周的开始 1-6为周一~周六,0为周日,默认0
 * @retrun Array
 */function getWeekRange($date, $start=0){    // 将日期转时间戳
    $dt = new DateTime($date);    $timestamp = $dt->format(&#39;U&#39;);    // 获取日期是周几
    $day = (new DateTime(&#39;@&#39;.$timestamp))->format(&#39;w&#39;);    // 计算开始日期
    if($day>=$start){        $startdate_timestamp = mktime(0,0,0,date(&#39;m&#39;,$timestamp),date(&#39;d&#39;,$timestamp)-($day-$start),date(&#39;Y&#39;,$timestamp));
    }elseif($day<$start){        $startdate_timestamp = mktime(0,0,0,date(&#39;m&#39;,$timestamp),date(&#39;d&#39;,$timestamp)-7+$start-$day,date(&#39;Y&#39;,$timestamp));
    }    // 结束日期=开始日期+6
    $enddate_timestamp = mktime(0,0,0,date(&#39;m&#39;,$startdate_timestamp),date(&#39;d&#39;,$startdate_timestamp)+6,date(&#39;Y&#39;,$startdate_timestamp));    $startdate = (new DateTime(&#39;@&#39;.$startdate_timestamp))->format(&#39;Y-m-d&#39;);    $enddate = (new DateTime(&#39;@&#39;.$enddate_timestamp))->format(&#39;Y-m-d&#39;);    return array($startdate, $enddate);
}?>
Copy after login

Example: The test starts from Monday to Sunday to calculate the start and end time of the week

<?php$date = &#39;2016-04-27&#39;;for($start=0; $start<=6; $start++){    list($startdate, $enddate) = getWeekRange($date, $start);    echo &#39;date:&#39;.$date.&#39; week start:&#39;.$start.&#39; range:&#39;.$startdate.&#39;, &#39;.$enddate.&#39;<br>&#39;;
}?>
Copy after login

Output:

date:2016-04-27 week start:0 range:2016-04-24, 2016-04-30date:2016-04-27 week start:1 range:2016-04-25, 2016-05-01date:2016-04-27 week start:2 range:2016-04-26, 2016-05-02date:2016-04-27 week start:3 range:2016-04-27, 2016-05-03date:2016-04-27 week start:4 range:2016-04-21, 2016-04-27date:2016-04-27 week start:5 range:2016-04-22, 2016-04-28date:2016-04-27 week start:6 range:2016-04-23, 2016-04-29
Copy after login

This article explains the relevant content of php to obtain the specified date. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

Detailed explanation of how PHP generates a unique RequestID class

How to check the database table capacity through MySQL

Explanation on how to use php str_getcsv to parse a string into an array

The above is the detailed content of Explain the relevant content of php to obtain the specified date. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template