PHP does not display time from database correctly

I have a script that counts the time before the event

enter image description here

Start date and start time are retrieved from the database.

My problem

Days are calculated correctly, but the hours are the same for each event and are not calculated correctly

while(){ $gameDate = $row['event_date']; $time=$row['event_time']; $calcDate = $gameDate.$time; $calcDate = strtotime(str_replace('/', '-', $gameDate)); $remaining = $calcDate - time(); $days_remaining = floor($remaining / 86400); $hours_remaining = floor(($remaining % 86400) / 3600); } 

EVENT TABLE

enter image description here

Again, look at the 1st image, how the clock is displayed as 13 for both events, even if it is different times for both events in the database

Any idea what I am doing wrong here or how can I improve the script?

+6
source share
1 answer

There is an error in this line:

 $calcDate = strtotime(str_replace('/', '-', $gameDate)); 

Replace $gameDate with $calcDate , which contains your full date + time line in which you created one line:

 $calcDate = strtotime(str_replace('/', '-', $calcDate)); 

Or just do this:

 $data = array( array('event_date' => '2015-07-13', 'event_time' => '15:00'), array('event_date' => '2015-07-11', 'event_time' => '20:00') ); foreach ($data as $row) { $calcDate = strtotime(str_replace('/', '-', $row['event_date'].$row['event_time'])); $remaining = $calcDate - time(); $days_remaining = floor($remaining / 86400); $hours_remaining = floor(($remaining % 86400) / 3600); echo $days_remaining." ".$hours_remaining."\n"; } 

Working Code: http://sandbox.onlinephpfunctions.com/code/128a284a176098ede0dfc7bc18cfc5f7081d2afa

This way you avoid mixing different variables .; -)

+2
source

All Articles