Adiing time (10:56:45+01:12:52)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumaabey
    New Member
    • Jun 2007
    • 29

    Adiing time (10:56:45+01:12:52)

    how to add time ,the format of the time is
    (00:45:55+01:22 :01)
  • Jeigh
    New Member
    • Jul 2007
    • 73

    #2
    Not sure what you're asking exactly but you can use:

    [code=php]
    $time = date('Y-m-d H:i:s', time());
    echo $time;
    [/code]

    This will display somthing like 2007-08-20 09:27:05

    Edit:

    Or are you asking how to add time together, as in 2:00 + 5:00 = 7:00, except in the format you posted? (I think this is what you're asking)

    I'm not sure how to do this, but at a guess maybe you could explode the time at the colon, then adding them together normally using PHP (just like using 2 + 3) Then display it using somthing like:

    $hours : $minutes : $seconds

    But my knowledge of PHP is very limited and I assume there is a much easier way of doing this.

    Comment

    • sumaabey
      New Member
      • Jun 2007
      • 29

      #3
      ok thanks.After exploding the time format ,how i will get the desire result?

      Comment

      • Jeigh
        New Member
        • Jul 2007
        • 73

        #4
        I'd do somthing like the following:

        [code=php]
        $time = explode(":", *time string here*);

        $hours = $time[0];
        $minutes = $time[1];
        $seconds = $time[2];

        $time2 = explode(":", * second time string here*);

        $hours2 = $time2[0];
        $minutes2 = $time2[1];
        $seconds2 = $time2[2];

        $new_hours = $hours + $hours2;
        $new_minutes = $minutes + $minutes2;
        $new_seconds = $seconds + $seconds2;

        echo "$new_hours:$ne w_minutes:$new_ seconds";
        [/code]

        As far as I can see that would work but I think I may have made a simple problem 10x harder than it needed to be, I tend to overcomplicate things sometimes, but I don't know what the correct way to do it would be :P

        Comment

        • Purple
          Recognized Expert Contributor
          • May 2007
          • 404

          #5
          Hi,

          can I suggest you take a read of the time() function in the php manual.

          This function returns a timestamp based on the Unix epoch - simply the number of seconds since January 1 1970 00:00:00. You can increment and decrement this by adding and subtracting the number of seconds to achieve you desired date and time.

          so if now is 10:56:45, you would add and then format the output using the date()

          [PHP]$newtime = time() + (01*60*60)+(12* 60)+52;
          echo date("F j, Y, g:i a",$newtime) ; [/PHP]

          as an aside, you could also do it using mktime() function

          Regards Purple
          Last edited by Purple; Aug 20 '07, 01:44 PM. Reason: typo

          Comment

          • ronnil
            Recognized Expert New Member
            • Jun 2007
            • 134

            #6
            instead of splitting the string, you could also use strtotime (or strftime) add the two unixtime values together and encode it again with date()

            personally i keep everything in unixtime until i have to echo it to the user.... makes it a lot easier to work with :)

            Comment

            • sumaabey
              New Member
              • Jun 2007
              • 29

              #7
              thanks the problem got solved

              Comment

              Working...