note 65626 deleted from ref.datetime by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 17:48:02 +0000 |
| Subject: | note 65626 deleted from ref.datetime by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133207@lists.php.net to get a copy of this message | ||
Note Submitter: gary at gmartellino dot com
----
here's a quick script i used to find recurring dates, it offers some flexibility in how you
want to iterate though the dates
<?php
// ----------------------------------------------------------
// Recurring dates
// Returns an array of recurring dates
// ----------------------------------------------------------
class recur {
var $endOption; // set to either "endBy" or "endAfter" -- endby if you want to
end by a date and
// end after if you want to end after 'x' amount of occurrences
var $endValue; // set to either a date in 'xxxx-xx-xx' format or a number of
occurrences
var $start; // set to the starting date in 'xxxx-xx-xx' format
function interval($type, $spread){
$startDate = explode("-", $this->start);
$time = mktime(0, 0, 0, $startDate[1], $startDate[2], $startDate[0]);
$dates[] = $this->start;
if($this->endOption == "endAfter"){
for($i = 1; $i < $this->endValue; $i++){
$futureTime = strtotime("+$spread $type", $time);
$dates[] = date("Y-m-d", $futureTime);
$time = $futureTime;
}
return $dates;
}else if($this->endOption == "endBy"){
$endDate = explode("-", $this->endValue);
$endTime = mktime(0, 0, 0, $endDate[1], $endDate[2], $endDate[0]);
while($endTime > $time){
$futureTime = strtotime("+$spread $type", $time);
if($futureTime > $endTime){
break;
}
$dates[] = date("Y-m-d", $futureTime);
$time = $futureTime;
}
return $dates;
}
}
}
?>
example:
<?php
include("includes/recur.class.php");
$recur = new recur();
$recur->endOption = "endBy";
$recur->endValue = '2006-12-09';
$recur->start = '2006-04-28';
print_r($recur->interval("day", 4));
?>