Page 1 of 1

php date issue

Posted: Tue Apr 25, 2006 12:44 pm
by sulen
I have written a script to add recurring events that happen once every week to work with a calendar but I am facing a very frustrating bug. The output produced is as under

1156834800/2006-08-29
1157439600/2006-09-05
1158044400/2006-09-12
1158649200/2006-09-19
1159254000/2006-09-26
1159858800/2006-10-03
1160463600/2006-10-10
1161068400/2006-10-17
1161673200/2006-10-24
1162278000/2006-10-30
1162882800/2006-11-06
1163487600/2006-11-13
1164092400/2006-11-20
1164697200/2006-11-27
1165302000/2006-12-04

You can see for the month of October or 10 the second last date is 24 and last day is 30 instead of it being 31 which is 24+7 which it is doing prefectly in all other cases. Any help on this will be appreciated. Thanks in advance

The code is as below

Code: Select all

$dayschedulearray[6] = "Mon/Wed";
$dayschedulearray[7] = "Tue/Thu";
$dayschedulearray[8] = "Mon/Wed/Fri";
$dayschedulearray[1] = "Mon";
$dayschedulearray[2] = "Tue";
$dayschedulearray[3] = "Wed";
$dayschedulearray[4] = "Thu";
$dayschedulearray[5] = "Fri";

$dayschedule[6] = "Monday";
$dayschedule[7] = "Tuesday";
$dayschedule[8] = "Monday";
$dayschedule[1] = "Monday";
$dayschedule[2] = "Tuesday";
$dayschedule[3] = "Wednesday";
$dayschedule[4] = "Thursday";
$dayschedule[5] = "Friday";

if(!$conn=mysql_connect('192.168.2.5','webaccess','dcpass'))
die("Could not connect to mysql.\n".mysql_error());

mysql_select_db("den");

