Jump to content

Last Monday of a month


C0NFUSED

Recommended Posts

I've found several ways and answers on how to do this..

 

Currently I'm trying this:

 

strtotime("last Monday of {$_POST['to-month']} {$_POST['to-year']}");

 

I have confirmed that when the variables are replaced, it looks like "last Monday of May 2013"

 

Yet I'm getting a NULL value. I've tried variations on this:

 

"last Monday May 2013"

"last Monday in May 2013"

 

Everything I'm trying is returning NULL! strtotime does work, I use it in another place and it works great.. My server has php version 5.2

Link to comment
Share on other sites

1369612800

2013-05-27

 

<?php
$time = strtotime("last Monday of May 2013");
echo $time.'<br>';
echo date('Y-m-d', $time);
?>

 

How did you debug your string? what's print_r($_POST); show? How have you debugged the strtotime? Post the actual code.

Link to comment
Share on other sites

They are two different values tho.

 

1369612800

2013-05-27

1367193600

2013-04-29

 

<?php
$time = strtotime("last Monday of May 2013");
echo $time.'<br>';
echo date('Y-m-d', $time);
echo '<br>';
$time = strtotime("last Monday May 2013");
echo $time.'<br>';
echo date('Y-m-d', $time);
?>

 

The of one is accurate.

Link to comment
Share on other sites

FWIW:

 

http://www.php.net/manual/en/datetime.formats.relative.php

Also observe that the "of" in "ordinal space dayname space 'of' " and "'last' space dayname space 'of' " does something special.

  1. It sets the day-of-month to 1.
  2. "ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01").
  3. "ordinal dayname " does advance to another day. (Example: "first tuesday july 2008" means "2008-07-08", see also point 4 in the list above).
  4. "'last' dayname 'of' " takes the last dayname of the current month. (Example: "last wed of july 2008" means "2008-07-30")
  5. "'last' dayname" takes the last dayname from the current day. (Example: "last wed july 2008" means "2008-06-25"; "july 2008" first sets the current date to "2008-07-01" and then "last wed" moves to the previous Wednesday which is "2008-06-25").

Link to comment
Share on other sites

1369612800

2013-05-27

 

<?php
$time = strtotime("last Monday of May 2013");
echo $time.'<br>';
echo date('Y-m-d', $time);
?>

 

How did you debug your string? what's print_r($_POST); show? How have you debugged the strtotime? Post the actual code.

 

 

Well I tried this:

 

echo strtotime("last Monday of {$_POST['to-month']} {$_POST['to-year']}");
echo " / ";
echo "last Monday of {$_POST['to-month']} {$_POST['to-year']}";
die();

 

which returns

/ last Monday of November 2013

the strtotime returns nothing and the input it's getting is "last Monday of November 2013"

Link to comment
Share on other sites

 

Does not return false:

strtotime("last Monday May 2012");

 

Does return false:

strtotime("last Monday of May 2012");

 

At least in PHP 5.2.17... 5.3.X is different, and both will work.

Remove the "of", and it should work. Also, mini versions are important. There may be differences between 5.2.1 and 5.2.17.

Link to comment
Share on other sites

Rather than rely on strtotime to figure out what you want, you could just figure it out explicitly:

<?php




$month = 5;
$year = 2013;

$lastDay = mktime(0, 0, 0, $month+1, 1, $year) - 86400;
$wday = date('w', $lastDay);
$targetDay = 1;
$offset = $wday<$targetDay?abs(7-$targetDay+$wday):$wday-$targetDay;

echo date('r', $lastDay-($offset*86400));

 

edit: more generic

 

Edited by kicken
Link to comment
Share on other sites

Just to set the record straight…

 

The special "of" behaviour was introduced in PHP 5.3.0; earlier versions will not understand this statement and will return FALSE.

 

Whilst "last Monday May 2013" may appear to work (in that it doesn't return FALSE), it does not get the last Monday in May 2013. As described in the manual [1], "Relative statements are always processed after non-relative statements."  The non-relative statements are "May" and "2013", and "last Monday" (meaning previous Monday) is a relative statement.

 

PHP processes that as:

 

  1. Set month to May (midnight on May 1st of the current year)
  2. Set year to 2013 (midnight on May 1st 2013)
  3.  Adjust day to last (previous) Monday (midnight on Monday April 29th 2013).

 

 

[1] http://php.net/datetime.formats.relative

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.