PHP中的日期比较 [英] Date comparison in PHP

查看:100
本文介绍了PHP中的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个日期存储在我的SQL数据库中,作为一个255个字符的VARCHAR。我在一个对象内声明了这个字符串为

I currently have a date that's being stored in my SQL database as a VARCHAR of 255 characters. I declared this string as

   //within an object...
   $date = date(DATE_RFC822);

现在,稍后在编码中,我意识到我需要实际比较日期。我的初始,非常幼稚的尝试看起来有点像这样:

Now, later on in the coding, I realise that I need to actually compare dates with each other. My initial, very naive attempt looked a little bit like this:

if(object_1->date > object_2->date){
 //do this that assumes that object_1 was created at a later date than object_2
}else{
 continue;
}

虽然在同一天的不同时间工作正常;一周后使用代码开始显示重大错误。

While this worked fine for different times of the same day; using the code a week later began to show significant bugs.

推荐答案

strtotime() 将字符串转换为unix时间(一个整数),可以容易与其他unix时间值进行比较。如果您运行的是PHP 5.2.8或更高版本,您还可以使用 DateTime 类比较容易比较(参见这个问题了解更多信息)

strtotime() converts a string into unix time (an integer) which can easily be compared with other unix time values. If you are running PHP 5.2.8 or greater, you could also make use of the DateTime class which are relatively easy to compare (see this question for more info)

这篇关于PHP中的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