|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50048 Strtotime Last fails
Submitted: 2009-11-01 14:12 UTC Modified: 2009-11-02 11:50 UTC
From: arnbme at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.0 OS: Wamp or Linux
Private report: No CVE-ID: None
 [2009-11-01 14:12 UTC] arnbme at gmail dot com
the last option of strtotime seems to ignore the int $now parameter. 

Reproduce code:
$mths = array("","January","February","March","April","May","June","July","August","September","October","November","December");
$x = strtotime('2009-11-29');		//date to age
$Year = date("Y",$x);
$Month = date("m",$x);
if ($Month == 12){
	$Month = 1;}
$n=strtotime($mths[$Month].' 01, '.$Year);
echo 'base date:',date("Y-m-d",$n),$br;
if ($n==false)
	die ('bad date');
$nextdt=date( "Y-m-d",strtotime("last Sunday", $n));
echo 'Calc Last:',$nextdt,$br; //result is 2009-11-29 should be 2009-12-27

Expected result:
Should show 2009-12-27

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-01 20:00 UTC]
Thank you for your bug report.

Your example prints the last Sunday for December 1st, 2009. The Sunday before that date is November 29th. Can you explain why you think the output is wrong? Also, could you provide a shorter reproduce script, like this:

$now = strtotime('2009-12-01');
echo date('c', strtotime('last Sunday', $now))."\n";
 [2009-11-02 01:14 UTC] arnbme at gmail dot com
It would seem my interpretation of "last" varies from the one used by PHP strtotime. I assumed "last Sunday" means the last date that was a Sunday in the month specified by now, similar to how "4 Sunday" moves forward in time, however PHP strtotme apparently interprets "last" to mean "prior" in which case it is operating as designed. 

Example 1 for strtotime does not show any results. If documentation for using last with strtotime is available please post a link.
 [2009-11-02 07:19 UTC]
No, in common English usage, when you just say "last Sunday" it means the previous Sunday.

eg. "Where were you last Sunday?"

You have to be explicit and say:

"What date is the last Sunday of November?"

Then the English makes sense, and if you try that with strtotime() you will find that it works.

eg. strtotime("last Sunday of November")
 [2009-11-02 11:22 UTC] arnbme at gmail dot com
It's also common to set repeating events such as meetings on the "nth" or "last" "weekday" of a month. As far as I know no one has yet learned how to hold an event on the last day of a prior month, unless we are holding the last event in a handbasket.
 [2009-11-02 11:25 UTC]
For repeating events like that use a DateInterval.

For example, this will give you the last Tuesday of every month of 2009:

db = new DateTime('2008-12-31');
$de = new DateTime('2009-12-31');
$di = DateInterval::createFromDateString('last tuesday of next month');
$dp = new DatePeriod($db, $di, $de, DatePeriod::EXCLUDE_START_DATE);
foreach($dp as $dt) {
   echo $dt->format("F jS\n") . "<br>\n";
 [2009-11-02 11:28 UTC]
You also seem to imply that strtotime() should have some sort of bias toward future dates?  That makes no sense.  You need dates in the past almost as often as you need dates in the future.
 [2009-11-02 11:50 UTC] arnbme at gmail dot com
(PHP 5 >= 5.3.0)
My Wamp server, that I use for development runs PHP 5.3, 5.2.11 and 5.2.9. My commercial web host runs PHP 5.2.9 and my code was initially developed in PHP 3 something, hence my use of strtotime, but thank you for a way to code future dates in PHP >=5.3

BTW you have a coding error in your example
db = new DateTime('2008-12-31'); should be
$db = new DateTime('2008-12-31'); 

Au revoir (beam me out of here Scotty)
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Jul 03 18:01:26 2020 UTC