PHP去掉日期里面,日月前面的0方法

原创 PHP技巧
阅读数: 818 2018年09月26日

1、正则去掉日期日月前面的0

<?php
$arr = [
	'2019-09-03',
	'2019年09月01日',
	'2019/09/04',
];

$rule = '/\b0/';

foreach($arr as $str){
	$newstr = preg_replace($rule, "", $str);
	var_export($newstr);
}

<<<EOD
2019-9-3
2019年9月1日
2019/9/4
EOD;



2、日期格式化方法(不能处理:2019年09月01日格式的)

$arr = [
	'2019-09-03',
	'20190901',
];

$rule = '/\b0/';

foreach($arr as $str){
	$newstr = date('Y-n-j', strtotime($str));
	var_export($newstr);
}

<<<EOD
2019-9-3
2019/9/4
EOD;


phpriji.cn | 网站地图 | 沪ICP备17015433号-1