public function iCalDateToUnixTimestamp($icalDate)
{
/**
* iCal times may be in 3 formats, ref http://www.kanzaki.com/docs/ical/dateTime.html
* UTC: Has a trailing 'Z'
* Floating: No timezone reference specified, no trailing 'Z', use local time
* TZID: Set timezone as specified
* Use DateTime class objects to get around limitations with mktime and gmmktime. Must have a local timezone set
* to process floating times.
*/
$pattern = '/\\AT?Z?I?D?=?(.*):?';
// 1: TimeZone
$pattern .= '([0-9]{4})';
// 2: YYYY
$pattern .= '([0-9]{2})';
// 3: MM
$pattern .= '([0-9]{2})';
// 4: DD
$pattern .= 'T?';
// Time delimiter
$pattern .= '([0-9]{0,2})';
// 5: HH
$pattern .= '([0-9]{0,2})';
// 6: MM
$pattern .= '([0-9]{0,2})';
// 7: SS
$pattern .= '(Z?)/';
// 8: UTC flag
preg_match($pattern, $icalDate, $date);
$timezone = str_replace(':', '', $date[1]);
// Unix timestamp can't represent dates before 1970
if ($date[2] <= self::UNIX_MIN_YEAR) {
return false;
}
// Unix timestamps after 03:14:07 UTC 2038-01-19 might cause an overflow
// if 32 bit integers are used.
if ($date[8] == 'Z') {
$convDate = new \DateTime('now', new \DateTimeZone('UTC'));
} else {
if (!$timezone) {
$convDate = new \DateTime('now', new \DateTimeZone($this->calendarTimeZone()));
} else {
if ($this->isValidTimeZoneId($timezone)) {
$convDate = new \DateTime('now', new \DateTimeZone($timezone));
} else {
$convDate = new \DateTime('now', new \DateTimeZone(date_default_timezone_get()));
}
}
}
$convDate->setDate((int) $date[2], (int) $date[3], (int) $date[4]);
$convDate->setTime((int) $date[5], (int) $date[6], (int) $date[7]);
$timestamp = $convDate->getTimestamp();
return $timestamp;
}