Jump to content

Durration to time


The Little Guy

Recommended Posts

This basic function turns seconds into a year, month, week, etc.

 

I have all of them working (I believe) except for $yr and $mo, do I have the math correct for those two?

 

function duration($len){
$yr = floor($len / 52 / 12 / 7 / 24 / 60 / 60);
$mo = floor($len / 12 / 7 / 24 / 60 / 60);
$wk = floor($len / 7 / 24 / 60 / 60);
$dy = floor($len / 24 / 60 / 60);
$hr = floor($len / 60 / 60);
$min = floor($len / 60);
$sec = floor($len % 60);
return array('yr' => $yr, 'mo' => $mo, 'wk' => $wk, 'dy' => $dy, 'hr' => $hr, 'min' => $min,'sec' => $sec);
}

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/
Share on other sites

52 / 12 / 7 / 24 / 60 / 60

 

Don't take this the wrong way, but what in the world are you doing?

 

 

First off, you're not factoring in leap year, but that's cool, so let's get down to the math.

 

 

Well, let's just assume 365 days, since months/weeks/so on would get weird.  (52*7 = 364...  12*30 = 360 of course... so....)

 

 

So....  We need to find the seconds in 365 days...  365 days * 24 hours * 60 minutes * 60 seconds = 31536000 seconds.

 

 

So, years = total seconds/31536000 seconds.  But, based on your floor() call, you want whole number years.

 

 

$yr = floor($len/31536000);

 

 

 

Now....  You want a running total, yes?  The easiest solution is essentially this:

 

total time = x;

years = floor(total time/seconds in a year);

total time -= years*seconds in a year;

months = floor(total time/seconds in a month);

total time -= months*seconds in a month;

hours = floor(total time/seconds in an hour);

total time -= hours*seconds in an hour;

minutes = floor(total time/seconds in a minute);

total time -= minutes*seconds in a minute;

seconds = total time;

 

 

 

You could of course use modulo instead of subtraction, but I think subtraction is probably quicker.

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/#findComment-759422
Share on other sites

OMG, I never thought of doing it that way!

 

Thanks for the help!

 

But to answer your question:

Don't take this the wrong way, but what in the world are you doing?

 

What I am doing is just displaying how long it has been since a document has been modified.

 

So, I am doing this:

 

function duration($len){
$yr = floor($len / 31536000);
$mo = floor($len / 2629743);
$wk = floor($len / 7 / 24 / 60 / 60);
$dy = floor($len / 24 / 60 / 60);
$hr = floor($len / 60 / 60);
$min = floor($len / 60);
$sec = floor($len % 60);
return array('yr' => $yr, 'mo' => $mo, 'wk' => $wk, 'dy' => $dy, 'hr' => $hr, 'min' => $min,'sec' => $sec);
}
$len = duration(strtotime('2009-02-10') - strtotime('2008-02-10'));
echo '<br>';
print_r($len);

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/#findComment-759447
Share on other sites

Ahhh, I didn't mean what are you doing with this code, I meant what was that block of code trying to do.

 

 

Specifically:  52 / 12 / 7 / 24 / 60 / 60

 

52 weeks, 12 months, 7 days, 24 hours, 60 minutes and 60 seconds in a year?

 

The math behind that didn't make sense.

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/#findComment-759449
Share on other sites

Ahhh, I didn't mean what are you doing with this code, I meant what was that block of code trying to do.

 

 

Specifically:  52 / 12 / 7 / 24 / 60 / 60

 

52 weeks, 12 months, 7 days, 24 hours, 60 minutes and 60 seconds in a year?

 

The math behind that didn't make sense.

 

yeah, and that was 7 days a week, but I don't think that is relative to finding any thing :)

 

I don't think I really need to calculate for leap year, due to the fact when the actual number of years will just show some things like this:

 

3 years ago

1 year ago

5 years ago

 

(I hope those dates don't occur) What do you think?  When it is close to 1 year of no change, I could just display "about 1 year ago"

 

 

Off Topic:

 

after doing this, I think you could calculate someones b-day off this...

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/#findComment-759453
Share on other sites

In the big scheme of things, leap years won't matter in this code, but if you needed it to be super accurate, you would have to factor in all kinds of things.

 

 

 

As for finding the birthday, it would be a weird situation since the input would be seconds since the person was born (or since the day of the month in which they were born).

Link to comment
https://forums.phpfreaks.com/topic/144691-durration-to-time/#findComment-759457
Share on other sites

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.