如何将冰岛的月份名称转换为英语的飞蛾名称?

时间:2018-07-01 11:32:18

标签: php regex date datetime

我有一些日期变量包含冰岛的月份名称,例如:

$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. j\xC3\xBAn\xC3\xAD 2018...') #1 {main} thrown 

是否可以将这些月份的名称转换或转换为英文月份的名称?

1 个答案:

答案 0 :(得分:0)

我想到了这个,它需要安装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

希望这会有所帮助。