note 80844 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 03 Aug 2008 11:55:56 +0000 |
| Subject: | note 80844 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143119@lists.php.net to get a copy of this message | ||
Note Submitter: Jeff Stevenson
----
This may not be the best way to do it, but it works very well for me.
<?php
/**
* Function used to find the dates of specific days between
* two different dates. So, if you want to find all the
* mondays and all the wednesdays between 2 dates, this
* function is right for you.
*
* @param string $startDate e.g. 2007-01-31
* @param string $endDate e.g. 2007-12-31
* @param csv string $days e.g. 'Su,M,T,W,TH,F,S';
* @return array
*/
function getDaysBetween($startDate, $endDate, $days){
$endDate = strtotime($endDate);
$days = explode(',', 'M,W');
$dates = array();
foreach($days as $day){
$newDate = $startDate;
switch ($day){
case 'Su':
$day = 'Sun';
break;
case 'M':
$day = 'Mon';
break;
case 'T':
$day = 'Tue';
break;
case 'W':
$day = 'Wed';
break;
case 'Th':
$day = 'Thur';
break;
case 'F';
$day = 'Fri';
break;
case 'S':
$day = 'Sat';
break;
}
while(($date = strtotime($newDate)) <= $endDate){
$dates[] = date('Y-m-d', $date)."\n";
$newDate = date('Y-m-d', $date).' next '.$day;
}
}
sort(array_unique($dates));
return $dates;
}
?>
Example:
$startDate = '2006-08-28';
$endDate = '2006-10-09';
$days = 'M,W';
then
getDaysBetween($startDate, $endDate, $days);
returns
Array
(
[0] => 2006-08-28
[1] => 2006-09-04
[2] => 2006-09-11
[3] => 2006-09-18
[4] => 2006-09-25
[5] => 2006-10-02
[6] => 2006-10-09
[7] => 2006-08-28
[8] => 2006-08-30
[9] => 2006-09-06
[10] => 2006-09-13
[11] => 2006-09-20
[12] => 2006-09-27
[13] => 2006-10-04
)