note 82515 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:59:57 +0000 |
| Subject: | note 82515 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142101@lists.php.net to get a copy of this message | ||
I am having problems with the timhavens's <i>dates_betweeen_dates()</i> script not
returning proper output for calls like:
<?php
print_r(dates_between_dates('2008-04-14', '2008-04-14'));
print_r(dates_between_dates('2008-04-14', '2008-04-15'));
?>
Here is a function that returns the expected results:
<?php
/*
* dates_between_dates - Returns array of all dates between $date1 and $date2
*
* @param start_date Start-Date of range
* @param end_date End-Date of range
* @returns array dates
*/
function dates_between_dates($start_date, $end_date) {
// Standardize date formats
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
// if $end_date is later, swap the dates
if (strtotime($start_date) > strtotime($end_date)) {
$a = $start_date;
$start_date = $end_date;
$end_date = $a;
}
// initialize array with start date
$return_array = array($start_date);
// Initialize loop counter date
$the_date = $start_date;
// loop through days
while ($the_date != $end_date) {
$the_date = date('Y-m-d', strtotime($the_date."+1 day"));
$return_array[] = $the_date;
}
return $return_array;
}
?>
--was--
I am having problems with the timhavens's <i>dates_betweeen_dates()</i> script not
returning proper output for calls like:
<?
print_r(dates_between_dates('2008-04-14', '2008-04-14'));
print_r(dates_between_dates('2008-04-14', '2008-04-15'));
?>
Here is a function that returns the expected results:
<?
/*
* dates_between_dates - Returns array of all dates between $date1 and $date2
*
* @param start_date Start-Date of range
* @param end_date End-Date of range
* @returns array dates
*/
function dates_between_dates($start_date, $end_date) {
// Standardize date formats
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
// if $end_date is later, swap the dates
if (strtotime($start_date) > strtotime($end_date)) {
$a = $start_date;
$start_date = $end_date;
$end_date = $a;
}
// initialize array with start date
$return_array = array($start_date);
// Initialize loop counter date
$the_date = $start_date;
// loop through days
while ($the_date != $end_date) {
$the_date = date('Y-m-d', strtotime($the_date."+1 day"));
$return_array[] = $the_date;
}
return $return_array;
}
?>
http://php.net/manual/en/function.strtotime.php