【问题标题】:How to convert Icelandic months names to English moths names?如何将冰岛月份名称转换为英文飞蛾名称?
【发布时间】:2018-12-09 21:33:03
【问题描述】:

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

$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 

有没有办法将这些月份名称翻译或转换为英文月份名称?

【问题讨论】:

标签: php regex date datetime


【解决方案1】:

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多