'Inserting events into google calendar

My project is of creating chatbots. And I have an appointment component in it. So when visitor selects any time slots and date of appointment it should get insert in google calendar. I have integrated google calendar before this using Oath login. So this is working fine. Problem comes when I select 3rd April 12AM slot it gets inserted into 2nd April 12AM. And this problem is only from 12AM to 2.30AM. After 2.30AM, all the time slot is inserted perfectly into 3rd April. Please help me out with this issue.

I am sharing my code as well:

include_once 'google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
define("SCOPE",Google_Service_Calendar::CALENDAR_READONLY);
define("APP_NAME","Google Calendar");

$client->setClientId('xyz.apps.googleusercontent.com');
$client->setClientSecret('abc');
$client->setRedirectUri(base_url('dashboard/Dashboard'));
$client->setApplicationName(APP_NAME);
$client->setScopes([SCOPE]);
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$client->setAccessToken($google_access_token);

$service = new Google_Service_Calendar($client);
$user_timezone = $service->calendars->get('primary')->getTimezone();
                     

$appointment_data=$this->db->query("select * from {PRE}_master_bot_question where question_id='".$response_data['question_id']."' ")->row_array();

$answer_text=explode(",",$this->input->post('answer', true));

$from_time = $answer_text[1];

$datetime_from = date("c", strtotime($date.$from_time)); //Convert datetime into RFC3339 format

$newDate = date('H:i:s', strtotime($answer_text[1]. ' +'.$appointment_data['duration'].'minutes'));

$to_time = date("g:iA", strtotime($newDate));

$datetime_to = date("c", strtotime($date.$to_time)); //Convert datetime into RFC3339 format

// Get user calendar timezone
$user_timezone = $service->calendars->get('primary')->getTimezone();
                      
$event = new Google_Service_Calendar_Event(array(
    'summary' => 'Appointment confirmation',
    'location' => 'Online Meeting',
    'start' => array(
        'dateTime' => $datetime_from,
        'timeZone' => $user_timezone
    ),
   'end' => array(
       'dateTime' => $datetime_to,
       'timeZone' => $user_timezone
    ),
    "guestsCanInviteOthers" => false,
    "guestsCanModify" => false,
    "guestsCanSeeOtherGuests" => false,
));
                      
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source