4

I have a DateTime Object which holds an past timestamp.

I would now like to check if this DateTime is older than for example 48Hours.

How can I compore them best?

Regards

EDIT: Hi,

thanks for the help. Heres the helper method. Any naming suggesstions?

    protected function checkTemporalValidity(UserInterface $user, $hours)
{
    $confirmationRequestedAt = $user->getConfirmationTokenRequestedAt();
    $confirmationExpiredAt = new \DateTime('-48hours');

    $timeDifference = $confirmationRequestedAt->diff($confirmationExpiredAt);

    if ($timeDifference->hours >  $hours) {
        return false;
    }

    return true;
}
bodokaiser
  • 15,122
  • 22
  • 97
  • 140
  • thanks :) lets see what I do with the var names – bodokaiser Apr 17 '12 at 20:02
  • It formats slightly differently than `date()`, look up on the [DateInterval::format()](http://www.php.net/manual/en/dateinterval.format.php). But beware there's no such member variable as `hours` in `DateInterval`. Please look into the documentation :) – dan-lee Apr 17 '12 at 20:06
  • Hi, I noticed this "bug-canidate" but for first it is ok :) – bodokaiser Apr 18 '12 at 14:52

4 Answers4

6
$a = new DateTime();
$b = new DateTime('-3days');

$diff = $a->diff($b);

if ($diff->days >= 2) {
  echo 'At least 2 days old';
}

I used $a and $b for 'testing' purposes. DateTime::diff returns a DateInterval object, which has a member variable days that returns the actual day difference.

dan-lee
  • 14,365
  • 5
  • 52
  • 77
3

You might want to look here: How do I compare two DateTime objects in PHP 5.2.8?

The easiest solution therefore would probably be to just create another DateTime object that has a date of NOW -48Hours and then compare to that.

Community
  • 1
  • 1
bardiir
  • 14,556
  • 9
  • 41
  • 66
0

I know this answer is a bit late, but maybe it helps somebody else:

/**
 * Checks if the elapsed time between $startDate and now, is bigger
 * than a given period. This is useful to check an expiry-date.
 * @param DateTime $startDate The moment the time measurement begins.
 * @param DateInterval $validFor The period, the action/token may be used.
 * @return bool Returns true if the action/token expired, otherwise false.
 */
function isExpired(DateTime $startDate, DateInterval $validFor)
{
  $now = new DateTime();

  $expiryDate = clone $startDate;
  $expiryDate->add($validFor);

  return $now > $expiryDate;
}

$startDate = new DateTime('2013-06-16 12:36:34');
$validFor = new DateInterval('P2D'); // valid for 2 days (48h)
$isExpired = isExpired($startDate, $validFor);

This way you can also test for other periods than whole days, and it works on Windows servers with older PHP versions too (there was a bug with DateInterval->days returning always 6015).

martinstoeckli
  • 23,430
  • 6
  • 56
  • 87
0

For people who don't want to work with days...

You can get a unix timestamp with the DateTime::getTimestamp() method. A unix timestamp is in seconds, which is easy to handle. So you can do:

$now = new DateTime();
$nowInSeconds = $now->getTimestamp();

$confirmationRequestedAtInSeconds = $confirmationRequestedAt->getTimestamp();

$expired = $now > $confirmationRequestedAtInSeconds + 48 * 60 * 60;

$expired will be true if the time is expired

Wilt
  • 41,477
  • 12
  • 152
  • 203