Getting Close - but need Help

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

Post Reply
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Getting Close - but need Help

Post by Pavilion »

Hello Everyone:

I think I'm getting close, but need a little assist to get over the hump. In short, I'm trying to generate monday dates between a begin date and an end date. Following is my test script:

Code: Select all

/// add by weeks
$start = strtotime("09/12/2013 07:00 PM");
$end = strtotime("09/12/2013 09:00 PM");
$finishCycle = strtotime("10/01/2014 07:00 PM");

$repeatStart = date("h:i:s", strtotime(date("h:i:s", $start))); 
$repeatEnd = date("h:i:s", strtotime(date("h:i:s", $end)));
$subArry = Array();
$repeatArry = Array();
$finish = date("m/d/Y", $finishCycle);
$return = date("m/d/Y", $start);
$iterate = 0;
// while compares finish date to date returned from function. If the return date is >= finish then the while continues to loop
while(strtotime($finish)>=strtotime($return))
  {
  $return = date("m/d/Y", strtotime(' Monday +'.$iterate.' week')) ; 
  $returnStart = $return . " " . $repeatStart;
  $iterate = $iterate + 1;
	  if (strtotime($return)<=strtotime($finish))
	  {
	 //  echo "return: " . date("D m/d/Y h:i:s", strtotime($returnStart)) . "</br>";

			$subArry = array();
			$mondayStart = date("D m/d/Y h:i:s", strtotime($returnStart)); 
			// $mondayEnd  = date("Y-m-d", strtotime(' Monday +'.$i.' week')) . " " . $repeatEnd; 
			array_push($subArry, $mondayStart);
			array_push($repeatArry, $subArry);  
	  } 
  } 
var_dump($repeatArry);
At this point I'm holding off on generating $mondayEnd until I can figure out how to get $mondayStart correct output. My problem is as follows:
  1. The array is dumping correct Monday dates
  2. BUT, those dates are between today's date and the $finish date.
  3. How do I get this line: $return = date("m/d/Y", strtotime(' Monday +'.$iterate.' week')) ; to start iterating off of the actual start date ($start)instead of today's date?
Thanks in advance for your assistance. ~ Pavilion
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting Close - but need Help

Post by AbraCadaver »

Not sure what your doing with the times, but here's a simpler start maybe:

Code: Select all

for($next=strtotime('Monday', $start); $next <= $finishCycle; $next=strtotime('next Monday', $next)) {
    $array[] = date('"D m/d/Y h:i:s', $next);
}
print_r($array);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Getting Close - but need Help

Post by Pavilion »

AbraCadaver wrote:Not sure what your doing with the times, but here's a simpler start maybe:

Code: Select all

for($next=strtotime('Monday', $start); $next <= $finishCycle; $next=strtotime('next Monday', $next)) {
    $array[] = date('"D m/d/Y h:i:s', $next);
}
print_r($array);
Thank you so much AbraCadaver. You're help is appreciated. ~ Pavilion
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Getting Close - but need Help

Post by Pavilion »

AbraCadaver wrote:Not sure what your doing with the times, but here's a simpler start maybe:

Code: Select all

for($next=strtotime('Monday', $start); $next <= $finishCycle; $next=strtotime('next Monday', $next)) {
    $array[] = date('"D m/d/Y h:i:s', $next);
}
print_r($array);
I'm back :) with a new twist.

You wondered what I was going to do with the times. My users want to be able to setup repeat events in their scheduling application. They really like the new ability to generate activities on the same day of the week every week, 2 weeks, etc... However, they are now asking for the ability to generate repeat events on the same day/week every month or the same day/week of the same month every year. An example might be the 1st Tuesday of every month, or the 2nd Wednesday in January every single year.

Do you have any suggestions at all on how to generate the same week & day every month, or the same month, week and day every year?

Thanks in Advance - Pavilion
Post Reply