Heim >Backend-Entwicklung >PHP-Tutorial > 小小的有关问题

小小的有关问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 13:32:181160Durchsuche

小小的问题
为什么这样也echo错误呢
$date1 = "Apr/13/2012";
$date2 = "Apr/6/2012";
if($date1 > $date2){
echo "正确";
}else{
echo "错误"."<script>alert('错误');</script>";
}

?>

------解决方案--------------------
你比较的是字符串,又不是数字。$date1与$date2第一个不同的字符是"1" and "6".
"1"的ascii 码比"6"的ascii小,当然date1>date2!

比较前先把字符串转换为时间戳(strtotime)

PHP code

<?php $date1 = "Apr/13/2012";
$date2 = "Apr/6/2012";
if(strtotime($date1) > strtotime($date2)){
echo "正确";
}else{
echo "错误"."<script>alert('错误');</script>";
}

?>
<br><font color="#e78608">------解决方案--------------------</font><br>字符串好像是不能比较大小吧!!
<br><font color="#e78608">------解决方案--------------------</font><br>标准时间格式没有你这样的写法:Apr/13/2012<br>应该是这种:Apr 13 2012<br>这种:4/13/2012<br>或者是这种:2012-4-13<br>或者:..... <div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn