List Info

Thread: note 78025 added to ref.datetime




note 78025 added to ref.datetime
user name
2007-09-24 15:25:45
I wrote a simple script to format a duration in seconds.
Give the function some value in seconds and it will return
an array.

<?php

function format_duration($seconds) {

	$periods = array(
		'centuries' => 3155692600,
		'decades' => 315569260,
		'years' => 31556926,
		'months' => 2629743,
		'weeks' => 604800,
		'days' => 86400,
		'hours' => 3600,
		'minutes' => 60,
		'seconds' => 1
	);

	$durations = array();

	foreach ($periods as $period => $seconds_in_period) {
		if ($seconds >= $seconds_in_period) {
			$durations[$period] = floor($seconds /
$seconds_in_period);
			$seconds -= $durations[$period] * $seconds_in_period;
		}
	}
	
	return $durations;

}

echo format_duration(864);

/*
[minutes] => 14
[seconds] => 24
*/

echo format_duration(3600);

/*
[hours] => 1
*/

echo format_duration(11111111);

/*
[months] => 4
[days] => 6
[hours] => 20
[minutes] => 28
[seconds] => 59
*/

?>
----
Server IP: 69.147.83.197
Probable Submitter: 75.153.221.123
----
Manual Page -- http://
www.php.net/manual/en/ref.datetime.php
Edit        -- https://master
.php.net/note/edit/78025
Del: integrated  -- h
ttps://master.php.net/note/delete/78025/integrated
Del: useless     -- http
s://master.php.net/note/delete/78025/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78025/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78025/spam
Del: non-english -- 
https://master.php.net/note/delete/78025/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78025/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78025
Reject      -- https://mast
er.php.net/note/reject/78025
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )