PHP 获取某段时间内所有的周的方法,返回有开始时间到结束时间所有周
<?php
/**
* 获取某段时间内所有的周的方法,返回有开始时间到结束时间所有周(1-6)
* @param $start_date
* @param $end_date
* @return array|bool
*/
function getWeekFromRange($start_date, $end_date) {
$start_timestamp = strtotime ( date ( 'Y-m-d' . ' 00:00:00', $start_date ) );
$end_timestamp = strtotime ( date ( 'Y-m-d' . ' 23:59:59', $end_date) );
if ($start_timestamp>=$end_timestamp) return false;
$weeks = floor ( ($end_timestamp - $start_timestamp) / (86400 * 7) );
$week = array ();
for($i = 0; $i <= $weeks; $i ++) {
if ($i == 0) {
$startday = date ( 'Y-m-d' . ' 00:00:00', $start_timestamp );
$endday = date ( "Y-m-d H:i:s", mktime ( 23, 59, 59, date ( "m", $start_timestamp ), date ( "d", $start_timestamp ) - date ( "w", $start_timestamp ) + 7, date ( "Y", $start_timestamp ) ) );
} elseif ($i == $weeks) {
$startday = date ( "Y-m-d H:i:s", mktime ( 0, 0, 0, date ( "m", $start_timestamp ), date ( "d", $start_timestamp ) - date ( "w", $start_timestamp ) + 1, date ( "Y", $start_timestamp ) ) );
$endday = date ( 'Y-m-d' . ' 00:00:00', $end_timestamp );
} else {
$startday= date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m",$start_timestamp),date("d",$start_timestamp)-date("w",$start_timestamp)+1,date("Y",$start_timestamp)));
$endday = date("Y-m-d H:i:s",mktime(23,59,59,date("m",$start_timestamp),date("d",$start_timestamp)-date("w",$start_timestamp)+7,date("Y",$start_timestamp)));
}
$start_timestamp += 86400 * 7;
$week [$i] = array (
$startday,
$endday
);
}
return $week;
}
?>
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。