检查两个 PHP datetime 对象是否设置为相同的日期(忽略时间)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8328416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if two PHP datetime objects are set to the same date ( ignoring time )
提问by Dave Shaw
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.
我只想比较 2 个 datetime 对象,看看它们是否设置为相同的日期,但我不关心对象的时间组件。目前我正在使用 date_format 命令来提取像“Ymd”这样的字符串进行比较,但这似乎很尴尬。
$firstDate = date_format($firstDateTimeObj, 'Y-m-d');
$secondDate = date_format($secondDateTimeObj, 'Y-m-d');
if !($firstDate == $secondDate) {
// some code
}
I'm new to programming and PHP so any pointers appreciated.
我是编程和 PHP 的新手,因此不胜感激。
回答by Evert
Use the object syntax!
使用对象语法!
$firstDate = $firstDateTimeObj->format('Y-m-d');
$secondDate = $secondDateTimeObj->format('Y-m-d');
You were very close with your if expression, but the ! operator must be within the parenthesis.
你很接近你的 if 表达式,但是 ! 运算符必须在括号内。
if (!($firstDate == $secondDate))
This can also be expressed as
这也可以表示为
if ($firstDate != $secondDate)
回答by lonesomeday
My first answer was completely wrong, so I'm starting a new one.
我的第一个答案是完全错误的,所以我要开始一个新的答案。
The simplest way, as shown in other answers, is with date_format
. This is almost certainly the way to go. However, there is another way that utilises the full power of the DateTime classes. Use diff
to create a DateInterval
instance, then check its d
property: if it is 0
, it is the same day.
如其他答案所示,最简单的方法是使用date_format
. 这几乎肯定是要走的路。但是,还有另一种方法可以利用 DateTime 类的全部功能。使用diff
创建一个DateInterval
实例,然后检查其d
属性:如果是0
,那是同一天。
// procedural
$diff = date_diff($firstDateTimeObj, $secondDateTimeObj);
// object-oriented
$diff = $firstDateTimeObj->diff($secondDateTimeObj);
if ($diff->format('%a') === '0') {
// do stuff
} else {
// do other stuff
}
Note that this is almost certainly overkill for this instance, but it might be a useful technique if you want to do more complex stuff in future.
请注意,对于这个实例来说,这几乎肯定是矫枉过正,但如果你想在未来做更复杂的事情,它可能是一种有用的技术。
回答by Wesley van Opdorp
I think your approach is good, but I would remove the -
as they do not add anything.
我认为你的方法很好,但我会删除-
它们,因为它们没有添加任何东西。
$firstDate = date_format($firstDateTimeObj, 'Ymd');
$secondDate = date_format($secondDateTimeObj, 'Ymd');
if ($firstDate != $secondDate) {
// some code
}
回答by Paper-bat
Searching for an answer with the same problem.. I arrived to this solution that's look better for me then use diff or other things.
正在寻找具有相同问题的答案.. 我找到了这个对我来说看起来更好的解决方案,然后使用 diff 或其他东西。
The main problem was ignoring the time parts of object DateTime, just set it to a time, for example at 12:00:00
主要问题是忽略对象DateTime的时间部分,只需将其设置为时间,例如在12:00:00
$firstDateTimeObj->setTime(12, 0, 0);
$secondDateTimeObj->setTime(12, 0, 0);
// The main problem was checking if different.. but you can use any comparison
if ($firstDateTimeObj != $secondDateTimeObj) {
}
回答by itsazzad
This worked great for me.
这对我很有用。
$date1=DateTime::createFromFormat('Y-m-d H:i:s', '2014-07-31 07:30:00')->format('Y-m-d');
$date2=DateTime::createFromFormat('Y-m-d H:i:s', '2014-08-01 17:30:00')->format('Y-m-d');
if($date1==$date2){
echo "==";
}else{
echo "!=";
}
回答by Don Bryn
Looking here for the same question and came up with another way to do it. Fairly concise:
在这里寻找同样的问题并想出了另一种方法来做到这一点。相当简洁:
$diff = date_diff($date1,$date2);
if ($diff->days == '0') {
/* it's the same date */
}
First get a date_interval object using date_diff. The date_interval object has a property for 'days' which you can use to get the total number of days between the two dates.
首先使用 date_diff 获取 date_interval 对象。date_interval 对象有一个“天数”属性,您可以使用它来获取两个日期之间的总天数。
I'm using this to compare today to registration dates and send emails at specific time intervals based on how long they have been a member.
我用它来比较今天和注册日期,并根据他们成为会员的时间在特定的时间间隔发送电子邮件。
回答by romulodl
Initialise a DateTime
object and set the time to 00:00:00
. Then you can use any comparison between dates ignoring the times:
初始化一个DateTime
对象并将时间设置为00:00:00
。然后您可以在忽略时间的情况下使用日期之间的任何比较:
$today = \DateTime::createFromFormat('d/m/Y H:i:s', '21/08/2014 00:00:00');
$tomorrow = \DateTime::createFromFormat('d/m/Y H:i:s', '22/08/2014 00:00:00');
// now you can compare them just with dates
var_dump($today < $tomorrow); //true
var_dump($today === $tomorrow); //false
var_dump($today !== $tomorrow); //true
@Evert's answer works fine, but it looks like really wrong to transform a DateTime
object into a string and then compare them.
@Evert 的回答工作正常,但将DateTime
对象转换为字符串然后比较它们看起来真的是错误的。
回答by Jonathan Joosten
Quick and dirty, reusable with as many dates as you like, may still need some adjusting:
快速而肮脏,可重复使用任意数量的日期,可能仍需要一些调整:
update: less dirty.
更新:不那么脏。
if(sameDate($firstDateTimeObj, $secondDateTimeObj, $ThirdDateTimeObj))
{
// your code
}
function sameDate (DateTime ...$dates) {
foreach($dates as $date)
{
$test[] = $date->format('Ymd');
}
$result = array_unique($test);
if(count($result) == 1)
{
return true;
{
return false;
}
回答by user1073670
$first = strtotime(date_format($firstDateTimeObj, 'Y-m-d'));
$second = strtotime(date_format($secondDateTimeObj, 'Y-m-d'));
if ($first != $second) {
// some code
}
回答by TeChn4K
strtotime convert a textual date into a Unix timestamp : http://fr.php.net/manual/en/function.strtotime.php
strtotime 将文本日期转换为 Unix 时间戳:http: //fr.php.net/manual/en/function.strtotime.php
$first = strtotime(date_format($firstDateTimeObj, 'Y-m-d'));
$second = strtotime(date_format($secondDateTimeObj, 'Y-m-d'));
if ($first != $second) {
// some code
}
And also :
并且 :
if ($first > $second) {
// some code
}
if ($first < $second) {
// some code
}