note 81529 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 04:02:51 +0000 |
| Subject: | note 81529 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142106@lists.php.net to get a copy of this message | ||
Just an correction to Jeff Stevenson's function of 2008-02-02.
That code as is didn't really work, but the right idea was there.. (e.g., the $days parameter
wasn't really used, and the sort didn't save) There were a few typos and minor other
things, try the code below instead...
<?php
function getDaysBetween($startDate, $endDate, $days)
{
$days = explode(',', $days);
$dates = array();
foreach($days as $day)
{
$newDate = $startDate;
switch ($day){
case 'Su':
case 'Sun':
$day = 'Sun';
break;
case 'M':
case 'Mon':
$day = 'Mon';
break;
case 'T':
case 'Tu':
case 'Tue':
$day = 'Tue';
break;
case 'W':
case 'Wed':
$day = 'Wed';
break;
case 'Th':
case 'Thu':
case 'Thur':
$day = 'Thu';
break;
case 'F';
case 'Fri';
$day = 'Fri';
break;
case 'S':
case 'Sat':
$day = 'Sat';
break;
default:
continue 2;
}
$curtime = strtotime("next $day", strtotime($startDate));
$endtime = strtotime("+1 day ", strtotime($endDate));
while($curtime <= $endtime)
{
$dates[] = date('Y-m-d', $curtime);
$curtime=strtotime("next 2 $day", $curtime);
}
}
$dates=array_unique($dates);
sort($dates);
return $dates;
}
?>
[EDIT by danbrown AT php DOT net: Applied user-provided fix to this code.]
--was--
Just an correction to Jeff Stevenson's function of 2008-02-02.
That code as is didn't really work, but the right idea was there.. (e.g., the $days parameter
wasn't really used, and the sort didn't save) There were a few typos and minor other
things, try the code below instead...
function getDaysBetween($startDate, $endDate, $days)
{
$days = explode(',', $days);
$dates = array();
foreach($days as $day)
{
$newDate = $startDate;
switch ($day){
case 'Su':
case 'Sun':
$day = 'Sun';
break;
case 'M':
case 'Mon':
$day = 'Mon';
break;
case 'T':
case 'Tu':
case 'Tue':
$day = 'Tue';
break;
case 'W':
case 'Wed':
$day = 'Wed';
break;
case 'Th':
case 'Thu':
case 'Thur':
$day = 'Thu';
break;
case 'F';
case 'Fri';
$day = 'Fri';
break;
case 'S':
case 'Sat':
$day = 'Sat';
break;
default:
continue 2;
}
$curtime = strtotime("next $day", strtotime($startDate));
$endtime = strtotime($endDate);
while($curtime <= $endtime)
{
$dates[] = date('Y-m-d', $curtime);
$curtime=strtotime("next 2 $day", $curtime);
}
}
$dates=array_unique($dates);
sort($dates);
return $dates;
}
http://php.net/manual/en/function.strtotime.php