Home > Article > Backend Development > php calculate start date and end date of week for given date
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 example in this article describes how PHP calculates the start date and end date of the week of a given date. Share it with everyone for your reference, the details are as follows:
<?php /** * 取得给定日期所在周的开始日期和结束日期 * @param string $gdate 日期,默认为当天,格式:YYYY-MM-DD * @param int $weekStart 一周以星期一还是星期天开始,0为星期天,1为星期一 * @return array 数组array( "开始日期 ", "结束日期"); */ function getAWeekTimeSlot($gdate = '', $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('2017-01-24',1); echo "Week Start:{$timeSlot[0]}--->Week End: {$timeSlot[1]} "; ?>
The running results are as follows:
Week Start:2017-01-23--->Week End: 2017-01-29
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php implements calculation of the start of the week where the given date is date And end date
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
The above is the detailed content of php calculate start date and end date of week for given date. For more information, please follow other related articles on the PHP Chinese website!