Search code examples
phpmysqlfullcalendarfullcalendar-schedulerfullcalendar-4

Fullcalendar events render from DB without regard to timezone


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!


Solution

  • 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