note 102271 added to dateinterval.format

From: Date: Sat, 05 Feb 2011 01:15:12 +0000
Subject: note 102271 added to dateinterval.format
Groups: php.notes 
Request: Send a blank email to php-notes+get-176698@lists.php.net to get a copy of this message
Quick class to allow you to input a time in any unit, and have it recalculate in to different denominations (for example, seconds to hours, minutes and seconds): <?php class DateIntervalEnhanced extends DateInterval { /* Keep in mind that a year is seen in this class as 365 days, and a month is seen as 30 days. It is not possible to calculate how many days are in a given year or month without a point of reference in time.*/ public function to_seconds() { return ($this->y * 365 * 24 * 60 * 60) + ($this->m * 30 * 24 * 60 * 60) + ($this->d * 24 * 60 * 60) + ($this->h * 60 *60) + $this->s; } public function recalculate() { $seconds = $this->to_seconds(); $this->y = floor($seconds/60/60/24/365); $seconds -= $this->y * 31536000; $this->m = floor($seconds/60/60/24/30); $seconds -= $this->m * 2592000; $this->d = floor($seconds/60/60/24); $seconds -= $this->d * 86400; $this->h = floor($seconds/60/60); $seconds -= $this->h * 3600; $this->i = floor($seconds/60); $seconds -= $this->i * 60; $this->s = $seconds; } } // Example usage $di = new DateIntervalEnhanced('PT3600S'); $di->recalculate(); // outputs 1:0:0 instead of 0:0:3600 now! echo $di->format('%H:%i:%s'); ?> ---- Server IP: 216.235.13.26 Probable Submitter: 64.180.62.209 ---- Manual Page -- http://www.php.net/manual/en/dateinterval.format.php Edit -- https://master.php.net/note/edit/102271 Del: integrated -- https://master.php.net/note/delete/102271/integrated Del: useless -- https://master.php.net/note/delete/102271/useless Del: bad code -- https://master.php.net/note/delete/102271/bad+code Del: spam -- https://master.php.net/note/delete/102271/spam Del: non-english -- https://master.php.net/note/delete/102271/non-english Del: in docs -- https://master.php.net/note/delete/102271/in+docs Del: other reasons-- https://master.php.net/note/delete/102271 Reject -- https://master.php.net/note/reject/102271 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176698) next »