kaust Posted November 25, 2006 Share Posted November 25, 2006 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-2006Thur 07-12-2006Thur 04-01-2006*Dates for first Thursday of the next three months.Brief example of Weekly output:Thur 02-11-2006Thur 09-11-2006Thur 16-11-2006*Dates for next three ThursdaysI 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 More sharing options...
Psycho Posted November 25, 2006 Share Posted November 25, 2006 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 More sharing options...
mansuang Posted November 25, 2006 Share Posted November 25, 2006 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 More sharing options...
kaust Posted November 25, 2006 Author Share Posted November 25, 2006 mansuang, that's it. I'd almost worked out similar functions when I received your response. Thanks for the help! ;D Link to comment https://forums.phpfreaks.com/topic/28446-future-dates/#findComment-130192 Share on other sites More sharing options...
kaust Posted November 25, 2006 Author Share Posted November 25, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.