Home > Backend Development > PHP Tutorial > PHP implementation to calculate the start date and end date of the week of a given date

PHP implementation to calculate the start date and end date of the week of a given date

墨辰丷
Release: 2023-03-28 06:32:02
Original
1641 people have browsed it

This article mainly introduces PHP to calculate the start date and end date of the week of a given date, involving PHP date and time related operations and conversion skills. It has certain reference value. Friends in need can refer to it

The details are as follows:

<?php
/**
 * 取得给定日期所在周的开始日期和结束日期
 * @param string $gdate 日期,默认为当天,格式:YYYY-MM-DD
 * @param int $weekStart 一周以星期一还是星期天开始,0为星期天,1为星期一
 * @return array 数组array( "开始日期 ",  "结束日期");
 */
function getAWeekTimeSlot($gdate = &#39;&#39;, $weekStart = 0) {
 if (! $gdate){
 $gdate = date ( "Y-m-d" );
 }
 $w = date ( "w", strtotime ( $gdate ) ); //取得一周的第几天,星期天开始0-6
 $dn = $w ? $w - $weekStart : 6; //要减去的天数
 $st = date ( "Y-m-d", strtotime ( "$gdate  - " . $dn . "  days " ) );
 $en = date ( "Y-m-d", strtotime ( "$st  +6  days " ) );
 return array ($st, $en ); //返回开始和结束日期
}
$timeSlot=getAWeekTimeSlot(&#39;2017-01-24&#39;,1);
echo "Week Start:{$timeSlot[0]}--->Week End: {$timeSlot[1]} ";
?>
Copy after login

The running results are as follows:

Week Start:2017-01-23--->Week End: 2017-01-29
Copy after login

The above is this article The entire content, I hope it will be helpful to everyone's study.


Related recommendations:

php method to get the number of days difference between a given date

PHP implementation method to obtain the current date and the day of the month this Monday is

PHP implementation is for Methods for addition and subtraction of dates, months, days, weeks, hours, minutes, seconds, etc.

##

The above is the detailed content of PHP implementation to calculate the start date and end date of the week of a given 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