Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > How to add one week to a given date?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-07, 07:24
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
How to add one week to a given date?

Hi,

the following for loop will give the date for the next few weeks from todays date.

Code:
for ( $counter = 0; $counter < $recurrent; $counter += 1) { $day = date('Y-m-d', strtotime("+ $counter weeks")); }

But I need it to give the date for the next few weeks from any given date. I've tried:

Code:
for ( $counter = 0; $counter < $recurrent; $counter += 1) { $date = $_POST['date'] $day = date('$date', strtotime("+ $counter weeks")); }
where $date is a date variable in the form yyyy-mm-dd but this doesn't seem to work.

Could someone give me a hand.

Cheers
Reply With Quote
  #2 (permalink)  
Old 09-19-07, 07:46
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,144
This should help
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #3 (permalink)  
Old 09-19-07, 08:32
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,460
...or you could just add the relevant number of seconds to the desired date variable

so for one week thats 60 * 60 * 24 * 7. ie seconds * minutes * hours * days
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #4 (permalink)  
Old 09-19-07, 12:44
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
The following calculates the epoch time each loop and adds the number of days onto it. Thankfully mktime works with date addition, so (when counter = 4) day 19 + (7*4) = 47, is added on to the time.
Code:
<?php $recurrent = 4; for ( $counter = 0; $counter < $recurrent; $counter += 1) { date_default_timezone_set('GMT'); $day = mktime(0,0,0,date('m'),date('d')+(7*$counter),date('Y')); echo date('Y-m-d',$day)."<br>"; } ?>
Reply With Quote
  #5 (permalink)  
Old 09-19-07, 12:49
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Basically you need to separate out your variables.
Code:
$date = explode('-',$_POST['date']); $day = $date[2]; $month = $date[1]; $year = $date[0]; $recurrent = 4; for ( $counter = 0; $counter < $recurrent; $counter += 1) { date_default_timezone_set('GMT'); echo date('Y-m-d',mktime(0,0,0,$month,$day+(7*$counter),$year)); echo "<br/>"; }

Last edited by aschk : 09-19-07 at 12:52.
Reply With Quote
  #6 (permalink)  
Old 09-19-07, 12:56
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Just to clarify for you why you second attempt wasnt working :

Assuming that the passed variable $_POST['date'] is in the right format (i.e. yyyy-mm-dd).
$date = $_POST['date'];

You were passing it to date
date($date,....))

However the first parameter of the date function is the definition of what you want date to return e.g. 'D F y : O' , so giving it '12-05-2005' is moot because it won't understand that format. If you are uncertain about using the date function see the manual (http://uk2.php.net/manual/en/function.date.php).
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On