Convert RFC 3339 timestamp string to Unix timestamp

@param string $dateTime

@return integer
181909
public function convertToUnixTimestamp($dateTime)
    {
        // ignore fractional second
        if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:|\.\d+)(Z|[+-]\d{2}:\d{2})$/', $dateTime, $matches)) {
            $timestamp = gmmktime(
                $matches[4], $matches[5], $matches[6], // hours, minutes, seconds
                $matches[2], $matches[3], $matches[1]  // month, day, year
            );

            // local offset (timezone)
            if ($matches[7] !== 'Z') {
                preg_match('/^([+-])(\d{2}):(\d{2})$/', $matches[7], $matches);

                $offset = ($matches[1] === '-' ? -1 : 1) * ($matches[2] * 60 + $matches[3]) * 60;
                $timestamp -= $offset;
            }
        }

        return $timestamp;
    }
1225469
public function setThaiYear(DateTime $timestamp){
        if($this->checkThaiLocale()){
            return $timestamp->setDate(($timestamp->format('Y')+543),$timestamp->format('m'),$timestamp->format('d'));
        }else{
            return $timestamp;
        }
    }
1167159
public static function ToTimestamp($DateTime = '') {
      if (($TestTime = strtotime($DateTime)) !== FALSE) {
         return $TestTime;
      } elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})(?:\s{1}(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?)?$/', $DateTime, $Matches)) {
         $Year = $Matches[1];
         $Month = $Matches[2];
         $Day = $Matches[3];
         $Hour = ArrayValue(4, $Matches, 0);
         $Minute = ArrayValue(5, $Matches, 0);
         $Second = ArrayValue(6, $Matches, 0);
         return mktime($Hour, $Minute, $Second, $Month, $Day, $Year);
      } elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $DateTime, $Matches)) {
         $Year = $Matches[1];
         $Month = $Matches[2];
         $Day = $Matches[3];
         return mktime(0, 0, 0, $Month, $Day, $Year);
      // } elseif ($DateTime == '') {
      //    return time();
      } else {
         return FALSE;
      }
   }
550010
public static function timeTense($datetime)
    {
        $datetime = self::makeCarbon($datetime);
        $yesterday = $datetime->subDays(1);
        $tomorrow = $datetime->addDays(1);
        $time = $datetime->format('H:i');
        $date = $datetime->format('j M Y');

        if ($datetime->isToday()) {
            $date = 'Today';
        }
        elseif ($datetime->isYesterday()) {
            $date = 'Yesterday';
        }
        elseif ($datetime->isTomorrow()) {
            $date = 'Tomorrow';
        }

        return $date.' at '.$time;
    }
345754
protected function toDateTime($timestamp)
    {
        if (is_numeric($timestamp)) {
            // Unix timestamp.
            $result = new DateTime();
            $result->setTimestamp($timestamp);
        } else {
            // Formatted timestamp, e.g. yyyy-mm-dd hh:mm:ss.
            $result = DateTime::createFromFormat(static::$timestampFormat, $timestamp);
        }
        return $result;
    }
1235045
public function isoToTimestamp($isoTime)
    {
        $dateTime = \DateTime::createFromFormat(self::SOLR_ISO_DATETIME_FORMAT,
            $isoTime);
        return $dateTime ? (int)$dateTime->format('U') : 0;
    }
868589
protected function getTstamp($time = null)
    {
        $time = !is_null($time) ? strtotime($time) : time();
        $month = date("M", $time);
        $day = substr("  " . date("j", $time), -2);
        $hhmmss = date("H:i:s", $time);
        return $month . " " . $day . " " . $hhmmss;
    }
418389
public static DateTime fromString(String rfc3339Timestamp) {
		if (rfc3339Timestamp == null) {
			return null;
		}
		
		DateTime dateTime = new DateTime(rfc3339Timestamp, DateTimeZone.UTC);
		return dateTime;
	}
900726
public static function fromString($time)
    {
        try {
            $date = new \DateTime($time);
        } catch (\Exception $e) {
            throw new \InvalidArgumentException(sprintf('Invalid time "%s".', $time));
        }

        return self::fromDate($date);
    }
63349
private function validateDateTime($datetime)
    {
        if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', $datetime, $parts) == true) {
            $time = gmmktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]);

            $input_dt = new \DateTime($datetime, new \DateTimeZone('GMT0'));
            $input_time = $input_dt->getTimestamp();
            if ($input_time === false) return false;

            return $input_time == $time;
        } else {
            return false;
        }
    }
