note 80844 added to function.strtotime
| From: | JeffStevenson at osu1 dot php dot net | Date: | Sun, 03 Feb 2008 01:14:42 +0000 |
| Subject: | note 80844 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135744@lists.php.net to get a copy of this message | ||
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
)
----
Server IP: 209.41.74.194
Probable Submitter: 67.182.238.127
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/80844
Del: integrated -- https://master.php.net/note/delete/80844/integrated
Del: useless -- https://master.php.net/note/delete/80844/useless
Del: bad code -- https://master.php.net/note/delete/80844/bad+code
Del: spam -- https://master.php.net/note/delete/80844/spam
Del: non-english -- https://master.php.net/note/delete/80844/non-english
Del: in docs -- https://master.php.net/note/delete/80844/in+docs
Del: other reasons-- https://master.php.net/note/delete/80844
Reject -- https://master.php.net/note/reject/80844
Search -- https://master.php.net/manage/user-notes.php