Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 11:52 PM   PM User | #1
blackrain87
New Coder

 
Join Date: Jun 2010
Posts: 71
Thanks: 14
Thanked 0 Times in 0 Posts
blackrain87 is an unknown quantity at this point
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?
blackrain87 is offline   Reply With Quote
Old 01-28-2013, 12:23 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-28-2013, 08:36 PM   PM User | #3
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
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.
Thyrosis is offline   Reply With Quote
Old 01-28-2013, 08:40 PM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
(time()+60*60) == strtotime( '+ 1 hour' )
PHP Code:
$new_time date"m/d/y h:i a"strtotime"+ 1 hour" ) ); 
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 01-28-2013, 08:49 PM   PM User | #5
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
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
Thyrosis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 AM.


Advertisement
Log in to turn off these ads.