note 97669 deleted from function.checkdate by mazzanet

From: Date: Sun, 02 May 2010 16:53:45 +0000
Subject: note 97669 deleted from function.checkdate by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168856@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. <?php function createDateRangeArray( $fromDate, $toDate) { $fromDate = (string) $fromDate; $toDate = (string) $toDate; $splitFrom = explode ( '-', $fromDate ); $splitTo = explode ( '-', $fromDate ); if ( $fromDate <= $toDate && checkdate( (int)$fromDate[1], (int)$fromDate[2], (int)$fromDate[0] ) && checkdate( (int)$toDate[1], (int)$toDate[2], (int)$toDate[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 = false; } return $dateRange; }

« previous php.notes (#168856) next »