Jump to content

first day of the month strtotime issue


smugford2

Recommended Posts

I have a simple calendar with a repeating event on specific weekdays

 

repeat every monday

repeat every thursday

etc etc

 

The specific issue that i'm having is the following code works for everything except the first day of the month.

  $cMonth = "april";
  $cYear = "2010";
$thu_counter = (int) date("d", strtotime("first thursday ".$cMonth." ".$cYear));

The expected result should be 1  (for Thursday april first 2010)

The result that i'm getting is 8 (or Thursay April Eighth 2010)

I've moved the calendar ahead one month and tested it for may as well and I have the same issue.

  $cMonth = "may";
  $cYear = "2010";
$sat_counter = (int) date("d", strtotime("first saturday ".$cMonth." ".$cYear));
echo $sat_counter; 

expected result 1

actual result 8

 

any thoughts

Thanks in Advanced

Link to comment
https://forums.phpfreaks.com/topic/197769-first-day-of-the-month-strtotime-issue/
Share on other sites

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

It seems so. I wrote a quick workaround for it, maybe you could incorporate it into a function and use it.

$occurrence = 'first';
$day_name = 'thursday';
$month = 'April';
$year = 2010;
$date = (int) date("d", strtotime($occurrence . " " . $day_name . " " . $month . " " . $year));
if( $occurrence == 'first' && $date >= 8 ) {
     $date -= 7;
}
echo $date;

I have a simple calendar with a repeating event on specific weekdays

 

repeat every monday

repeat every thursday

etc etc

 

The specific issue that i'm having is the following code works for everything except the first day of the month.

  $cMonth = "april";
  $cYear = "2010";
$thu_counter = (int) date("d", strtotime("first thursday ".$cMonth." ".$cYear));

The expected result should be 1  (for Thursday april first 2010)

The result that i'm getting is 8 (or Thursay April Eighth 2010)

I've moved the calendar ahead one month and tested it for may as well and I have the same issue.

  $cMonth = "may";
  $cYear = "2010";
$sat_counter = (int) date("d", strtotime("first saturday ".$cMonth." ".$cYear));
echo $sat_counter; 

expected result 1

actual result 8

 

any thoughts

Thanks in Advanced

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.