Jump to content


Photo

Time Diff In Minutes


  • Please log in to reply
2 replies to this topic

#1 sun14php

sun14php
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 29 June 2006 - 12:47 PM

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 minutes in a another field like

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


how can i achive above result
guide me please.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 12:56 PM

[!--quoteo(post=389233:date=Jun 29 2006, 08:47 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 08:47 AM) View Post[/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 minutes 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 = 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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 12:56 PM

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?


Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users