note 28844 added to function.strtotime
| From: | geoff at hoffmans dot net | Date: | Sat, 25 Jan 2003 18:11:57 +0000 |
| Subject: | note 28844 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42738@lists.php.net to get a copy of this message | ||
I needed a function to generate an array of mysql-formatted dates in-between two user-seleceded
dates.
Requires the use of both strtotime() and date().
Here's the function I wrote:
function dateRange($from, $to){
// $from and $to are mysql date strings like '2003-01-25', '2003-02-01'
$dates = array();
$fromDate = strtotime($from);
$toDate = strtotime($to);
$daysInclusive = ($toDate - $fromDate)/60/60/24;
// this is inclusive. If you need exclusive use 1 and <
for($daynum=0;$daynum<=$daysInclusive;$daynum++){
array_push($dates, date("Y-m-d", $fromDate+($daynum*60*60*24)));
}
return $dates;
}
Called like so:
$datearray = dateRange($_POST["start_date"], $_POST["end_date"]);
Hope it saves you a few minutes one day ;)
--
http://www.php.net/manual/en/function.strtotime.php
http://master.php.net/manage/user-notes.php?action=edit+28844
http://master.php.net/manage/user-notes.php?action=delete+28844
http://master.php.net/manage/user-notes.php?action=reject+28844