首页 > 解决方案 > php 和 Microsoft Graph API 中的带时区的 DateTime

问题描述

我正在尝试阅读房间的日历条目并在他们的日历上输出接下来的三个事件。但是,我首先进行测试以查看是否只能获取当天的前 3 个事件,但似乎时区或其他原因导致它无法正确显示事件。

这是我写的一个函数:

function mb_get_meetings($mb_email = null, $mb_proxy = false, $mb_datetime_start = null, $mb_datetime_finish = null)
{
    date_default_timezone_set('Australia/Melbourne');

    // get the Microsoft Open Graph API
    $mb_msgraph        = json_decode(mb_microsoft_opengraph($mb_proxy), true); // custom function to get Beaker Token + $mb_proxy for internal proxy on or off for dev testing
    $mb_msgraph_token    = $mb_msgraph['access_token'];

    $mb_datetimenow    = new DateTime();
    $mb_datetimezone    = new DateTimeZone('Australia/Melbourne');
    $mb_datetimenow->setTimezone($mb_datetimezone);

    $mb_datetime_start    = new DateTime($mb_datetime_start, $mb_datetimezone);
    $mb_datetime_finish = new DateTime($mb_datetime_finish, $mb_datetimezone);

    $mb_datetime_start    = $mb_datetime_start->format('Y-m-d\TH:i:s.u');
    $mb_datetime_finish = $mb_datetime_finish->format('Y-m-d\TH:i:s.u');

    $mb_url_string = 'https://graph.microsoft.com/v1.0/users/' . $mb_email . '/calendar/calendarView?startDateTime=' . $mb_datetime_start . '&endDateTime=' . $mb_datetime_finish;

    $mb_args = array(
        'headers' => array(
            'Authorization'    => 'Bearer ' . $mb_msgraph_token,
            'Content-Type'    => 'application/x-www-form-urlencoded;charset=UTF-8',
            'Prefer'        => 'outlook.timezone="Australia/Melbourne"'
        ),
        'httpversion'    => '1.1'
    );
    $mb_output    = wp_remote_get($mb_url_string, $mb_args);
    $mb_output    = wp_remote_retrieve_body($mb_output);
    return $mb_output;
}

我使用 Wordpress 作为后端,它确实检索了正文。

在我的前端页面中,我调用:

$mbroom = (mb_get_meetings('email@domain.tld', true, 'today 7am', 'today 7pm'));
$mbroom = json_decode($mbroom, true);
$mbroom = $mbroom['value'];


foreach ($mbroom as $k => $v) {

    // get the first 3 entries
    if ($k < 3) {

        print_r($k);
        print_r($v['subject']);
        print_r(date('g:i', strtotime($v['start']['dateTime']));
        print_r(date('g:i', strtotime($v['end']['dateTime']));
        print_r($v['organizer']['emailAddress']['name']);

        echo '<hr>';
    }
}

在结果中,有时我会没有日历条目,而有时我可能会在下午 2 点获得条目,但从早上 8 点开始就没有任何条目。我尝试将其更改为硬编码YYYY-MM-DDD 08:00YYYY-MM-DD 20:00但无济于事。我也尝试过yesterdaytomorrow但没有/不正确的结果。

我还尝试预订一整天的 30 分钟会议,但也没有奏效。

难道我做错了什么?

标签: phpmicrosoft-graph-apimicrosoft-graph-calendar

解决方案


我建议以 UTC 格式发送 startDateTime 和 endDateTime 参数,格式为 ISO 8601,如文档中所述(https://docs.microsoft.com/en-us/graph/api/user-list-calendarview?view=graph-rest -1.0&tabs=http ),你已经完成了。但是,我建议使用 PHP 常量,因为它不易出错(https://www.php.net/manual/en/class.datetimeinterface.php#datetime.constants.iso8601)。对这些参数执行以下操作:

 $startDateTime = new DateTime(date("Y-m-d H:i:s"));
 $startDateTime = $startDateTime->format(DATE_ISO8601);
 $startDateTime = substr($startDateTime, 0, strpos($startDateTime, '+'));

编辑:使用子字符串删除 DateTime 字符串的语言环境部分可能是一件幼稚的事情,但它确实起到了作用。


推荐阅读