Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php if statement to check if current datetime is between 2 datetime columns

Tags:

php

datetime

PostQopen and PostQClose are datetime columns. I need a php if statement to check if the current datetime is between the PostQopen and PostQClose columns.

$now = date('Y-m-d H:i:s');

if($rows['PostQopen'] >= '$now' && $rows['PostQClose'] < '$now' ){

echo "TRUE";

} else { 

echo "FALSE";

};
like image 722
Craig Martin Avatar asked Apr 12 '14 18:04

Craig Martin


People also ask

How do you compare time in PHP?

Interval Between Different Dates In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument. The diff() method will return a new object of type DateInterval .

How can I get second date between two dates in PHP?

Show activity on this post. $timeFirst = strtotime('2011-05-12 18:20:20'); $timeSecond = strtotime('2011-05-13 18:20:20'); $differenceInSeconds = $timeSecond - $timeFirst; You will then be able to use the seconds to find minutes, hours, days, etc.


1 Answers

Use DateTime.

$now = new DateTime("now");
$PostQopen = new DateTime($rows['PostQopen']);
$PostQClose = new DateTime($rows['PostQClose']);

if($PostQopen >= $now && $PostQClose <=$now){
 // do ur stuff
}
like image 188
Abhik Chakraborty Avatar answered Sep 29 '22 07:09

Abhik Chakraborty