74565
static function howLongAgo($datetime)
    {
        $timestamp = strtotime($datetime);
        $seconds = time();

        $time = date('Y', $seconds) - date('Y', $timestamp);
        if ($time > 0)
        {
            if ($time == 1)
            {
                return '去年';
            }
            else
            {
                return $time . '年前';
            }
        }

        $time = date('m', $seconds) - date('m', $timestamp);
        if ($time > 0)
        {
            if ($time == 1)
            {
                return '上月';
            }
            else
            {
                return $time . '个月前';
            }
        }
        $time = date('d', $seconds) - date('d', $timestamp);
        if ($time > 0)
        {
            if ($time == 1)
            {
                return '昨天';
            }
            elseif ($time == 2)
            {
                return '前天';
            }
            else
            {
                return $time . '天前';
            }
        }

        $time = date('H', $seconds) - date('H', $timestamp);
        if ($time >= 1)
        {
            return $time . '小时前';
        }

        $time = date('i', $seconds) - date('i', $timestamp);
        if ($time >= 1)
        {
            return $time . '分钟前';
        }

        $time = date('s', $seconds) - date('s', $timestamp);
        return $time . '秒前';
    }
1206130
public function unix2DosTime($unixtime = 0)
    {
        $timearray = (0 == $unixtime) ? getdate() : getdate($unixtime);
        if ($timearray['year'] < 1980) {
            $timearray['year'] = 1980;
            $timearray['mon'] = 1;
            $timearray['mday'] = 1;
            $timearray['hours'] = 0;
            $timearray['minutes'] = 0;
            $timearray['seconds'] = 0;
        } // end if
        return (($timearray['year'] - 1980) << 25)
            | ($timearray['mon'] << 21)
            | ($timearray['mday'] << 16)
            | ($timearray['hours'] << 11)
            | ($timearray['minutes'] << 5)
            | ($timearray['seconds'] >> 1);
    }
298787
public static function getDateTime($datetime = null)
    {
        if ($datetime instanceof \DateTime) {
            return clone $datetime;
        }
        if ($datetime instanceof \DateTimeImmutable) {
            return new \DateTime($datetime->format(\DateTime::ATOM));
        }
        if (\is_string($datetime)) {
            if (\is_numeric($datetime)) {
                /*
                    check if datetime is an int of the the form YYYYMMDD or YYYYMMDDHHMMSS
                    Debug::_log('datetime', $datetime)
                */
                $isTs = !preg_match('/^(19|20)\d{2}(0[1-9]|1[0-2])[0-3][0-9](([01]\d|2[0-3])\d[0-5]\d[0-5]\d)?$/', $datetime);
                if ($isTs) {
                    $datetime = '@'.$datetime;
                }
            }
            Debug::_log('datetime', $datetime);
            return new \DateTime($datetime);
        }
        if (\is_int($datetime) || \is_float($datetime)) {
            $dateTime = new \DateTime();
            $dateTime->setTimestamp($datetime);
            return $dateTime;
        }
        return new \DateTime();
    }
1187000
public static function createFromDateTime(DateTimeInterface $dateTime): self
    {
        $utcTimestamp = $dateTime->getTimestamp();

        // yes, PHP DateTime can accept integer timestamp only as a string ¯\_( ͡° ͜ʖ ͡°)_/¯
        $result = (new self("@$utcTimestamp"))->setTimezone($dateTime->getTimezone());

        return $result;
    }
53936
public static function fromDateTime(DateTime $dateTime)
    {
        $date = new Date();
        $date->setYear(intval($dateTime->format('Y')));
        $date->setMonth(intval($dateTime->format('m')));
        $date->setDay(intval($dateTime->format('d')));

        $result = new AdManagerDateTime();
        $result->setDate($date);
        $result->setHour(intval($dateTime->format('H')));
        $result->setMinute(intval($dateTime->format('i')));
        $result->setSecond(intval($dateTime->format('s')));
        $result->setTimeZoneID($dateTime->format('e'));

        return $result;
    }
601636
public static function toUnix($time) {
        if (!$time) {
            return time();

        } else if ($time instanceof DateTime) {
            return $time->format('U');
        }

        return is_string($time) ? strtotime($time) : (int) $time;
    }
865093
private function formatTimeAsString(\DateTimeInterface $dateTime, $ns)
    {
        $dateTime = $dateTime->setTimeZone(new \DateTimeZone('UTC'));
        if ($ns === null) {
            return $dateTime->format(Timestamp::FORMAT);
        } else {
            $ns = (string) $ns;
            $ns = str_pad($ns, 9, '0', STR_PAD_LEFT);
            if (substr($ns, 6, 3) === '000') {
                $ns = substr($ns, 0, 6);
            }

            return sprintf(
                $dateTime->format(Timestamp::FORMAT_INTERPOLATE),
                $ns
            );
        }
    }