$query = mysql_query("select c.day, c.datein, c.dateout, c.timein, c.timeout, co.deptcno, c.studio from courserequest c , course co where c.course = co.cid order by c.id DESC limit 1") or die (mysql_error());
 while ($result = mysql_fetch_array($query)) 
	{

	$sem_start = $result['datein'];
	$sem_end = $result['dateout'];

	$day  = $result['day'];

	$course_day = $dayschedule[$day];

	list($sem_year, $sem_month, $sem_day) = explode("-", $result['datein']);
	//$sem_start_date = ;
	$sem_start_day = date("l", mktime(0, 0, 0, $sem_month, $sem_day, $sem_year));
	//echo $sem_start_day.",".$course_day;

	$sem_start_date = strtotime($sem_start);


		switch ($sem_start_day) 
			{
			case "Monday":
				 if($course_day == 'Monday')
					 $newdate = $sem_start_date;
 				 if($course_day == 'Tuesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 1); 
				 if($course_day == 'Wednesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 2); 
				 if($course_day == 'Thursday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 3); 
				 if($course_day == 'Friday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 4); 
				 //echo "One";
				 break;
			case "Tuesday":
				 if($course_day == 'Monday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 6);
 				 if($course_day == 'Tuesday')
					 $newdate = $sem_start_date ; 
				 if($course_day == 'Wednesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 1); 
				 if($course_day == 'Thursday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 2); 
				 if($course_day == 'Friday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 3); 
				 //echo "Two";
				 break;
			case "Wednesday":
				 if($course_day == 'Monday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 5);
 				 if($course_day == 'Tuesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 6); 
				 if($course_day == 'Wednesday')
					 $newdate = $sem_start_date ; 
				 if($course_day == 'Thursday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 1); 
				 if($course_day == 'Friday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 2);
				 //echo "Two";
				 break;
			case "Thursday":
				 if($course_day == 'Monday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 4);
 				 if($course_day == 'Tuesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 5); 
				 if($course_day == 'Wednesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 6); 
				 if($course_day == 'Thursday')
					 $newdate = $sem_start_date ; 
				 if($course_day == 'Friday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 1); 
				 //echo "Two";
				 break;
			case "Friday":
				if($course_day == 'Monday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 3);
 				 if($course_day == 'Tuesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 4); 
				 if($course_day == 'Wednesday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 5); 
				 if($course_day == 'Thursday')
					 $newdate = $sem_start_date + (60 * 60 * 24 * 6); 
				 if($course_day == 'Friday')
					 $newdate = $sem_start_date ; 
				 //echo "Two";
				 break;
			default:
				 echo "Day mismatch";
				 //break;
			}
			
		
		//echo date('y-m-d', $newdate); 
		$sem_end_date = strtotime($sem_end);

		if(!$conn2=mysql_connect('localhost','root','partytime'))
			die("Could not connect to local mysql.\n".mysql_error());

		mysql_select_db("vcalendar");

		$timein = date('H:i:s' , strtotime($result['timein']));
		$timeout = date('H:i:s' , strtotime($result['timeout']));


      	// adds values to events table
//	$add2base = mysql_query("insert into events (event_id, user_id, category_id, event_title, event_date, event_time, event_time_end, event_is_public  ) values ('', '2', '" . $result['studio'] . "', '" . $result['deptcno'] . "', '" . date('y-m-d', $newdate) . "', '" . $timein  . "', '" .  $timeout . "', '1')") or die("MySQL error - insert error:<br>" . mysql_error());
//
//		$lastinserted = mysql_insert_id();

		//echo $sem_end_date."<br>";
		$newdate2 = $newdate + (60 * 60 * 24 * 7);
		echo $newdate2."/".date('Y-m-d', $newdate2)."<br>";
		
		while( $sem_end_date > $newdate2)
		{
			
			
			// adds values to events table
//			$add2base = mysql_query("insert into events (event_parent_id, user_id, category_id, event_title, event_date, event_time, event_time_end, event_is_public  ) values ('".$lastinserted."', '2', '" . $result['studio'] . "', '" . $result['deptcno'] . "', '" . date('y-m-d', $newdate2) . "', '" . $timein  . "', '" .  $timeout . "', '1')") or die("MySQL error - insert error:<br>" . mysql_error());

			
			
			$newdate2 = $newdate2 + (60 * 60 * 24 * 7);
			echo $newdate2."/".date('Y-m-d', $newdate2)."<br>";
		}

		//echo "insert into events (event_parent_id, user_id, category_id, event_title, event_date, event_time, event_time_end, event_is_public  ) values ('', '2', '" . $result['studio'] . "', '" . $result['deptcno'] . "', '" . date('y-m-d', $newdate) . "', '" . $timein  . "', '" .  $timeout . "', '1')";

	}

Posted: Tue Apr 25, 2006 1:17 pm
by feyd
The essense of the output works fine for me, so I have no idea what you could be running into.

Code: Select all

[feyd@home]>php -r "$newdate = strtotime('-1 week', 1156834800); $sem_end_date = mktime(0, 0, 0, 12, 1, 2006); while($sem_end_date > $newdate) { $newdate = $newdate + (60 * 60 * 24 * 7); echo $newdate.'/'.date('Y-m-d', $newdate).PHP_EOL; }"
1156834800/2006-08-29
1157439600/2006-09-05
1158044400/2006-09-12
1158649200/2006-09-19
1159254000/2006-09-26
1159858800/2006-10-03
1160463600/2006-10-10
1161068400/2006-10-17
1161673200/2006-10-24
1162278000/2006-10-31
1162882800/2006-11-07
1163487600/2006-11-14
1164092400/2006-11-21
1164697200/2006-11-28
1165302000/2006-12-05

Posted: Tue Apr 25, 2006 2:35 pm
by pickle
Rather than using $date + (60 * 60 * 24 * 6), etc, use

Code: Select all

$date = strototime('+1 week',$date);
That'll make it daylight saving time safe as well as being easier to read. Daylight saving time kicks in on the 29th (between the 24th & 31st) so that might be what's messing with you.

Posted: Tue Apr 25, 2006 2:50 pm
by sulen
Yeah it was the daylight savings issue. Thanks for the insight.