#18655 [Com]: strtotime() with "next Sunday"

From: Date: Tue, 30 Jul 2002 18:28:59 +0000
Subject: #18655 [Com]: strtotime() with "next Sunday"
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-15615@lists.php.net to get a copy of this message
ID: 18655 Comment by: m.ford@lmu.ac.uk Reported By: thearcher@comcast.net Status: Bogus Bug Type: Date/time related Operating System: RedHat 6.2 kernel 2.4.18 #3 PHP Version: 4.2.2 New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2002-07-30 14:11:57] 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 13:57:20] thearcher@comcast.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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=18655&edit=1

« previous php.bugs (#15615) next »