【问题标题】:Convert date from dutch to english将日期从荷兰语转换为英语
【发布时间】:2015-07-27 11:33:49
【问题描述】:

我以为这会很容易,但事实并非如此! 我有日期: mei 28, 2015 (dutch) 我想把它转换成英文 Y-m-d

问题是站点是多语言的,所以我希望它保持动态(将 mai 替换为可能)不是解决方案,我已经尝试过:

setlocale(LC_TIME, 'NL_nl');
setlocale(LC_ALL, 'nl_NL'); 
echo strftime("%Y-%m-%d", strtotime($value));

或

 $date = date_parse(($value));
  print_r($date);

但我一直在得到: 1970-01-01

有什么想法吗?

我正在尝试在 magento 中实现这一点,所以也许有这个 magento 功能

【问题讨论】:

  • 您是否将 $value 作为字符串 'mei 28,2015' 传递? strtotime 恐怕只支持英文。
  • 是的 $value 是 mei 28,2015,我正在尝试找到将其转换为 englsih 的方法
  • 您是否考虑过仅对本月进行字符串替换。只有 12 个,一个带有案例的简单函数就可以做到这一点。翻译后 strtotime 等应该可以工作。
  • @TheHumbleRat 12 只代表一种语言,网站是多语言的
  • @dfg5rte4 aha 在这种情况下我的解决方案会失败。我有兴趣看到答案。

标签: php magento date


【解决方案1】:

使用 strptime() 函数 - http://php.net/manual/en/function.strptime.php

$value = 'mei 28, 2015';
$format = '%B %d, %Y';
setlocale(LC_TIME, 'NL_nl');    
setlocale(LC_ALL, 'nl_NL');

// get the array with date details 
$result = strptime($value, $format);
$day = str_pad($result['tm_mday'] + 1, 2, '0', STR_PAD_LEFT);
$month = str_pad($result['tm_mon'] + 1, 2, '0', STR_PAD_LEFT);
$year = $result['tm_year'] + 1900;

print $year.'-'.$month.'-'.$day;

【讨论】:

  • strftime('%F %d %Y') 返回 2015-05-15 15 2015
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 2014-03-09
相关资源
最近更新 更多