Jump to content
Updating IPB tonight Read more... ×

Archived

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

sun14php

Time Diff In Minutes

Recommended Posts

i am using two time colums to store 2 time values using currtime() mysql function.
now i want to store the diff. between above two time filed into [b]minutes[/b] in a another field like

PASTTIME NOWTIMKE DIFFRENCE
05:10:00 05:20:00 10


how can i achive above result
guide me please.

Share this post


Link to post
Share on other sites
[!--quoteo(post=389233:date=Jun 29 2006, 08:47 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 08:47 AM) [snapback]389233[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i am using two time colums to store 2 time values using currtime() mysql function.
now i want to store the diff. between above two time filed into [b]minutes[/b] in a another field like

PASTTIME NOWTIMKE DIFFRENCE
05:10:00 05:20:00 10
how can i achive above result
guide me please.
[/quote]

i would run something like this:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] tableName SET diffrence [color=orange]=[/color] MINUTE(TIMEDIFF(PASTTIME, NOWTIMKE)); [!--sql2--][/div][!--sql3--]

obviously, this will set ALL your rows, so you could add a WHERE clause that would update only one row of your choice.

also, keep in mind that the mysql function MINUTE() will only return an int between 0 and 59, so if you are ever going to have a difference of more than an hour, you may want to rethink how you handle them.

Share this post


Link to post
Share on other sites
you can explode the time into hours/min/sec like this

$time1 = '05:10:00';
$time2 = '05:20:00';

$starttime = explode(':', $time1);
$endtime = explode(':', $time2);

$difference = ($endtime[0] - $starttime[0]) * 60 + ($endtime[1] - $starttime[1]);

echo $difference;

Did you consider the case that the result maybe wrong if the 2 time was taken more than 24 hours apart?

Share this post


Link to post
Share on other sites

×

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.