php根据时区,取时间戳和日期。

正文开始

    //根据时区将时间戳转换成当地时间戳
    public static function getTimeByTimezone($timezone, $timeInt = null)
    {
        if (!$timeInt) $timeInt = time();
        date_default_timezone_set('UTC');
        $timestamp = strtotime(sprintf("%s hours", $timezone),  $timeInt);
        $timestampBack = $timestamp + (-$timezone) * 60 * 60;
        $t = strtotime(date('Y-m-d', $timestamp));
        $todayInt = $t + (-$timezone) * 60 * 60;
        return [$timestampBack, $todayInt];
    }
    /**
     * 时间格式化 设备时间转字符串
     * @param string $dateformat 时间格式
     * @param int $timestamp 时间戳
     * @param int $timeoffset 时区偏差
     * @return string
     */
    public static  function qgmdate($dateformat = 'Y-m-d H:i:s', $timestamp = '', $timeoffset = 8) {
        if(empty($timestamp)) {
            $timestamp = time();
        }
        $result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
        return $result;
    }

根据文本时间转当地时间戳

   //根据某个时间某个时区的时间戳
    public static function getTimeIntByStr($timezone, $timeStr = '')
    {
        date_default_timezone_set('UTC');
        //这里是0时区取时间戳,偏小了一次
        $timestamp = strtotime($timeStr);
        $timestampBack = $timestamp + (-$timezone) * 60 * 60;
        return $timestampBack;
    }

正文结束

PHP接口(interface)和抽象类(abstract) Thinkphp mysql 数据库断线重连 MySQL server has gone away