且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP 转换日期格式 dd/mm/yyyy =>yyyy-mm-dd

更新时间:2022-05-04 23:03:09

m/d/yd-m-y 格式的日期通过查找消除歧义在各个组件之间的分隔符处:如果分隔符是一个斜线(/),则假定为美国m/d/y;而如果分隔符是破折号 (-) 或点 (.),然后是欧洲的 d-m-y假定格式.查看更多信息.

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. Check more here.

使用默认的日期函数.

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );

编辑 我刚刚测试了它,不知何故,PHP 不能很好地处理 dd/mm/yyyy 格式.这是另一种解决方案.

EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.

$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));