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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    71
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Add an hour to date

    Hello there,

    I've been trying to work with the date function to add an hour to the current time.

    Here is what I have right now:

    PHP Code:
    $new_time date("m/d/y h:i a",strtotime("m/d/y h:i a + 1 hour")); 
    This sorta works, but it give the date as

    Code:
    12/31/69 4:00 pm
    It got the time of 4:00 pm correct, but it is definitely not the new years of 1969. Where did I go wrong?

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Mmm have you looked at the docs? You are not supplying strtotime() with a valid time so it is defaulting to "" - the beginning of time!

    There are plenty of examples on the docs page.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Try this:

    PHP Code:
    $new_time date("m/d/y h:i a",(time()+60*60)); 
    This way you generate $new_time on the basis of the current timestamp and add the amount of seconds in an hour (60 seconds * 60 minutes).

    But agreed with Andrews comment, check out the PHP documentation, possibly PHPs time() explanation.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    (time()+60*60) == strtotime( '+ 1 hour' )
    PHP Code:
    $new_time date"m/d/y h:i a"strtotime"+ 1 hour" ) ); 

  • #5
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by kbluhm View Post
    (time()+60*60) == strtotime( '+ 1 hour' )
    PHP Code:
    $new_time date"m/d/y h:i a"strtotime"+ 1 hour" ) ); 
    That's cool, your way requires less thinking


  •  

    Posting Permissions

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