Reputation: 1145
I have these conditions:
$year = date('Y');
$month = date('M');
$day = date('j');
$hour = date('H');
if($hour >= 00) $a = 05;
if($hour >= 04) $a = 09;
if($hour >= 08) $a = 13;
if($hour >= 12) $a = 17;
if($hour >= 16) $a = 21;
if($hour >= 20) $a = 01;
which should mean that if the current server time is 4:10AM for example,
then a
value should be 09
.
yet when I run a test and echo a
, the value is 13
.
Here'w what I'm echoing:
<?php
$now = new DateTime();
echo $now->format("M j, Y H:i:s O")."\n";
?>
<p>
<?php echo $a ?>
</p>
<p>
<?php echo $month ?>
</p>
<p>
<?php echo $year ?>
</p>
<p>
<?php echo $day ?>
</p>
<p>
<?php echo $hour ?>
</p>
and the results:
Oct 25, 2014 04:59:55 -0400
13
Oct
2014
25
04
Upvotes: 0
Views: 62
Reputation: 749
Extending @Barmars answer ,
even
if(09 < "09")
{
echo "yes";
}
else
{
echo "false";
}
will echo yes
use format character G
, to avoid leading zeroes .
echo $now->format("M j, Y G:i:s O")
and then compare.
Upvotes: 0
Reputation: 780724
Get rid of the leading zeroes on your numbers. When a number begins with 0
, it's interpreted as octal. But octal doesn't have digits 8
and 9
, so the number parser stops at those characters, and interprets 08
and 09
as 0
. So the line:
if ($hour >= 08) $a = 13;
is treated as if you'd written:
if ($hour >= 0) $a = 13;
Since 4 >= 0
is true, it sets $a
to 13
.
Actually, since you're dealing with strings, the correct thing to do is use string comparisons:
if ($hour >= '20') $a = '01';
elseif ($hour >= '16') $a = '21';
elseif ($hour >= '12') $a = 17;
...
Upvotes: 2