C0NFUSED Posted January 31, 2013 Share Posted January 31, 2013 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 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. Quote Link to comment Share on other sites More sharing options...
shlumph Posted January 31, 2013 Share Posted January 31, 2013 (edited) 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. Edited January 31, 2013 by shlumph Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 I just showed you it worked. The code I pasted is LITERALLY what I ran and got that output. If it works for you without the "of", then take out the "of". What is the issue? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 31, 2013 Share Posted January 31, 2013 Also, what version of PHP are you using. Some of the possible formats are not supported in earlier versions. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 31, 2013 Share Posted January 31, 2013 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. It sets the day-of-month to 1. "ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01"). "ordinal dayname " does advance to another day. (Example: "first tuesday july 2008" means "2008-07-08", see also point 4 in the list above). "'last' dayname 'of' " takes the last dayname of the current month. (Example: "last wed of july 2008" means "2008-07-30") "'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"). Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 (edited) Edit: Psycho got the link before I could Edited January 31, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
C0NFUSED Posted January 31, 2013 Author Share Posted January 31, 2013 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" Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 Also, what version of PHP are you using. Some of the possible formats are not supported in earlier versions. Quote Link to comment Share on other sites More sharing options...
C0NFUSED Posted January 31, 2013 Author Share Posted January 31, 2013 (edited) Also, what version of PHP are you using. Some of the possible formats are not supported in earlier versions. My server has php version 5.2 Edited January 31, 2013 by C0NFUSED Quote Link to comment Share on other sites More sharing options...
shlumph Posted February 1, 2013 Share Posted February 1, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 1, 2013 Share Posted February 1, 2013 Without the "of" is incorrect. Quote Link to comment Share on other sites More sharing options...
kicken Posted February 1, 2013 Share Posted February 1, 2013 (edited) 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 February 1, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
salathe Posted February 1, 2013 Share Posted February 1, 2013 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: Set month to May (midnight on May 1st of the current year) Set year to 2013 (midnight on May 1st 2013) Adjust day to last (previous) Monday (midnight on Monday April 29th 2013). [1] http://php.net/datetime.formats.relative Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.