在PHP中转换日期格式

问题描述:

我正在尝试将日期从yyyy-mm-dd转换为dd-mm-yyyy(但不在SQL中);但是我不知道date函数如何需要时间戳,并且无法从该字符串获取时间戳.

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.

这怎么可能?

使用strtotime()date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(请参见 strtotime 日期文档.)

请注意,这是对原始问题的快速解决方案.要进行更广泛的转换,您实际上应该使用 DateTime 类进行解析和格式:-)

Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format :-)