note 97673 added to function.checkdate
| From: | tom at shrimponthebarbie dot co dot uk | Date: | Sun, 02 May 2010 14:55:04 +0000 |
| Subject: | note 97673 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168855@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 92.48.74.199
Probable Submitter: 87.194.11.214
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/97673
Del: integrated -- https://master.php.net/note/delete/97673/integrated
Del: useless -- https://master.php.net/note/delete/97673/useless
Del: bad code -- https://master.php.net/note/delete/97673/bad+code
Del: spam -- https://master.php.net/note/delete/97673/spam
Del: non-english -- https://master.php.net/note/delete/97673/non-english
Del: in docs -- https://master.php.net/note/delete/97673/in+docs
Del: other reasons-- https://master.php.net/note/delete/97673
Reject -- https://master.php.net/note/reject/97673
Search -- https://master.php.net/manage/user-notes.php