Skip to content
Advertisement

MYSQL add 2 hours to time in Database when echo to PHP

I have tried numerous ways to get this time zone issue resolved. I have a MYSQL database that stores Time in the following format.

2021-01-15 14:34:46

The time is uploaded by a tellematic device so I have no control over the time loaded to the DB

I need to add two ours to the time from the DB and echo it to a PHP Page

I have tried the following:

SELECT * FROM gs_objects WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > dt_tracker 
AND imei = '358899053810647'

<td><?php echo $rows['dt_tracker']; ?></td>

The time stays the same no change to it not adding the two hours

AND

SELECT CONCAT(DATE_FORMAT(dt_tracker, '%Y-%m-%d '),
              DATE_FORMAT(DATE_ADD(dt_tracker, INTERVAL 2 HOUR), '%H:%m:%s'),
              DATE_FORMAT(dt_tracker, '')) AS dt_format, 
FROM gs_objects WHERE imei = '358899053810647'

<?php echo $rows['dt_format']; ?>

There is no output at all

dt_tracker is the date and time saved to the database the DATATYPE is DATETIME

I need to add two hours to the output time.

2021-01-15 16:34:46

Resolved with the following Query:

SELECT CONCAT(DATE_FORMAT(dt_tracker, '%H:%i:%s '),
              DATE_FORMAT(DATE_ADD(dt_tracker, INTERVAL 2 HOUR), '%H:%i:%s'),
              DATE_FORMAT(dt_tracker, ' %p')) AS dt_tracker, imei FROM 
gs_objects WHERE imei = '358899053064062'

Advertisement

Answer

SELECT CONCAT(DATE_FORMAT(dt_tracker, '%H:%i:%s '),
              DATE_FORMAT(DATE_ADD(dt_tracker, INTERVAL 2 HOUR), '%H:%i:%s'),
              DATE_FORMAT(dt_tracker, ' %p')) AS dt_tracker, imei FROM 
gs_objects WHERE imei = '358899053064062'

In my initial Query I was conflicting the Date format with the Time Format

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement