note 43898 deleted from function.date by aidan

From: Date: Thu, 08 Jul 2004 00:33:41 +0000
Subject: note 43898 deleted from function.date by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72847@lists.php.net to get a copy of this message
Note Submitter: joe ---- From all my reading in this section of the site, it amazes me how complex everyone seems to be making their date calculation functions! For most date operations the time (cpu cycles? definately programmer time anyway...) it takes to parse a date string into a unix timestamp using the strtotime() function is minimal compared to the time it takes to do it any other way: <?php // Returns number of seconds between low_date and high_date function date_diff_seconds ( $low_date, $high_date ) { return strtotime($high_date)-strtotime($low_date); } // Returns number of minutes between low_date and high_date function date_diff_minutes ( $low_date, $high_date ) { return floor(date_diff_seconds($low_date,$high_date)/60); // 60 seconds per minute } // Returns number of hours between low_date and high_date function date_diff_hours ( $low_date, $high_date ) { return floor(date_diff_seconds($low_date,$high_date)/3600); // 3600 seconds per hour } // Returns number of hours between low_date and high_date function date_diff_days ( $low_date, $high_date ) { return floor(date_diff_seconds($low_date,$high_date)/86400); // 86400 seconds per day } $now=date("Y-m-d H:i:s"); $then=date("Y-m-d H:i:s",time()-(60*60*24*365)); // 1 year ago echo "Difference between '$now' and '$then' is:\n"; echo date_diff_seconds($then,$now) . " seconds, or "; echo date_diff_minutes($then,$now) . " minutes, or "; echo date_diff_hours($then,$now) . " hours, or "; echo date_diff_days($then,$now) . " days.\n"; ?> Output: Difference between '2004-07-07 15:40:56' and '2003-07-08 15:40:56' is: 31536000 seconds, or 525600 minutes, or 8760 hours, or 365 days.

« previous php.notes (#72847) next »