php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18655 strtotime() with "next Sunday"
Submitted: 2002-07-30 13:57 UTC Modified: 2002-07-30 14:45 UTC
From: thearcher at comcast dot net Assigned:
Status: Closed Package: Date/time related
PHP Version: 4.2.2 OS: RedHat 6.2 kernel 2.4.18 #3
Private report: No CVE-ID:
 [2002-07-30 13:57 UTC] thearcher at comcast dot net
The configure line is:
'./configure' '--enable-ftp' '--enable-bc-math' '--enable-gd-native-ttf' '--enable-inline-optimization' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-sysvsem' '--enable-sysvshm' '--enable-sockets' '--enable-track-vars' '--enable-trans-sid' '--with-freetype-dir=/usr' '--with-gd=/usr' '--with-imap=/usr' '--with-imlib=/usr' '--with-jpeg-dir=/usr' '--with-kerberos=/usr/kerberos' '--with-mysql=/usr/local/mysql' '--with-openssl=/usr' '--with-png-dir=/usr' '--with-t1lib=/usr' '--with-ttf=/usr' '--with-xml' '--with-xpm-dir=/usr/X11R6' '--with-zlib-dir=/usr' '--with-apache=../apache_1.3.26' '--with-snmp' '--enable-ucd-snmp-hack'

I was writing a script that was supposed to determine all the Sundays between an entered date and the current date.  I had a loop in which I used "strtotime("next Sunday",$date)".  This did not work and just returned the sunday after the current date instead of the Sunday after the timestamp i gave it.  I had used an identical expression outside of the loop and it correctly returned the next Sunday of the timestamp i gave it.

This script replicates the problem:
<?
$now=time();
$date=strtotime("November 24, 2002");
while($now<$date) {
 echo date("l, F j, Y", $now)."<br>";
 $now=strtotime("next Sunday", $now);
}
?>

That loop will never terminate as it logically should.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-30 14:11 UTC] rasmus@php.net
strtotime() will return the first sunday it finds after the given timestamp in this case.  So you really need to move the timestamp ahead each time and ask it to find the next Sunday in order to prevent it from finding the same Sunday again.  Or perhaps keep adding 1 week instead once you found the first one.  Watch out for the daylight savings switchover though.
 [2002-07-30 14:28 UTC] m dot ford at lmu dot ac dot uk
That's *not* what the spec of strtotime at http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html (linked from http://www.php.net/manual/en/function.strtotime.php) says.

I quote:

"A number may precede a day of the week item to move forward supplementary weeks. It is best used in expression like `third monday'."

and:

"A few numbers may be written out in words in most contexts. ... Here is the list: `first' for 1, `next' for 2, `third' for 3, ..."

implying that "next sunday" should return the *second* Sunday from the given date.

And, indeed, the following does exactly what the original poster intended:

  while($now<$date) {
    echo date("l, F j, Y", $now)."<br>";
    $now=strtotime("2 Sunday", $now);
  }

So the problem would appear to be that, notwithstanding the GNU documentation, the PHP strtotime() interpretation of "next" is not, in fact, equivalent to 2.

Cheers!

Mike
 [2002-07-30 14:45 UTC] rasmus@php.net
Yeah, reading a bit more it looks like you are right.  I have fixed this in CVS now.  "next" maps to "2" now instead of "1"
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 07:02:14 2014 UTC