Hello, I am having trouble finding out how to add 2 time values together. How can I add the following times together? $time1 = date('H:i:s'); $time2 = mysql_result($result,$i,"timeDestApt"); Can someone please advise how this can be done? Regards Central
You'd need to use something like www.php.net/strtotime or www.php.net/mktime to make them into UNIX timestamps, add them, then use www.php.net/date to see what a human-readable version would be. Jay
Hello, Still having trouble with this one.. My code is as follows. Can anyone tell me why the end result is not what it should be? I'm trying to add $time_to_dest to the current time to get one complete total time. $time_to_dest is extracted from a db field and is provided by a third party program. <?php date_default_timezone_set('Europe/London'); $time_now = date('H:i:s'); $time_to_dest = "03:00:00"; //extracted from a db field and provided by a third party program $time0 = $time_now; $time1 = strtotime($time0); $time2 = strtotime($time_to_dest); $time3 = $time1+$time2; $time4 = date('H:i:s', $time3); echo "Time Now = $time0 <br>"; echo "Coverted = $time1 <br>"; echo "Time to destination = $time_to_dest <br>"; echo "Converted = $time2 <br>"; echo "So both times are added together in their converted format - $time1 + $time2 = $time3 <br>"; echo "The result is then converted back to a readable output - <b>$time4</b>"; ?> Code (markup): I think there is something that i'm missing. Can anyone point out where i'm going wrong? Regards
This is what i'm trying to gain: time1 = 13:00:00 //time now + time2 = 03:00:00 //time to destination = time3 = 16:00:00 //required output Regards
I dont think it works because time2 = 03:00:00 //time to destination the time2 is being treated as 3.00AM midnight, not 3 hours. This works. $time_now = date('H:i:s'); $time_used_to_dest = "+ 3 hours"; //extracted from a db field and provided by a third party program $time_reach = strtotime("$time_now $time_used_to_dest"); PHP: Anyway, there might be any other better way to do it I am not sure.
Hello, The problem is that the db field is constantly being updated, and the program will only provide the time to destination in the H:i:s format. All I need to do is find some way of adding the time to dest to the current client time.
So if the variable $time2 is being read as 3:00 AM and not 3 hours how do I convert 03:00:00 into 3 hours?
If you're using MySQL then it may be easier to add the times from there and then put the result into PHP. Would be something like mysql -> SELECT ADDTIME('13:00:00', '03:00:00'); -> '16:00:00' EDIT: Sorry just re-read your post. Do you have access to query the database or has the data already been imported?
Hi Limotek, The data is inserted into a mysql db field automatically by a program that runs on the clients machine, it is updated every few seconds. Regards
Ah ok. My method won't work then as you need the current time as well which as far as I'm aware, you can't get via MySQL. What I would suggest is to split to $time_to_dest variable and then use the strtotime() function to get $time_reach. Try $time_now = date('H:i:s'); $new_time_to_dest = explode(":",$time_to_dest); $time_reach = strtotime("$time_now +$new_time_to_dest[0] hours $new_time_to_dest[1] minutes $new_time_to_dest[2] seconds"); PHP: Sorry, haven't had a chance to test but it should work. Let me know.
I've tried looking for a way to do that before and didn't have any joy. @central-internet: I would say you should do it sillysoft's way as it would be more efficient.