Add 5 weeks to time stamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Add 5 weeks to time stamp

Post by Luke »

I am making a seminar schedule that can be added to and deleted from via entries in a database. One of the fields is the seminar start date in the form of a timestamp. I would like to be able to take that timestamp and automatically add 5 weeks plus 2 hours to it. Is that possible to do accurately?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could use php's strtotime() function to add the specified time.

ex:

Code: Select all

$date = "10/26/2005";
$newdate = date("m/d/Y",strtotime($date." +5 weeks"));
echo $newdate
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

What about just adding 2430000 seconds to the timestamp, which calculates to 5 weeks and 2 hours... is there any reason this would be inaccurate?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that should work too

although, not sure your math is right.

I come up with: 3031200 for 5 weeks and 2 hours.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Typically I like to keep as much stuff that can be done in MYSQL in MYSQL.

Code: Select all

"INSERT INTO MY TABLE(tmstamp) VALUES(ADD_TIME(NOW(),5 WEEKS))"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

One armed space goat wrote:What about just adding 2430000 seconds to the timestamp, which calculates to 5 weeks and 2 hours... is there any reason this would be inaccurate?
Only if you cross a DST boundary in that timescale, but generally no.

Using strtotime() makes far more sense in terms of code readability though ;)

EDIT | I missed the mysql stuff. Always let mysql do as much work as you can.... it's faster than php.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Burrito wrote:you could use php's strtotime() function to add the specified time.

ex:

Code: Select all

$date = "10/26/2005";
$newdate = date("m/d/Y",strtotime($date." +5 weeks"));
echo $newdate
This works for the weeks, but hours come out all funky. I need it to be plus 5 weeks and 2 hours.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$date = "10/26/2005 10:00:00"; 
$newdate = date("m/d/Y g:i a",strtotime($date." +5 weeks")); 
$newdate = date("m/d/Y g:i a",strtotime($newdate." + 2 hours"));
echo $newdate
8O
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

strtotime(date('m/d/Y'), '+5 weeks and 2 hours'));
;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11wtq wrote:

Code: Select all

strtotime(date(), '+5 weeks and 2 hours'));
;)
ya or that... 8)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

d11wtq wrote:

Code: Select all

strtotime(date('m/d/Y'), '+5 weeks and 2 hours'));
;)
That made this happen..
error wrote: Warning: date() [function.date]: Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in e:\Inetpub\wwwroot\classes.php on line 16
10/26/05 3:09 and PHP Warning: date() [function.date]: Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in e:\Inetpub\wwwroot\classes.php on line 16
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try removing the "and"

so just "5 weeks 2 hours"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry, date() shouldn't be there :oops:

Look at Burrito's first example and you'll figure it out :D
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I think the "and" is what is causing that error though.

I'm pretty sure the correct syntax for adding multiple "types" with strtotime is just a space separator...try what I just posted and see if it works.
Post Reply