0

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);


//this condition i need to run    

if($gettime >= strtotime($startdate1) && $gettime <= strtotime($enddate1))
{
echo"ok working";
}

please help me in dis regard

thanks

Fawad ali
  • 33
  • 2

5 Answers5

1

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.

ajon
  • 7,868
  • 11
  • 48
  • 86
1

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))
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

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";
}
blad
  • 665
  • 5
  • 8
0

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

rx80
  • 156
  • 4
0

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?

Community
  • 1
  • 1
Alfred
  • 21,058
  • 61
  • 167
  • 249