Check if two PHP datetime objects are set to the same date ( ignoring time )

PhpDatetime

Php Problem Overview


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
}

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

Php Solutions


Solution 1 - Php

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 (!($firstDate == $secondDate))

This can also be expressed as

if ($firstDate != $secondDate)

Solution 2 - Php

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.

// 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.

Solution 3 - Php

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.

The main problem was ignoring the time parts of object DateTime, just set it to a time, for example at 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) {
}

Solution 4 - Php

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
}

Solution 5 - Php

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 "!=";
}

Solution 6 - Php

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.

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.

Solution 7 - Php

strtotime convert a textual date into a Unix timestamp : 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
}

Solution 8 - Php

$first = strtotime(date_format($firstDateTimeObj, 'Y-m-d'));
$second = strtotime(date_format($secondDateTimeObj, 'Y-m-d'));

if ($first != $second) {
    // some code
}

Solution 9 - Php

Initialise a DateTime object and set the time to 00:00:00. Then you can use any comparison between dates ignoring the times:

$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.

Solution 10 - Php

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;
       
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDave ShawView Question on Stackoverflow
Solution 1 - PhpEvertView Answer on Stackoverflow
Solution 2 - PhplonesomedayView Answer on Stackoverflow
Solution 3 - PhpMassimoView Answer on Stackoverflow
Solution 4 - PhpWesley van OpdorpView Answer on Stackoverflow
Solution 5 - PhpitsazzadView Answer on Stackoverflow
Solution 6 - PhpDon BrynView Answer on Stackoverflow
Solution 7 - PhpTeChn4KView Answer on Stackoverflow
Solution 8 - Phpuser1073670View Answer on Stackoverflow
Solution 9 - PhpromulodlView Answer on Stackoverflow
Solution 10 - PhpJonathan JoostenView Answer on Stackoverflow