note 74766 deleted from function.time by aidan

From: Date: Wed, 15 Apr 2009 16:03:34 +0000
Subject: note 74766 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153327@lists.php.net to get a copy of this message
Note Submitter: jason at thinkingman dot org ---- 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 )

« previous php.notes (#153327) next »