php strtotime()
失败,持续一位数的分钟或秒。
例如-
echo date('m/d/Y H:i:s', strtotime('02/23/2013 8:50:1 PM')); // 01/01/1970 01:00:00
echo date('m/d/Y H:i:s', strtotime('02/23/2013 8:5 PM')); // 01/01/1970 01:00:00
是否有快速解决方案?
我知道,我们可以分开;检查分钟或秒的字符串长度,并添加一个0前缀。
顺便说一句,有一件奇怪的事情是,如果最后一部分是一位数字,它就不能通过
例如-
echo date('m/d/Y H:i:s', strtotime('02/23/2013 8:5:10 PM')); // 02/23/2013 20:05:10
// worked well, despite of one digit minute
PHP日期手册页规定日期格式化命令的说明符i和s
这是没有办法的,它们必须包含零。
$time = '02/23/2013 8:5 PM';
$time = preg_replace('/ ([0-9]*:[0-9]) /', " ${1}0 ", $time );
echo date('m/d/Y H:i:s', strtotime($time));
preg_replace_callback('/ ([0-9]*):([0-9]*):([0-9]*)/', function($p){
return " ".str_pad($p[1], 2, 0, STR_PAD_LEFT).":".str_pad($p[2],2 , 0, STR_PAD_LEFT).":".str_pad($p[3],2, 0, STR_PAD_LEFT);
}, $some_bad_date_string);
这将把";2021年4月11日6:5:0";至";2021年4月11日06:05:00";。