我有一些包含冰岛月份名称的日期变量,例如:
$date = '27. júní 2018 04:53'
我想比较日期,但是当我尝试这样做时:
strtotime('27. júní 2018 04:53');
我得到空白结果。
当我尝试时:
new DateTime('27. júní 2018 04:53');
我收到一个错误:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct():
Failed to parse time string (27. júní 2018 04:53) at position 0 (2):
Unexpected character'
Stack trace: #0: DateTime->__construct('27. jxC3xBAnxC3xAD 2018...') #1 {main} thrown
有没有办法将这些月份名称翻译或转换为英文月份名称?
我想出了这个,它需要安装php7.0-intl
:
<?php
$input = '27. júní 2018 04:53';
$locale = 'is_IS';
$date_type = IntlDateFormatter::FULL;
$time_type = IntlDateFormatter::FULL;
$timezone = 'Atlantic/Reykjavik';
$calendar = IntlDateFormatter::GREGORIAN;
$pattern = 'd. MMMM yyyy HH:ss'; // see http://userguide.icu-project.org/formatparse/datetime
$formatter = new IntlDateFormatter($locale, $date_type, $time_type, $timezone, $calendar, $pattern);
$timestamp = $formatter->parse($input);
$output = DateTime::createFromFormat('U', $timestamp)->format('d F Y H:i:s');
echo $output;
这为您提供:
27 June 2018 04:00:53
希望这有帮助。