I’m getting google calendar events, via iCal, which have a date like this:
'20240713T011500Z'
The regular php funcs dont seem to like this format.
This function works, but seems pretty clunky. Is there not a built in way to deal with this format?
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');
2
Answers
I assume that input properly validated.
I think date() and strtotine() should do the trick.
Yeah, you don’t need to do that. You probably also don’t need to do whatever is in the
getTimeInThisTimezone()
function either.Output:
Ref: https://www.php.net/manual/en/datetimeimmutable.createfromformat.php