Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    68
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Adding time to a date and time?

    Hello

    I am trying to add 3 hours to a date and time, but when I do I get 3 hours and 12 minutes?

    Can anyone explain what I am doing wrong?

    PHP Code:
    <?

    $start_time 
    "2012-12-11 12:00:00";
    $hours_required "+3 hours";
    $finish_calculation date('Y-m-d h:m:s'strtotime($start_time "+" $hours_required));

    echo 
    "<br>" $start_time;
    echo 
    "<br>" $hours_required;
    echo 
    "<br>" $finish_calculation;

    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,180
    Thanks
    23
    Thanked 603 Times in 602 Posts
    I have always found working with date time a pain. PHP works in seconds not hours, months, or days. Php calculates the time in seconds from the epochs, January 1 1970 00:00:00 GMT, and goes from there.

    Your answer, but you should look up why this works:
    PHP Code:
    <?php
    $start_time 
    = new DateTime('2012-12-11 12:00:00');
    echo 
    $start_time->format('Y-m-d H:i:s') . "<br />";

    $finish_calculation $start_time->add(new DateInterval('PT3H'));
    echo 
    $finish_calculation->format('Y-m-d H:i:s') . "<br />";
    ?>
    P.S. FYI you should always start php with <?php and not <?. The later has been discontinued and it will eventually bite you.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    http://www.php.net/date

    Lowercase m is the numeric month with leading zeros (12 == December)

    Replace the lowercase m with a lowercase i... which is minutes with leading zeros.

    strtotime() also takes a second argument, the start time, which always defaults to the current timestamp... so this will also work:
    PHP Code:
    $start_time "2012-12-11 12:00:00"
    $hours_required "+3 hours"

    $finish_calculation date'Y-m-d h:i:s'strtotime(
        
    $hours_required,
        
    strtotime$start_time )
    ) ); 
    Last edited by kbluhm; 12-14-2012 at 03:18 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Datetime ftw (5.3+):
    PHP Code:
    $dt = new DateTime($start_time);
    $dt->add(new DateInterval('PT3H'));
    print 
    $dt->format('Y-m-d h:i:s'); 
    As for PHP time being an integer, I'm not sure how many languages *don't* use integers to store time (including things like filesystem inodes). This is part of the issue with the year 2038 time problem. Of course by 2038 this won't be an issue as x86 will be a thing of the past.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •