Jump to content

Future Dates


kaust

Recommended Posts

I'm looking to store recurring dates in a database. The form will allow the end-user to select weekly and monthly. Basically, I'd like to get xDate for the 1st, 2nd, 3rd,etc yDay for  zMonths.

Brief example of Monthly output:
Thur 02-11-2006
Thur 07-12-2006
Thur 04-01-2006
*Dates for first Thursday of the next three months.

Brief example of Weekly output:
Thur 02-11-2006
Thur 09-11-2006
Thur 16-11-2006
*Dates for next three Thursdays

I found a script for finding weekly recurring dates and it works perfectly but no luck finding help on a month.
[code]$datestart = date('Y-m-d');  /* the starting date */
echo $datestart."<br />";
$rep = 11;  /* number of future (or past) dates to display */
$nod = 7; /* number of days in the future (or past) to increment
              (or decrement) the date */
$nom = 0;  /* number of months in the future (or past) to increment
              (or decrement) the date */
$noy = 0;  /* number of years in the future (or past) to increment
              (or decrement) the date */

future_date($datestart,$rep,$nod,$nom,$noy);

function future_date($datestart,$rep,$nod,$nom,$noy) {
  while ($rep >= 1) {
    $datyy=substr($datestart,0,4);
    $datmm=substr($datestart,5,2);
    $datdd=substr($datestart,8,2);
    $fda=$datdd + $nod;
    $fmo=$datmm + $nom;
    $fyr=$datyy + $noy;
    $dat1=date("Y-m-d", mktime(0,0,0,$fmo,$fda,$fyr))."<BR>";
    echo $dat1;
    $datestart=$dat1;
    $rep--;
  }
} [/code]

Any suggestions? I've searched using several keywords and had no luck.
Link to comment
https://forums.phpfreaks.com/topic/28446-future-dates/
Share on other sites

I'm not clear on what you are after. Are you trying to get what you have in your first example, where you get the "first Thursday for each month" (or 2nd Friday, 3rd Monday, etc).

If that is the case, they how would you like it to be handled if the date to be repeated is the 5th weekday for that month?
Link to comment
https://forums.phpfreaks.com/topic/28446-future-dates/#findComment-130183
Share on other sites

Function "strtotime()" can help you.

My understanding for your objective is this.

[code]
<?php
$datestart = date('Y-m-d');  /* the starting date */
echo $datestart."<br />";
$rep = 11;  /* number of future (or past) dates to display */
$nod = 7; /* number of days in the future (or past) to increment
             (or decrement) the date */
$nom = 1;  /* number of months in the future (or past) to increment
             (or decrement) the date */
$noy = 0;  /* number of years in the future (or past) to increment
             (or decrement) the date */

future_date_weekly($datestart,$rep);
print "<hr>";
future_date_monthly($datestart,$rep);
print "<hr>";
future_date_yearly($datestart,$rep);

function future_date_weekly($datestart,$rep) {
 while ($rep>= 1) {
$datestart = date("l Y-m-d", strtotime(" +7 days",strtotime($datestart)));
   echo $datestart."<br>";
   $rep--;
 }
}
function future_date_monthly($datestart,$rep) {
 while ($rep>= 1) {
$datestart = strtotime($datestart);
$day_of_week = date("l");
$datestart = strtotime(date("Y-m-01",$datestart));
$datestart = strtotime("next month",$datestart);
$datestart = date("l Y-m-d",strtotime("first $day_of_week", $datestart));
   echo $datestart."<br>";
   $rep--;
 }
}
function future_date_yearly($datestart,$rep) {
 while ($rep>= 1) {
$datestart = strtotime($datestart);
$day_of_week = date("l");
$datestart = strtotime(date("Y-01-01",$datestart));
$datestart = strtotime("next year",$datestart);
$datestart = date("l Y-m-d",strtotime("first $day_of_week", $datestart));
   echo $datestart."<br>";
   $rep--;
 }
}

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28446-future-dates/#findComment-130185
Share on other sites

Small typo in the code above. The future_date_montly function did not format the day of the week based on the $datestart. Corrected code below. Again, thanks for your help!
[code]<?php
$datestart = date('Y-m-d');  /* the starting date */
echo $datestart."<br />";
$rep = 11;  /* number of future (or past) dates to display */
$nod = 7; /* number of days in the future (or past) to increment
              (or decrement) the date */
$nom = 1;  /* number of months in the future (or past) to increment
              (or decrement) the date */
$noy = 0;  /* number of years in the future (or past) to increment
              (or decrement) the date */

future_date_weekly($datestart,$rep);
print "<hr>";
future_date_monthly($datestart,$rep);
print "<hr>";
future_date_yearly($datestart,$rep);

function future_date_weekly($datestart,$rep) {
  while ($rep>= 1) {
$datestart = date("l Y-m-d", strtotime(" +7 days",strtotime($datestart)));
    echo $datestart."<br>";
    $rep--;
  }
}
function future_date_monthly($datestart,$rep) {
  while ($rep>= 1) {
$datestart = strtotime($datestart);
$day_of_week = date("l",$datestart); //Added $datestart variable
$datestart = strtotime(date("Y-m-01",$datestart));
$datestart = strtotime("next month",$datestart);
$datestart = date("l Y-m-d",strtotime("first $day_of_week", $datestart));
    echo $datestart."<br>";
    $rep--;
  }
}
function future_date_yearly($datestart,$rep) {
  while ($rep>= 1) {
$datestart = strtotime($datestart);
$day_of_week = date("l", $datestart); //Added $datestart variable
$datestart = strtotime(date("Y-01-01",$datestart));
$datestart = strtotime("next year",$datestart);
$datestart = date("l Y-m-d",strtotime("first $day_of_week", $datestart));
    echo $datestart."<br>";
    $rep--;
  }
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/28446-future-dates/#findComment-130199
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.