Nicolas Charvoz
Nicolas Charvoz

Reputation: 1509

Use of timestamp in PHP

I need to compare dates in PHP, in order to check if a contract has expired or not.

For now i'm doing this :

foreach ($this->array as $row)
{
  if(strtotime($date1) > strtotime($row['end_date']))
  {
    $delay = strtotime($row['end_date']);
    $expired = "V";
  }
  else {
    $delay = strtotime($row['end_date']);
    $expired = "X";
  }

So i just know if a contract has expired. I need to write :

echo "The contract will expire in" . $delay;

And the output would be :

The contract will expire in 3 days.

thanks for your help.

Upvotes: 0

Views: 40

Answers (1)

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

$interval = strtotime($date1) - strtotime($row['end_date']); // ... or replace $date1

$delayInDays = floor($interval / (60 * 60 * 24));

echo "The contract will expire in: " . $delayInDays;

If you're interested in object-oriented way to achieve this, you can take a look at DateTime::diff method.

Upvotes: 1

Related Questions