PHP - 使用区域设置字符串解析日期时间
我想解析诸如“Ayer,16:08”之类的日期时间,即西班牙语中的“昨天,16:08”。
我已经尝试过这个
$dateString = 'Ayer, 16:08';
setlocale(LC_ALL, 'es');
$time = strtotime($dateString);
echo date('d-m-Y H:i', $time);
,但它回显
01-01-1970 00:00
不过,如果我用英语字符串来做它,它工作得很好:
$dateString = 'Yesterday, 16:08';
$time = strtotime($dateString);
echo date('d-m-Y H:i', $time);
这是区域设置的问题吗?
谢谢
I want to parse datetimes like 'Ayer, 16:08' which is 'Yesterday, 16:08' in spanish.
I have tried this
$dateString = 'Ayer, 16:08';
setlocale(LC_ALL, 'es');
$time = strtotime($dateString);
echo date('d-m-Y H:i', $time);
but it echoes
01-01-1970 00:00
Nevertheless, if I do it with english strings it works just fine:
$dateString = 'Yesterday, 16:08';
$time = strtotime($dateString);
echo date('d-m-Y H:i', $time);
Is it a problem with locale?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如今,有 IntlDateFormatter 用于此目的,请参阅此 stackoverflow 答案:https://stackoverflow.com/a/32265594/682317
复制到这里:
These days there is IntlDateFormatter for this purpose, see this stackoverflow answer: https://stackoverflow.com/a/32265594/682317
Copied here:
在约会之前你需要将其翻译成英语。
创建一个包含西班牙语单词的数组,另一个包含 PHP 识别的相应英语翻译的数组。然后只需使用 $dateString 运行 str_ireplace() 即可。
像这样的东西应该有效:
You'll need to translate it into English before making the date.
Create an array with the Spanish words, and another with the corresponding English translations, as recognised by PHP. Then simply run str_ireplace() with $dateString.
Something like this should work:
在 手册 中,我看不到任何有关其他语言的信息。所以,你需要翻译它,正如 Zumi 所说
In Manual I can't see anything about others languages. So, you need translate it, as Zumi said