Reputation: 33
I have following code I am trying to compare two get in order to get into if statement but I something wrong with is code.
the following code should run if the time is above 23:29 and less then 08:10...
$gettime = "04:39"; // getting this from database
$startdate = strtotime("23:29");
$startdate1 = date("H:i", $startdate);
$enddate = strtotime("08:10");
$enddate1 = date("H:i", $enddate);
if ($gettime >= strtotime($startdate1) && $gettime <= strtotime($enddate1)) {
echo "ok working";
}
Upvotes: 0
Views: 751
Reputation: 53525
Assuming you're receiving the time from the DB in a date
format (and not as a string
):
change:
if($gettime >= strtotime($startdate1) && $gettime <= strtotime($enddate1))
to:
if($gettime >= strtotime($startdate1) || $gettime <= strtotime($enddate1))
Upvotes: 1
Reputation: 21386
You may refer to PHP documentation about DateTime::diff
function at their website http://php.net/manual/en/datetime.diff.php
You may also go through this stackoverflow question How to calculate the difference between two dates using PHP?
Upvotes: 0
Reputation: 664
Make sure your comaring the right types of data, time stamps with time stamps and not w/ strings etc...
$gettime= strtotime("22:00"); // getting this from database
$startdate = strtotime("21:00");
//$startdate1 = date("H:i", $startdate);
$enddate = strtotime("23:00");
//$enddate1 = date("H:i", $enddate);
//this condition i need to run
if($gettime >= $startdate && $gettime <= $enddate)
{
echo"ok working";
}
Upvotes: 1
Reputation: 8200
You are comparing the string with a date. $gettime is a string and you are comparing it with a time object.
You need to convert $gettime to a time object by calling $gettime = strtotime($gettime), and then you can compare it using > or < like you have above.
Upvotes: 1
Reputation: 156
For comparing times, you should use the provided PHP classes The DateTime::diff will return an object with time difference info: http://www.php.net/manual/en/datetime.diff.php
Upvotes: 0