I have an app [iphone], that sends to a server some times [using json], so the times look like hh:mm 24 hour format, the time gets saved in the db as varchar,
I need to calculate the elapsed time = endTime - startTime
but my problem is that I have the time in the db as varchar!, no time stamp,
thanks a lot!
so how to calculate the elapsed time, with out changing the varchar type of field in my db?
You can cast it, but you'd be better off having that as a datetime to start with.
cast(endtime as datetime) - cast(starttime as datetime) -- yields an interval
Easy:
$start_time = '11:10';
$end_time = '18:55';
$start_time = explode(':', $start_time);
$end_time = explode(':', $end_time);
$elapsed_time = $end_time[0]*60+$end_time[1]-$start_time[0]*60-$start_time[1];
// in minutes.
$elapsed_hours = floor($elapsed_time/60);
$elapsed_minutes = $elapsed_time-$elapsed_hours*60;
print $elapsed_hours.':'.$elapsed_minutes;
// 7:45
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With