THINKPHP时间比较问题

WBOY
Release: 2016-06-23 14:00:25
Original
1817 people have browsed it

选择时间段,比如2014-03-15 03:12:43 到2014-03-17 03:12:43这个时间段内的数据,前台页面输入的是2014-03-15这种形式,而数据库是2014-03-15 03:12:43这种形式,多了时分秒,我把2014-03-15这种添加字段成2014-03-15 00:00:00形式,然后与数据库的时间字段比较,比较不出大小来。正确的比较方式是什么?


回复讨论(解决方案)

时间比较要化为统一类型
你的数据库是 datatime 而传入的是 date
所以要把 datatime 化为 date 才能比较
date_format(时间字段, '%Y-%m-%d') = '$iuput'

也可以
to_days(时间字段) = to_days('$input')

时间比较要化为统一类型
你的数据库是 datatime 而传入的是 date
所以要把 datatime 化为 date 才能比较
date_format(时间字段, '%Y-%m-%d') = '$iuput'

也可以
to_days(时间字段) = to_days('$input')
数据库的字段是datetime,查询很多条记录。我想把date转换成datetime可以吗?

不可以
精度不同的两个数据,要按精度低的进行比较

<?php $zero1="2014-03-15 00:00:00"; $zero2="2014-03-15 03:12:43"; echo "zero1的时间为:".$zero1."<br>"; echo "zero2的时间为:".$zero2."<br>"; if(strtotime($zero1)<strtotime($zero2)){  echo "zero1早于zero2"; }else{  echo "zero2早于zero1"; }?>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!