如何将谷歌日历日期转换为可用日期? ('20240713T011500Z')

问题描述 投票:0回答:1

我通过 iCal 获取谷歌日历活动,其日期如下:

'20240713T011500Z'

常规的 php 函数似乎不喜欢这种格式。 这个函数可以工作,但看起来很笨重。没有内置的方法来处理这种格式吗?

function convertGCalDate($gdate){
    $parts = explode('T', $gdate);
    $date  = $parts[0];
    $nums  = str_split($date);
    $date  = $nums[0].$nums[1].$nums[2].$nums[3].'-'.$nums[4].$nums[5].'-'.$nums[6].$nums[7];
    
    $time = trim($parts[1], 'Z');
    $zulu = str_ends_with($parts[1], 'Z');
    
    $nums    = str_split($time);
    $newtime = $nums[0].$nums[1].':'.$nums[2].$nums[3].':'.$nums[4].$nums[5];
    
    $dt = $date.' '.$newtime;
    
    // this is my TZ func, it works, when I finally get the dt into the right format.
    // $datetime = new DateTime($time);
    // $datetime->setTimezone($that_tz);
    // etc
    $dt = getTimeInThisTimezone($dt, $thatTZ = 'Z', $thisTZ = 'America/Los_Angeles', $outformat = null);
    
    return $dt;
    //2024-07-12 7:15 pm

}

$whatIwant =  convertGCalDate('20240713T011500Z');
php datetime-format
1个回答
0
投票

我假设输入已正确验证。 我认为 date() 和 strtotine() 应该可以解决问题。

function convertGCalDate($gdate){
return echo date('Y-m-d H:i:s',strtotime($gdate));
}
© www.soinside.com 2019 - 2024. All rights reserved.