preg_replace 输出中未评估时间
Time is not evaluated in preg_replace output
我写了一个使用 preg_replace 转换日期格式的代码。代码如下:
$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);
它完美地改变了日期格式,但问题是它将时间输出为 H:i:s 而不是时间值!
例如输出是 1398/5/21, H:i:s 而不是 1398/5/21, 22:15:36。
所以,我添加了以下代码:
$time_f = preg_replace_callback(
"#([His])#",
function ($matches) {
return(gmdate($matches[1],$stamp));
},
$date
);
问题解决了。但现在它总是显示时间为:00:00:00
例如:1398/5/21,00:00:00
我该如何解决这个问题?
在您的 preg_replace_callback()
函数中,您使用了未定义的局部变量 $stamp
。如果需要从外部作用域继承这个变量,需要使用use()
选项。
$time_f = preg_replace_callback(
"#([His])#",
function ($matches) use ($stamp) {
return(gmdate($matches[1],$stamp));
},
$date
);
我写了一个使用 preg_replace 转换日期格式的代码。代码如下:
$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);
它完美地改变了日期格式,但问题是它将时间输出为 H:i:s 而不是时间值! 例如输出是 1398/5/21, H:i:s 而不是 1398/5/21, 22:15:36。 所以,我添加了以下代码:
$time_f = preg_replace_callback(
"#([His])#",
function ($matches) {
return(gmdate($matches[1],$stamp));
},
$date
);
问题解决了。但现在它总是显示时间为:00:00:00 例如:1398/5/21,00:00:00
我该如何解决这个问题?
在您的 preg_replace_callback()
函数中,您使用了未定义的局部变量 $stamp
。如果需要从外部作用域继承这个变量,需要使用use()
选项。
$time_f = preg_replace_callback(
"#([His])#",
function ($matches) use ($stamp) {
return(gmdate($matches[1],$stamp));
},
$date
);