0

I have made a chat using php, sql and ajax. All the message are stored in DB with a datetime format like this 0000-00-00 00:00:00. The issue: every day, from midnight (00:00:00) to 00:59:59 the messages are stored in DB, but not visualized in chat. In the rest of the day it work perfectly. I think it depend on UTC time zone. My timezone is UTC +1 (so the day doesn't go from 0 to 23, but from 1 to 0).

Actually I use this PHP code:

$now = new DateTime(null, new DateTimeZone('Europe/Rome'));
$now = $now->format('Y-m-d H:i:s');

The column where I store this datetime:

'time' datetime NOT NULL DEFAULT '0000-00-00 00:00:00'

The date are always stored correctly. Someone can help me?

EDIT: to call data stored I use this:

$datetime="SELECT var1, var2, time FROM chat WHERE time >= DATE_SUB(NOW(),INTERVAL 1 HOUR) ORDER BY time ASC";
$datetimeX = $db->prepare($datetime);
$datetimeX->execute();

while($row = $datetimeX->fetch(PDO::FETCH_ASSOC)) {

echo "<span style='color:#f2f2f2;'>" . $row['time'] . " </span>";

//other stuff
}

1 Answers1

0

As a php function date_default_timezone_set ( string $timezone_identifier ) : bool is an option to set default timezone used by all date/time functions.

For example:

<?php
    date_default_timezone_set('Europe/Amsterdam');
?>

A reference list is given for supported timezone.
Refer this for further information.

Community
  • 1
  • 1
Bhavik Parmar
  • 424
  • 1
  • 6
  • 9