如何在PHP中比较两个日期? [英] How can I compare two dates in PHP?

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

问题描述

如何在PHP中比较两个日期?

How can I compare two dates in PHP?

在数据库中,日期看起来像是2011-10-2.

In the database, the date looks like 2011-10-2.

如果我想将今天的日期与数据库中的日期进行比较,看哪个更大,我该怎么做?

If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?

我尝试过了

$today = date("Y-m-d");
$expire = $row->expireDate //from db

if($today < $expireDate) { //do something; }

但是它并不是真的那样.还有另一种方法吗?

but it doesn't really work that way. What's another way of doing it?

更新:我知道这篇文章有点陈旧,但是我只想提及carbon,它是与laravel一起使用的一类,但可以与经典php一起使用,并且确实对日期有疑问.检查一下:

Update: I know this post is kinda old but i just wanted to mention carbon, which is a class thats used with laravel but can be used with classic php and it does wonders with dates. check it out: Carbon

推荐答案

数据库中的日期如下所示:2011-10-2

in the database the date looks like this 2011-10-2

将其存储在YYYY-MM-DD中,然后进行字符串比较,因为'1'>'0'等.

Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.

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

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