PHP转换日期格式dd / mm / yyyy => yyyy-mm-dd [英] PHP convert date format dd/mm/yyyy => yyyy-mm-dd
问题描述
可能重复:
PHP日期字符串格式
我正在尝试将日期从 dd / mm / yyyy => yyyy-mm-dd
。我使用了mktime()函数和其他函数,但我似乎无法使其工作。我使用'/'
作为分隔符设置了爆炸
原始日期,但是我没有成功更改格式和将'/'
与' - '
交换。
I am trying to convert a date from dd/mm/yyyy => yyyy-mm-dd
. I have using the mktime() function and other functions but I cannot seem to make it work. I have managed to explode
the original date using '/'
as the delimiter but I have no success changing the format and swapping the '/'
with a '-'
.
任何帮助将不胜感激。
Any help will be greatly appreciated.
推荐答案
日期在
m / d / y
或dmy
格式通过在各种组件之间的分隔符处查找
来消除歧义:如果分隔符是
斜杠(/
),则假定为美国m / d / y
而如果
分隔符是破折号(-
)或点(。
),那么欧洲dmy
格式。请此处。
Dates in the
m/d/y
ord-m-y
formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/
), then the Americanm/d/y
is assumed; whereas if the separator is a dash (-
) or a dot (.
), then the Europeand-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));
这篇关于PHP转换日期格式dd / mm / yyyy => yyyy-mm-dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!