note 102813 added to dateinterval.format

From: Date: Tue, 08 Mar 2011 06:22:07 +0000
Subject: note 102813 added to dateinterval.format
Groups: php.notes 
Request: Send a blank email to php-notes+get-177651@lists.php.net to get a copy of this message
This is a modification and re-post of kuzb posts http://www.php.net/manual/en/dateinterval.format.php#102271. Fix: to_seconds function to include minutes. Here is the complete post with modifications: 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->i * 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: 69.147.83.197 Probable Submitter: 60.234.253.13 ---- Manual Page -- http://www.php.net/manual/en/dateinterval.format.php Edit -- https://master.php.net/note/edit/102813 Del: integrated -- https://master.php.net/note/delete/102813/integrated Del: useless -- https://master.php.net/note/delete/102813/useless Del: bad code -- https://master.php.net/note/delete/102813/bad+code Del: spam -- https://master.php.net/note/delete/102813/spam Del: non-english -- https://master.php.net/note/delete/102813/non-english Del: in docs -- https://master.php.net/note/delete/102813/in+docs Del: other reasons-- https://master.php.net/note/delete/102813 Reject -- https://master.php.net/note/reject/102813 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177651) next »