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
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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.