weixin_33696822 2019-11-06 01:00 采纳率: 0%
浏览 51

DateTime PHP的问题

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
}
  • 写回答

2条回答 默认 最新

  • weixin_33711647 2019-11-06 01:24
    关注

    You may try this

    $dateTimeZone = new DateTimeZone("Europe/Rome");
    $dateTime = new DateTime("now", $dateTimeZone);
    $Offset = $dateTimeZone->getOffset($dateTime);
    $now = date("Y-m-d H:i:s", time() + $Offset);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了