检查两个PHP日期时间对象是否设置为相同的日期(忽略时间) [英] Check if two PHP datetime objects are set to the same date ( ignoring time )

查看:150
本文介绍了检查两个PHP日期时间对象是否设置为相同的日期(忽略时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想比较2个datetime对象,看看它们是否设置在同一个日期,但是我并不在意对象的时间分量。目前我正在使用date_format命令来提取字符串Ym-d进行比较,但这似乎很尴尬。

I just want to compare 2 datetime objects to see if they are set to the same date, but I don't care about the time component of the the object. At the moment I am using the date_format command to extract strings like 'Y-m-d' to compare but this seems awkward.

$firstDate = date_format($firstDateTimeObj, 'Y-m-d');
$secondDate = date_format($secondDateTimeObj, 'Y-m-d');

if !($firstDate == $secondDate) {

// some code
}

我是新手编程和PHP,所以任何指针都赞赏。

I'm new to programming and PHP so any pointers appreciated.

推荐答案

使用对象语法!

$firstDate = $firstDateTimeObj->format('Y-m-d');
$secondDate = $secondDateTimeObj->format('Y-m-d');

你与你的if表达式非常接近,但是!操作符必须在括号内。

You were very close with your if expression, but the ! operator must be within the parenthesis.

if (!($firstDate == $secondDate))

这也可以表示为

if ($firstDate != $secondDate)

这篇关于检查两个PHP日期时间对象是否设置为相同的日期(忽略时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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