note 74766 added to function.time

From: Date: Thu, 26 Apr 2007 16:58:10 +0000
Subject: note 74766 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-125895@lists.php.net to get a copy of this message
Here you go. You can specify how much you wanna see -- years, weeks, days, hours, minutes or seconds. Returns an Array containing a String along with the individual time values. Usage: array calc_tl( int $unixTime, [int $unixTime], [char $selector] ) <?php function calc_tl($t, $sT = 0, $sel = 'Y') { $sY = 31536000; $sW = 604800; $sD = 86400; $sH = 3600; $sM = 60; if($sT) { $t = ($sT - $t); } if($t <= 0) { $t = 0; } $bs[1] = ('1'^'9'); /* Backspace */ switch(strtolower($sel)) { case 'y': $y = ((int)($t / $sY)); $t = ($t - ($y * $sY)); $r['string'] .= "{$y} years{$bs[$y]} "; $r['years'] = $y; case 'w': $w = ((int)($t / $sW)); $t = ($t - ($w * $sW)); $r['string'] .= "{$w} weeks{$bs[$w]} "; $r['weeks'] = $w; case 'd': $d = ((int)($t / $sD)); $t = ($t - ($d * $sD)); $r['string'] .= "{$d} days{$bs[$d]} "; $r['days'] = $d; case 'h': $h = ((int)($t / $sH)); $t = ($t - ($h * $sH)); $r['string'] .= "{$h} hours{$bs[$h]} "; $r['hours'] = $h; case 'm': $m = ((int)($t / $sM)); $t = ($t - ($m * $sM)); $r['string'] .= "{$m} minutes{$bs[$m]} "; $r['minutes'] = $m; case 's': $s = $t; $r['string'] .= "{$s} seconds{$bs[$s]} "; $r['seconds'] = $s; break; default: return calc_tl($t); break; } return $r; } // A few exaggerated examples: $startTime = time(); $stopTime = mktime(23,59,59,12,31,2011); $tY = calc_tl($startTime, $stopTime, 'Y'); // Years (default) $tD = calc_tl($startTime, $stopTime, 'D'); // Days $tH = calc_tl($startTime, $stopTime, 'H'); // Hours print_r($tY); print_r($tD); print_r($tH); ?> OUTPUT Array ( [string] => 4 years 35 weeks 6 days 4 hours 54 minutes 33 seconds [years] => 4 [weeks] => 35 [days] => 6 [hours] => 4 [minutes] => 54 [seconds] => 33 ) Array ( [string] => 1711 days 4 hours 54 minutes 33 seconds [days] => 1711 [hours] => 4 [minutes] => 54 [seconds] => 33 ) Array ( [string] => 41068 hours 54 minutes 33 seconds [hours] => 41068 [minutes] => 54 [seconds] => 33 ) ---- Server IP: 64.71.164.2 Probable Submitter: 71.128.119.221 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/74766 Del: integrated -- https://master.php.net/note/delete/74766/integrated Del: useless -- https://master.php.net/note/delete/74766/useless Del: bad code -- https://master.php.net/note/delete/74766/bad+code Del: spam -- https://master.php.net/note/delete/74766/spam Del: non-english -- https://master.php.net/note/delete/74766/non-english Del: in docs -- https://master.php.net/note/delete/74766/in+docs Del: other reasons-- https://master.php.net/note/delete/74766 Reject -- https://master.php.net/note/reject/74766 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125895) next »