Bob's version:
$query = "SELECT MAX( ProcessingDate ) FROM processinglog WHERE ProcessingTypeCode = 'D' ";
$i = 0;
$result = mysql_query( $query );
$entrydate = date('Y' ).'-'.date( 'm' ).'-'.date( 'd' );
$duedate = new DateTime( $entrydate );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$tstamp = mktime( 0, 0, 0, $dm, $dd, $dy);
while ( $row = mysql_fetch_array( $result, MYSQL_NUM ) )
{
$lastdateprocessed = $row[ 0 ];
$duedate = new DateTime( $lastdateprocessed );
$duedate -> modify( '+1 days' );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$processdate = $dy.'-'.$dm.'-'.$dd;
$i++;
}
if ($i == 0 ){
$duedate = new DateTime( $entrydate );
$duedate -> modify( '-1 days' );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$processdate = $dy.'-'.$dm.'-'.$dd;
}
// preform daily processing if the last day processed is less than the previous day
// loop for processdate = processdate to yesterday
$duedate = new DateTime( $processdate );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$pstamp = mktime( 0, 0, 0, $dm, $dd, $dy);
while ( $pstamp < $tstamp )
{
$duedate = new DateTime( $processdate );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$iday = (int)$dd;
$imonth = (int)$dm;
$iyear = (int)$dy;
$processmonth = 100 * $iyear + $imonth;
// snip another 1500 lines
My version:
$tstamp = time();
$query = "SELECT MAX(`ProcessingDate`) ProcessingDate FROM `processinglog` WHERE `ProcessingTypeCode` = 'D' ";
checkErr($res = $DB->Query($query, __FILE__, __LINE__));
$row = $DB->Fetch($res);
if($row === FALSE)
{
$process = new DateTime();
$process->setTimestamp($tstamp);
$process->modify('-1 days');
} else {
$process = new DateTime($row['ProcessingDate']);
$process->modify('+1 days');
}
$processdate = $process->format('Y-m-d');
$pstamp = $process->getTimestamp();
while($pstamp < $tstamp)
{
$processmonth = $process->format('Ym');
le sigh...
[update]
So I said I'd elaborate, but I'm tired (I just scrubbed my kitchen floor), so here's a rough overview
$entrydate = date('Y' ).'-'.date( 'm' ).'-'.date( 'd' );
$duedate = new DateTime( $entrydate );
$dd = date_format( $duedate, 'd' );
$dm = date_format( $duedate, 'm' );
$dy = date_format( $duedate, 'Y' );
$tstamp = mktime( 0, 0, 0, $dm, $dd, $dy);
You may look at that and think "Ok, that looks reasonable". But all it actually did was
$tstamp = time();
.....and that's it. But no, Bob decided to do it the hard way. Even most bad PHP developers I know would have at LEAST done date('Y-m-d' ). Then he takes it a step further and instantiates a DateTime object and another 3 strings. Slow to the max, especially on the crappy machines my office uses as "servers" (and I don't mean him doing it once slows down the site, that's negligible, but he does it thousands of times accross many thousands of files).