开发者

Comparing DateTime date values in PHP

开发者 https://www.devze.com 2023-01-27 12:51 出处:网络
What\'s the best way to compare the date values of two different DateTime objects?That is, comparing the year/month/day, but ignoring time.

What's the best way to compare the date values of two different DateTime objects? That is, comparing the year/month/day, but ignoring time.

Specifically, I'm trying to check if a given DateTime date value is found 开发者_开发技巧in an array of DateTime objects.

function dateExists($aDateTime, $anArrayOfDateTimes) {
...
}

Thanks


You could use array_filter:

<?php

$dates = array(
    new DateTime('2000-12-01'),
    new DateTime('2005-01-20'),
    new DateTime('2010-05-06'),
    new DateTime('2008-04-03'),
    new DateTime('2007-11-05'),
);

$theDate = new DateTime('2010-05-06'); 

$result = array_shift(array_filter($dates, function($d) use ($theDate) {
    return $d->format('Y-m-d') == $theDate->format('Y-m-d');
}));

var_dump($result);

$result is either a DateTime object (if found) or otherwise false.


You can use mktime function putting zero into the hour, minute, second fields. Then compare the results like this:

function compareDate($d1, $d2)
{
    $d1 = date_parse ($d1);
    $d2 = date_parse ($d2);
    return mktime (0, 0, 0, $d2['month'], $d2['day'], $d2['year']) - mktime (0, 0, 0, $d1['month'], $d1['day'], $d1['year'])

}

Pay attention to the odd order of the mktime arguments. PHP 5.3 adds the datediff istruction.


Yet another version

function arrayContainsDate($array, $date, $format = 'Y-m-d')
{
    if (is_string($date)) {
        $date = new DateTime($date);
    }
    $date = $date->format($format);
    foreach ($array as $dt) {
        if ($date === $dt->format($format)) {
            return TRUE;
        }
    }
    return FALSE;
}

Usage:

$dateTimes = array();
$dateTimes[] = new DateTime('2010-11-28 05:15:00');
$dateTimes[] = new DateTime('2010-06-10 08:18:00');
$dateTimes[] = new DateTime('2010-12-28 20:20:00');
var_dump(
    arrayContainsDate($dateTimes, '2010-12-01 06:06:06'),
    arrayContainsDate($dateTimes, '2010-06-10 13:12:11'),
    arrayContainsDate($dateTimes, new DateTime('2010-01-01 01:10:10')),
    arrayContainsDate($dateTimes, new DateTime('2010-12-28 23:15:00'))
);

Output (run on codepad):

bool(false)
bool(true)
bool(false)
bool(true)

If dont mind making the array into an object, you could consider creating a DateTime Collection instead that can hold this and other methods (example).


If your gonna compare two dates i would suggest using the strtotime to perform the check on the unix time values.


I don't see any other way than creating a formatted date string for each object:

function dateExists($aDateTime, $anArrayOfDateTimes)
{   
    $format = 'Ymd';
    $dateString = $aDateTime->format($format);

    foreach($anArrayOfDateTimes as $DateTimeToCompare)
        if($DateTimeToCompare->format($format) === $dateString)
            return true;

    return false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消