在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?

dqayok7935
dqayok7935 你没有显示任何输入,但为什么不只是php.net/manual/en/datetime.createfromformat.php
11 个月之前 回复
dtz33344
dtz33344 为什么不在$pattern数组中添加#H#,#i#和#s#,并按日期进行操作?
11 个月之前 回复
double820122
double820122 你在哪里设置$stamp?
11 个月之前 回复
dongwenyou4298
dongwenyou4298 为什么你期望在preg_replace()中评估任何东西?这就是preg_replace_callback()的全部原因-它允许您执行代码而不是使用固定替换。
11 个月之前 回复

1个回答



preg_replace_callback()</ code>函数中,您使用的是未定义的局部变量 $ stamp </ 代码>。 如果需要从外部作用域继承此变量,则需要使用 use()</ code>选项。</ p>

  $ time_f = preg_replace_callback(
“#([他的])#”,
函数($ matches)使用($ stamp){
return(gmdate($ matches [1],$ stamp));
},
$ date
);
</ code> </ pre>
</ div>

展开原文

原文

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
);

duanguo7021
duanguo7021 谢谢! 它解决了这个问题。
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问