Time difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Time difference

Post by nhan »

i have two time values that comes from the database in this format (10:17:45 PM) how can i find its difference...? i have searched the forums and it gave me 2838 results... can someone provide me the link or the solution maybe.. thanks in advance....
User avatar
bmcewan
Forum Commoner
Posts: 55
Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.

Post by bmcewan »

You might want to have a look at strtotime();


A very rough example, someone may come up with a cleaner solution.

Code: Select all

$time1 = strtotime($_POST['time1']);
$time2 = strtotime($_POST['time2']);

if ($time1 > $time2) { // avoid having a negative timestamp
    $time_diff = $time1 - $time2;
} else {
    $time_diff = $time2 - $time1;
}

$time_diff_text = strftime("%H:%M:%S, $time_diff);
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

thanks so much, i have done the script it gave me an idea, though the answer is somewhat confusing, time1=10:00:00 PM and Time2=11:45:20:PM, the answer gave me 9:45:20 PM which is confusing indeed.. anyways.. ill just manipulate the script.. thank you so much!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

That's a whole lot of work for what one mysql function can do ;) TIMEDIFF()
Post Reply