Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Same Day of Week Through Month

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I am creating a calendaring application and need to be able to optionally select a check-box or select-box and have it submit entries the same day of the week throughout the month or even for several months. I already have the values for the beginning and ending of the month in the code but need to know how to get the days required.

Below is the preliminary code that I'm using just to test and for testing, it is using only hard-coded values rather than form submission. This is just a rough draft but what's there is working to repeat an event daily for the whole month or for a specific number of days.

PHP:
/* Sample form fields shown for reference.
<select name="NoDays">
<option value="">Select Days (optional)</option>
<option value="1">Repeat 1 Day</option>
<option value="2">Repeat 2 Days</option>
<option value="3">Repeat 3 Days</option>
<option value="4">Repeat 4 Days</option>
<option value="5">Repeat 5 Days</option>
<option value="31">Repeat Every Day</option>
</select>

<input type="checkbox" name="RepeatMonthly" value="1"> 
*/

$RepeatDaily = 1;
$RepType = ($RepeatDaily == 1) ? "days" : "week";
$NumberDays = 4;
$NoDays = ($NumberDays >= 1) ? $NumberDays : "";

$cBegin = strtotime($rowCalendar['Start']);
$cEnd = strtotime($rowCalendar['End']);
$cLast = ($NoDays == 0) ? strtotime('last day of this month', $cEnd) : strtotime('+'.$NoDays.' days', $cEnd);

// Get beginning date/time, otherwise listing starts with the next day
echo $rowCalendar['StaffID'].", ".date("Y-m-d H:i:s",$cBegin).", ".date("Y-m-d H:i:s",$cEnd)."<br>";

while($cBegin <= $cLast) :
	echo $rowCalendar['StaffID'].", ".date("Y-m-d H:i:s",$cBegin).", ".date("Y-m-d H:i:s",$cEnd)."<br>";
	$cBegin = strtotime('+1 '.$RepType, $cBegin);
	$cEnd = strtotime('+1 '.$RepType, $cEnd);
endwhile;
 
I think I figured it out, or had even done so before posting but had an error so it wasn't working. Now it seems to be okay although I've not made it do nothing when there are no choices but I can solve that too with a little more thought. In this first attempt, it is "overly conditionalized" but surely it can be simplified. Also, it is suddenly starting at the 10th rather than the 1st but that too is likely just something I missed.

Here is my test code, the results from which will eventually be updated/inserted into the database.

PHP:
/* Form fields shown for reference.
<select name="StartTime">
<select name="EndTime">
<select name="NoDays">
<input type="checkbox" name="RepeatMonthly" value="1"> 
*/

if (isset($_POST) && ((isset($_POST['RepeatMonthly']) && $_POST['RepeatMonthly'] == 1) || isset($_POST['NoDays']))) :
	if (isset($_POST['RepeatMonthly']) || isset($_POST['NoDays'])) :
		$RepType = (!isset($_POST['RepeatMonthly'])) ? "days" : "week";
		$NoDays = (isset($_POST['NoDays']) && $_POST['NoDays'] >= 1) ? $_POST['NoDays'] : date('t',strtotime($_POST['StartTime']));

		$cBegin = strtotime($_POST['StartTime']);
		$cEnd = strtotime($_POST['EndTime']);
		$cLast = ($NoDays) ? strtotime("+$NoDays days", $cEnd) : strtotime('last day of this month', $cEnd);

		echo "Days in selected month: ".date('t',$cBegin) ."<br>";
		echo "Repeat type: $RepType<br>";
		echo "Start date code: strtotime('+1 $RepType', $cBegin)<br>";
		echo "End date code: strtotime('+1 $RepType', $cEnd)<p>";

		while($cBegin <= $cLast) :
			echo $rowCalendar['StaffID'].", ".date("Y-m-d H:i:s",$cBegin).", ".date("Y-m-d H:i:s",$cEnd)."<br>";
			$cBegin = strtotime("+1 $RepType", $cBegin);
			$cEnd = strtotime("+1 $RepType", $cEnd);
		endwhile;
	endif;
endif;
 
Code:
//if the incoming day is (say) a Friday capture that and all subsequent fridays that month
$days = array();
$begin = new DateTime($_POST['startTime']);
$thisMonth = $begin->format('m');
do{
  $days[] = $begin->format('Y-m-d H:i');
  $begin->modify('next ' . $begin->format('D'));
while($begin->format('m') == $thisMonth);
print_r($days);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top