Forum Moderators: coopster

Message Too Old, No Replies

Find the number of days, hours and minutes between 2 dates?

         

one_mind

9:02 am on Jul 24, 2006 (gmt 0)

10+ Year Member



Hi,

I have a datetime field in a mysql database and i want to output how many days, hours and minutes are left between the current date and the future date.

So far i just convert the datettime to a timestamp like so: $start = strtotime($began);

Where $began is the current time, basically i just need to know how to convert a timestamp to days, hours ect If i was to minus the current timestamp from the future timestamp.

Any help would be greatly appreciated.

Thanks

one_mind

9:43 am on Jul 24, 2006 (gmt 0)

10+ Year Member



Its ok, i figured it out.

$diff = $ends-$now;

$days = intval($diff/24/60/60);
$remain=$diff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60);

:)

siMKin

11:55 am on Jul 24, 2006 (gmt 0)

10+ Year Member



i don't think intval is really necessary here. if you're afraid that any of the variables is not numeric you should do a check on that and generate an error when they are not. That's much better imho then silently ignoring it and outputting unusefull information.

btw, if you ever want to include months and years, i have a function for that on my website (see profile)

incywincy

12:37 pm on Jul 24, 2006 (gmt 0)

10+ Year Member



if you're using mysql use the time functions to do your math. these are robust, well tested functions that will work and will save you a lot of development and test time. you can even use mysql functions to format your output.

one_mind

6:04 pm on Jul 24, 2006 (gmt 0)

10+ Year Member



I used the intval function to get rid of the decimals after the number like so: 2.324234234 to 2

My way works fine