0

I have stored travel times in a database. I want to display to the users only the available travels (those who are not yet gone) according to the current time.

This is my php code, but it returns "false" as result. What am I doing wrong please?

require "conn.php"; 

$sql = "SELECT * FROM Horaires";
$result = mysqli_query($conn, $sql);
$temp = array();
$now = strtotime(date("h:i"));

while ($row = mysqli_fetch_array($result))
{
$temp[]=array($row['timing']);
$time = strtotime($temp);
}

if ($now >= $time)
{ echo json_encode($time, JSON_NUMERIC_CHECK); }
else { echo "There is no more travels today"; }

$conn->close();
Mary
  • 41
  • 11
  • does both are in same format? what is that ? please check this once https://stackoverflow.com/questions/33446581/whats-the-right-way-to-compare-two-dates-in-php – Krish Oct 25 '17 at 13:45
  • Add to your question var_dump($temp); and var_dump($time); Also, you fetch many lines from Horaires but your comparison is outside the while, so you only compare the last result? – Nic3500 Oct 25 '17 at 13:52
  • sorry @Krish, i am a beginner. I've checked the post, it is about comparing dates. I want to compare times. I've tried with time() instead of date() but I still get the same result. – Mary Oct 25 '17 at 13:52
  • thank you @Nic. the problem was that in my database the time type was string, not time. So obvious. – Mary Oct 25 '17 at 13:59

1 Answers1

0

if its in a DateTime format (you can create that by using DateTime::createFromFormat("format","time") ) you can use the funciton ->diff to get the diffrence between them. Example:

$time1 = DateTime::createFromFormat("G:i:s",date('G:i:s'));
sleep(5);
$time2 = DateTime::createFromFormat("G:i:s",date('G:i:s'));
echo $time2->diff($time1);

thats how i would solve this. so best would be to get them on an DateTime format.

Ceddi
  • 11
  • 3