note 97673 deleted from function.checkdate by danbrown
| From: | danbrown@php.net | Date: | Sun, 02 May 2010 22:40:15 +0000 |
| Subject: | note 97673 deleted from function.checkdate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168872@lists.php.net to get a copy of this message | ||
Note Submitter: tom at shrimponthebarbie dot co dot uk
----
Having found other examples of creating an array of MySql formmated dates, I created my own using
checkdate(). The other mktime() based solutions were time zone dependant, and included hacks to get
around this. Dates is dates and timestamps is timestamps in my opinion. Anyway, you might like to
try this.
Simply returns an array of MySql formatted dates in a range.
function createDateRangeArray( $fromDate, $toDate) // YYYY-MM-DD, YYYY-MM-DD
{
$fromDate = (string) $fromDate;
$toDate = (string) $toDate;
$splitFrom = explode ( '-', $fromDate );
$splitTo = explode ( '-', $toDate );
if
(
$fromDate <= $toDate
&& sizeof( $splitFrom ) == 3
&& sizeof( $splitTo ) == 3
&& checkdate( (int)$splitFrom[1], (int)$splitFrom[2], (int)$splitFrom[0] )
&& checkdate( (int)$splitTo[1], (int)$splitTo[2], (int)$splitTo[0] )
)
{
$dateRange = (array) $fromDate;
$splitDate = $splitFrom;
while ( $dateRange[ sizeof($dateRange)-1 ] != $toDate )
{
if ( ! checkdate( $splitDate[1], ++$splitDate[2], $splitDate[0] ) )
{
if ( ! checkdate( (int)++$splitDate[1], (int)$splitDate[2]=1, (int)$splitDate[0]
) )
{
$splitDate[2] = $splitDate[1] = 1;
$splitDate[0]++;
}
}
$dateRange[] = str_pad($splitDate[0], 4, "0", STR_PAD_LEFT)
.'-'. str_pad($splitDate[1], 2, "0", STR_PAD_LEFT) .'-'.
str_pad($splitDate[2], 2, "0", STR_PAD_LEFT);
}
}
else
{
$dateRange = array();
}
return $dateRange;
}