简体   繁体   中英

issue with time format to insert event in google calendar with php

I am creating a code to insert an event in google calendar with PHP.

For that, I have made one script and put date formate static in that which is working correctly and inserting correctly.

But the problem is that how can I make '2020-01-21T17:00:00-07:00' with PHP dynamically?

I have tried echo date("Ymd").'T'.date("Ymd"); but it is not enough . Can anybody help me to create this date format?

You use the below code for your query

 <?php
 date_default_timezone_set('Asia/Calcutta');
 $date_with_time_zone = date(DATE_W3C);
 echo $date_with_time_zone;
 ?>

This function return that format

function formatDateForGoogleCalender($timestamp=0) {

    if (!$timestamp) {
        // set your timezone 
        date_default_timezone_set('Asia/Calcutta'); // for India 
        $timestamp = time();
    }
    $date = date('Y-m-d\TH:i:s', $timestamp);

    $matches = array();
    if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {

        $date .= "-".$matches[2].':'.$matches[3];
    } else {
       $date .= 'Z';
    }
    return $date;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM