记录一个PHP玄学问题-函数时间戳无法传入

发布于 2019-12-04  83 次阅读


PHP7.2,使用Yii2框架。代码如下:

A处:

        $date['create_time'] = time();
        $data['pictures'] = Yii::$app->user->identity->username;
        //$data['code'] = Yii::$app->user->identity->uid . date('Ymd');
            if(!$data['comnum']){
            $maxId = $model::find()->select('create_time,code')->orderBy('create_time DESC')->asArray()->one();
            $end = mktime(0,0,0,date("m",$data['create_time']),date("d",$data['create_time']),date("Y",$data['create_time']));
            if($end < $maxId['create_time']){
                $lastId = $maxId['code'];
            }else{
                $lastId = 0;
            }
            $data['code'] = intval($lastId) + 1;
            $data['comnum'] = \common\helpers\FuncHelper::getGht($data['code'],$data['create_time'],1);

B处:

        $data['reg_time'] = time();
        if(!$data['comnum']){
            $maxId = $model::find()->select('reg_time,code')->orderBy('reg_time DESC')->asArray()->one();
            $end = mktime(0,0,0,date("m",$data['reg_time']),date("d",$data['reg_time']),date("Y",$data['reg_time']));
            if($end < $maxId['reg_time']){
                $lastId = $maxId['code'];
            }else{
                $lastId = 0;
            }
            $data['code'] = intval($lastId) + 1;
        $data['comnum'] = \common\helpers\FuncHelper::getGht($data['code'],$data['reg_time'],0);

函数:

public static function getGht($code,$reg_time,$type){
    if(!$code)
        {
            return '';
        }
        $cc = ($type) ? 'YL' : 'TW';
        if(strlen($code) == 1){
            $code = '00' . $code;
        }elseif(strlen($code) == 2){
           $code = '0' . $code;
        }else{
            $code = $code;
        }
        var_dump($cc . date('Ymd', $reg_time) . $code);die;
        return $cc . date('Ymd', $reg_time) . $code;
}

返回结果:A处: string(13) "YL19700101008"

B处: string(13) "TW20191204004"

以上代码A、B处最后一行均引用了这个函数,代码几乎一致,但是函数接收不到A处第二个参数($data['create_time']),半小时未果,遂放弃。

--------------找到原因了,A处第一行变量不能赋值,原因不明

解决方法:函数第二个参数直接用time()代替,反正数据库内时间YII会自动补齐