|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-11-01 14:12 UTC] arnbme at gmail dot com
Description:
------------
the last option of strtotime seems to ignore the int $now parameter.
Reproduce code:
---------------
<?php
$br='<br>';
$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){
$Year++;
$Month = 1;}
else
$Month++;
$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:
--------------
2009-11-29
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 05:00:01 2025 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: <?php $now = strtotime('2009-12-01'); echo date('c', strtotime('last Sunday', $now))."\n"; ?>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")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"; }DateInterval::__construct (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)