duanche8554
2019-03-30 01:29
浏览 91
已采纳

Fullcalendar事件从DB渲染而不考虑时区

I currently have a fullcalendar application that I've set up using PHP and MYSQL (AWS RDS).

Event start and end times are stored in the database as strings looking like Fri Mar 29 2019 09:00:00 GMT-0700 (Pacific Daylight Time), which come from the event.start and event.end properties.

Then, they are loaded using load.php (called by events: load.php), which looks like:

<?php
include 'dbconfig.php';

$conn = $dbConnection;
$query = "SELECT * FROM Events ORDER BY id";

$statement = $conn->prepare($query);
$statement->execute();

$data = $statement->fetchAll();
$results = array();

foreach($data as $row) {
    $results[] = array(
        'id'   => $row["id"],
        'title'   => $row["title"],
        'resourceId' => $row["resource"],
        'start'   => $row["start_event"],
        'end'   => $row["end_event"]
    );
}

echo json_encode($results);

At this point, they display; however, they always display 7 hours ahead of when they are supposed to (e.g. 09:00:00 turns into 16:00:00).

I assume this is because when the data comes from the database, it is neglecting the timezone in some way, but when I change the timezone using timezone: 'UTC', nothing happens.

Any help/suggestions would be appreciated, and thanks!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhao3562 2019-03-30 02:22
    已采纳

    If your date strings are in the format as you describe in your question e.g.

    Fri Mar 29 2019 09:00:00 GMT-0700 (Pacific Daylight Time)
    

    You should be able to parse them into DateTime objects using date_create_from_format e.g.

    $date_string = 'Fri Mar 29 2019 09:00:00 GMT-0700 (Pacific Daylight Time)';
    $date = date_create_from_format('D M d Y H:i:s \G\M\TO+', $date_string);
    
    echo $date->format('Y-m-d H:i:s');
    

    Output:

    2019-03-29 09:00:00
    

    Demo on 3v4l.org

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题