note 118616 added to function.date
| From: | geoffrey dot hoffman at gmail dot com | Date: | Fri, 08 Jan 2016 23:40:20 +0000 |
| Subject: | note 118616 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204621@lists.php.net to get a copy of this message | ||
I just wanted to emphasise that the return value of date( ) is a string, even when the result of
your date format string is a number, such as "j" -> a number 1 to 31, or 'N'
-> a day number 1 for Monday through 7 for Sunday... it's still returned as a string!
"1" or "7" or "31". This is much more obvious on the
"zero-padded" results, but it's worth repeating.
If you aren't careful, you can get stuck in a while loop comparing days of the week with
something like:
<?php
// Evil! Don't use this! This will never return!
while ( date('N', $time ) !== 7 ) {
$time = $time - 86400;
}
?>
... for example. The result of date( ) should be cast to an int for numeric comparison with the
exact equality operator:
<?php
// Works!
while ( (int)date('N', $time ) !== 7 ) {
$time = $time - 86400;
}
?>
Wasted an hour today on that silly mistake.
----
Server IP: 72.52.91.14
Probable Submitter: 71.36.176.57
----
Manual Page -- http://php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/118616
Del: integrated -- https://master.php.net/note/delete/118616/integrated
Del: useless -- https://master.php.net/note/delete/118616/useless
Del: bad code -- https://master.php.net/note/delete/118616/bad+code
Del: spam -- https://master.php.net/note/delete/118616/spam
Del: non-english -- https://master.php.net/note/delete/118616/non-english
Del: in docs -- https://master.php.net/note/delete/118616/in+docs
Del: other reasons-- https://master.php.net/note/delete/118616
Reject -- https://master.php.net/note/reject/118616
Search -- https://master.php.net/manage/user-notes.php