Formatting bigint into weeks, days and hours

Hi guys. I hate to post this really nooby question here but here it is.
I’m trying to format a number into weeks, days and hours to display total run time in a dynamic signature but I’m not sure how to do it.
I have only been using php for 2 days and Java is more my forte.

I wrote this.

[php]
function formatTime($time) {
$seconds = $time / 1000;
$minutes = $seconds / 60;
$seconds %= 60;
$h = ($minutes / 60);
$minutes %= 60;
$d = ($h / 24);
$h %= 24;
$w = ($d / 7);
$d %= 7;
$s = $w == 1 ? “” : “s”;
$s1 = $d == 1 ? “” : “s”;
$s2 = $h == 1 ? “” : “s”;
return $w . " Week" . $s . “, " . $d . " Day” . $s1 . " and " . $h . " Hour" . $s2 . “.”;
}
[/php]

It doesn’t work though. I think I’m treating php too much like Java and haven’t understood something somewhere. Can anyone shed any light on where I’m going wrong?

What is the value of $time? I’m confused why you need to convert it to seconds

$time is a value stored to milliseconds but I’m going to convert it to seconds before I store it in the database in future.

Can you post a sample value?

There’s a couple ways to do this but here’s an example:

[php]
$weeks = intval($seconds / 604800); // 60 * 60 * 24 * 7
$days = intval($seconds / 86400 % 7); // 60 * 60 * 24
$hours = intval($seconds / 3600 % 24); // 60 * 60
[/php]

or just use date(), it has all those things built in.

How do you mean? Unless I misunderstood the question I’m pretty sure date() can’t do anything like this heh

If all he wants is the month, day, year, and so on, then date() will give that to him, something like date(‘y-m-d’, strtotime($time)); would print out 2012-11-24. i use date(“F d, Y”, $postdate)." at ". date(“g:i a”, $postdate), which prints out October 22, 2012 at 11:00 pm. My dates are already in a unix format, but all he needs to use is strototime() to change from milliseconds to a standard format. Something like date(‘h:i:s a’, strtotime($r[‘date’])) will give him hours:minutes:seconds am/pm.

I don’t think he means seconds as a timestamp but rather seconds elapsed. For example

[php]time() - ((604800 * 2) + (86400 * 2) + (3600 * 2))[/php]

Would be 2 weeks, 2 days, 2 hours

idk, it sounded to me like he already had the time stored in ms, he just needed a way of formatting it, which date will do.

Sponsor our Newsletter | Privacy Policy | Terms of Service