note 118616 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Fri, 06 Jan 2017 15:56:42 +0000 |
| Subject: | note 118616 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208710@lists.php.net to get a copy of this message | ||
Note Submitter: geoffrey dot hoffman at gmail dot com
----
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.