### Thread: computing d:h:m:s difference between two dates

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2000
Posts
3
Rep Power
0
There seem to be alot of ways to perform date subtraction but one way that I do not see in the documentation or posts is this:

I have a date in the future (TIMESTAMP1). I want to find out how many days,hours,minutes,seconds between NOW() and TIMESTAMP1.

All of the functions I have seen deal with either the difference in seconds, or formating a new date from the calculation. I am trying to get "countdown" information I guess you could call it, that says "x days, y hours, z minutes until TIMESTAMP.

If I have missed something obvious, my apologies, but any help would be greatly appreciated.

Thanks,
kp

2. No Profile Picture
Apprentice Deity
Devshed Loyal (3000 - 3499 posts)

Join Date
Jul 1999
Location
Niagara Falls (On the wrong side of the gorge)
Posts
3,237
Rep Power
22
The best idea I could come up with is to convert to a unix timestamp to get a difference in seconds. You would have to convert to ddhhmmss format from that.

select unix_timestamp(date_field)-unix_timestamp() as sec_diff from table;
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2000
Posts
3
Rep Power
0
Okay, that is something that I had considered but what is the best way to do the conversion to d:m:h:s afterwards?
4. No Profile Picture
Apprentice Deity
Devshed Loyal (3000 - 3499 posts)

Join Date
Jul 1999
Location
Niagara Falls (On the wrong side of the gorge)
Posts
3,237
Rep Power
22
\$days=intval(\$timediff/86400);
\$remain=\$timediff%86400;
\$hours=intval(\$remain/3600);
\$remain=\$remain%3600;
\$mins=intval(\$remain/60);
\$secs=\$remain%60;

print "\$days:\$hours:\$mins:\$secs";