Roy
Roy

Reputation: 49

Adding to datetime in php

If i have:

if($date>=$start_interval...){

//Some code

}

Where both $date (current date) and $start_interval are both in datetime format - like YYYY-MM-DD 00:00:00. What do i need to do to add an hour (or 60 minutes) to $start_interval?

So the if statement would be - if($date>=$start_interval (+hour)) - in other words if its more than an hour later do something. How would you do this?

Many thanks in advance

Upvotes: 1

Views: 81

Answers (2)

m4t1t0
m4t1t0

Reputation: 5731

<?php

if (strtotime($date) >= strtotime('+1 hour', strtotime($start_interval)) {

//Some code

}

More info: https://www.php.net/manual/en/function.strtotime.php

Upvotes: 0

John Conde
John Conde

Reputation: 219924

Look into DateTime. It makes working with dates easy.

$now = new DateTime();

$start_interval = new DateTime('2012-12-14 00:00:00');
$start_interval->modify('+1 hour');

if ($now >= $start_interval)
{
     // do something
}

Upvotes: 1

Related Questions