1

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

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Will this work if i am retrieving $start_interval from mysql?
Yes. You can pass it a value from the DB as long as it is in an acceptable format. MySQL's native datetime format is perfect to use.
So, in which case, am i right in thinking the second line of your suggestion is redundant? I'm pretty new to php that's all.
It's just a generic example to point you in the right direction. Your implementation may be slightly different.
0
<?php

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

//Some code

}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.