PHP比较日期 [英] PHP compare dates

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

问题描述


可能重复:

php date比较

我有一个从mySQL数据库获取的日期如下所示:

I have a date that I take from the mySQL database that looks like this:

2011-06-20

我以这种方式获得当前日期:

And I get the date of the current day in this way:

$todaydate = date('Y-m-d');

我需要知道的是如何比较两个结果?

What I need to know is how do I compare the two results?

如何比较日期和理解例如如果从数据库日期或一个月或一年...传递一个星期...。

How can I compare the dates and understand for example if a week is passed from the database date or a month or a year..etc..?

谢谢!

推荐答案

当MySQL具有内置功能时,已经。您应该查看MySQL的 DATEDIFF() 函数:

There is no need to put that burden on PHP when MySQL has built-in functionality for that already. You should take a look at MySQL's DATEDIFF() function:


DATEDIFF )以从一个日期到另一个日期的天数表示的值来表示 expr1 - expr2 expr1 expr2 是日期或日期和时间表达式。在计算中只使用值的日期部分。

DATEDIFF() returns expr1expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

两个日期的例子,给出7天的差异可以是:

An example of two dates that'd give a 7-day difference could be:

mysql> select datediff('2011-06-18','2011-06-25');
+-------------------------------------+
| datediff('2011-06-18','2011-06-25') |
+-------------------------------------+
|                                  -7 | 
+-------------------------------------+

这意味着第一个日期发生在第一个日期后的-7天;那是7天之前。如果你让两个参数切换位置,结果将是一个正7。

This means that the first date occured -7 days after the first date; that's 7 days before. If you let the two arguments switch place, the result would be a positive 7.

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

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