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

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

问题描述

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

How can I compare two dates in PHP?

日期以如下格式存储在数据库中

The date is stored in the database in the following format

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?

推荐答案

在数据库中日期看起来像这样 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天全站免登陆