我有以下时间格式:15/08/2011 12:32:23
日/月/年时:分:秒,我想转换为以下格式:Ymd H:i:s
我试过了,date('Y-m-d H:i:s', strtotime($time))
但它不起作用。它交换从字符串转换为日期数字的月份和日期。
我有以下时间格式:15/08/2011 12:32:23
日/月/年时:分:秒,我想转换为以下格式:Ymd H:i:s
我试过了,date('Y-m-d H:i:s', strtotime($time))
但它不起作用。它交换从字符串转换为日期数字的月份和日期。
strtotime
理解美国 ( mm/dd/YYYY
) 和欧洲 ( dd-mm-YYYY or dd.mm.YYYY
) 格式。您使用斜线来分隔日、月和年,这就是为什么您的日期被解释为美国的原因。要解决此问题,请将斜杠替换为破折号。
在这种情况下,您可以非常简单地从字符串中交换月份和日期:
$time_string = "15/08/2011 12:32:23";
$strtotime = explode("/",$time_string);
$strtotime = implode("/",array($strtotime[1], $strtotime[0], $strtotime[2]));
echo date('Y-m-d H:i:s', strtotime($strtotime));
你将不得不重新分析时间。strtotime 认为您正在尝试以 的格式输入字符串'm/d/Y H:i:s'
,但您正在提供'd/m/Y H:i:s'
.
list($date, $times) = explode(' ', $time);
list($day, $month, $year) = explode('/', $date);
$newTime = date('Y-m-d H:i:s', strtotime("$month/$day/$year $times");
在日期中用连字符替换斜杠,然后尝试它会起作用。
$a = '07/08/2019'; // 07 is day 08 is month.
echo date('Y-m-d', strtotime($a)); //output: 2019-07-08 where 07 became month.
$a = str_replace("/","-","07/08/2019"); // 07-08-2019
echo date('Y-m-d', strtotime($a)); //2019-08-07