Home > Backend Development > PHP Tutorial > 小小的有关问题

小小的有关问题

WBOY
Release: 2016-06-13 13:32:18
Original
1126 people have browsed it

小小的问题
为什么这样也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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template