doulanli6146 2019-08-12 14:39
浏览 129
已采纳

在preg_replace输出中不计算时间

I wrote a code that converts the date format using preg_replace. It's the following code:

$pattern=array(
    "#Y#",//full year
    "#y#",//short year

    "#M#",//month short name
    "#F#",//month full name
    "#m#",//month number 0 lead
    "#n#",//month number
    "#t#",//days in month

    "#l#",//full week day
    "#D#",//short week day

    "#d#",//day number of month
    "#j#",//day number of month

    "#a#",//AM/PM short view
    "#A#",//AM/PM full view
            );
$replace=array(
    $d->ENnum2FA($converted[0]),//year 13xx
    $d->ENnum2FA(substr($converted[0],2),true),//year xx lead zero

    $d->shmonths[$converted[1]],//month name
    $d->months[$converted[1]],//month name
    $d->ENnum2FA($converted[1],true), //month number
    $d->ENnum2FA($converted[1]), //month number
    //$converted[1],
    $d->j_days_in_month[$converted[1]],

    $d->days[strtolower(gmdate("D",$stamp))],//week day {full view}
    $d->ldays[strtolower(gmdate("D",$stamp))],//week day ‍‍{short view}

    $d->ENnum2FA($converted[2],true),//day of month
    $d->ENnum2FA($converted[2],true),//day of month

    $d->pmam[gmdate('a',$stamp)],
    $d->pmam[gmdate('A',$stamp)],

    );
// $format = "Y/m/d"; example
$date= preg_replace($pattern,$replace,$format);

It changes the date format perfectly ,but the problem is that it outputs the time as H:i:s instead of the time value! For example the output is 1398/5/21, H:i:s instead of 1398/5/21, 22:15:36. So, I added the following code:

$time_f = preg_replace_callback(
    "#([His])#",
    function ($matches) {
        return(gmdate($matches[1],$stamp));
    },
    $date
);

It solved the problem. But Now it shows the time always as: 00:00:00 For example: 1398/5/21, 00:00:00

How can I solve the problem?

  • 写回答

1条回答 默认 最新

  • dongwu9972 2019-08-12 14:50
    关注

    In your preg_replace_callback() function you're using the undefined local variable $stamp. If you need to inherit this variable from the outer scope, you need to use the use() option.

    $time_f = preg_replace_callback(
        "#([His])#",
        function ($matches) use ($stamp) {
            return(gmdate($matches[1],$stamp));
        },
        $date
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况