Get all week start date and end date within a date range in php

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

How to get all week start date and end date within a date range in PHP?

Week start = Sunday and week end = Saturday

Input

$start_date='2013-02-01'
$end_date = '2013-02-28'

Output

start date='2013-02-01' End date ='2013-02-02'
start date='2013-02-03' End date ='2013-02-09'
start date='2013-02-10' End date ='2013-02-16'
start date='2013-02-17' End date ='2013-02-23'
start date='2013-02-24' End date ='2013-02-28'

Below code return the week start and end of the date given

 function getWeekDates($date) {     $week =  date('W', strtotime($date));     $year =  date('Y', strtotime($date));     $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week     $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week     echo "Start Date-->".$from."End Date -->".$to;//Output : Start Date-->2012-09-03 End Date-->2012-09-09 }

How can I get the result above?

回答1:

Try this one...

$start_date = date('Y-m-d', strtotime('2013-02-01')); $end_date = date('Y-m-d', strtotime('2013-02-28')); $end_date1 = date('Y-m-d', strtotime('2013-02-28 + 6 days'));  for($date = $start_date; $date <= $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days'))) {     echo getWeekDates($date, $start_date, $end_date);     echo "\n"; }  function getWeekDates($date, $start_date, $end_date) {     $week =  date('W', strtotime($date));     $year =  date('Y', strtotime($date));     $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); //Returns the date of monday in week     if($from < $start_date) $from = $start_date;     $to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));   //Returns the date of sunday in week     if($to > $end_date) $to = $end_date;     echo "Start Date-->".$from."End Date -->".$to;//Output : Start Date-->2012-09-03 End Date-->2012-09-09 }  

See Codepad.



回答2:

my answer.

$start_date = '2013-02-01'; $end_date = '2013-02-28';  getWeekDates($start_date, $end_date);  function getWeekDates($date, $enddate) { $week = date('W', strtotime($date)); $year = date('Y', strtotime($date)); $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week $Edate = strtotime($enddate); $Sdate = strtotime($to); if ($Edate <= $Sdate) {     echo "<br>Start Date-->" . $from . "End Date -->" . $enddate; //Output : Start Date-->2012-09-03 End Date-->2012-09-09  } else {     echo "<br>Start Date-->" . $from . "End Date -->" . $to; //Output : Start Date-->2012-09-03 End Date-->2012-09-09     $to = date("Y-m-d", strtotime("$to +1days")); //Returns the date of monday in week     getWeekDates($to, $enddate); } }


回答3:

I hope I get your question right.

What you need to do is call your function with the start date, then add seven days to the start date and call your function again... You have to do this until you reach the end date.

Here is some untested code:

$current_date = strtotime($start_date); $end_date_as_date = strtotime($end_date);  while($current_date < $end_date_as_date){ getWeekDates($current_date); $current_date= strtotime("+7 day", $current_date); }

Hope this helps. If you need any further information leave a comment.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!