Adding 2 time values together

Discussion in 'PHP' started by central-internet, Oct 12, 2008.

  1. #1
    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
     
    central-internet, Oct 12, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    jayshah, Oct 12, 2008 IP
  3. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Thanks Jayshah i'll give it a try.
     
    central-internet, Oct 12, 2008 IP
  4. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    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
     
    central-internet, Oct 15, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    what is the result you looking for??
     
    ads2help, Oct 15, 2008 IP
  6. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    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
     
    central-internet, Oct 15, 2008 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    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.
     
    ads2help, Oct 15, 2008 IP
  8. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    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.
     
    central-internet, Oct 15, 2008 IP
  9. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    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?
     
    central-internet, Oct 16, 2008 IP
  10. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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?
     
    Limotek, Oct 16, 2008 IP
  11. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    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
     
    central-internet, Oct 16, 2008 IP
  12. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.
     
    Limotek, Oct 16, 2008 IP
  13. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #13
    Am I missing something?

    mysql -> SELECT ADDTIME(TIME_FORMAT(NOW(),'%H:%i:%s'), '03:00:00');
     
    Sillysoft, Oct 16, 2008 IP
  14. central-internet

    central-internet Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #14
    OK

    I'll try both and let you know how I get on.

    Thanks in Advance
     
    central-internet, Oct 16, 2008 IP
  15. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    Limotek, Oct 17, 2008 IP