Jump to content

[SOLVED] Time format help


bachx

Recommended Posts

I have a a UNIX format number that I want to convert into an Hours:Minutes:Seconds format for a stopwatch/countdown timer script I'm writing. I used date("H:i:s", ($time)), but the problem is, if the period I'm counting is above 24 hours, the Hours reset to 0. So If I was counting down 49 Hours, It's be displayed as 01:00:00 instead of 49:00:00. Any help?

Link to comment
https://forums.phpfreaks.com/topic/68284-solved-time-format-help/
Share on other sites

<?php
	$t = htmlentities($_GET['t']);
	$time['days'] = (int) ($t / (60 * 60 * 24));
	$t = $t % (60 * 60 * 24);
	$time['hours'] = (int) ($t / (60 * 60));
	$t = $t % (60 * 60);
	$time['minutes'] = (int) ($t / 60);
	$time['seconds'] = $t % 60;

	// output style 1
	foreach ($time as $key => $value) {
		if (!empty($value)) {
			echo "$value $key ";
		}
	}
	// output style 1 end

	echo '<br />';

	// output style 2
	$time['hours'] += $time['days'] * 24;

	foreach ($time as $key => $value) {
		if (strlen($time[$key]) == 1) {
			$time[$key] = '0' . $value;
		}
	}
	echo $time['hours'] . ':' . $time['minutes'] . ':' . $time['seconds'];
	// output style 2 end
?>

 

http://sneamia.net/junk/bachx/?t=100

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.