PHP - 使用区域设置字符串解析日期时间

发布于 2024-11-28 02:12:04 字数 454 浏览 1 评论 0原文

我想解析诸如“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-12-05 02:12:05

如今,有 IntlDateFormatter 用于此目的,请参阅此 stackoverflow 答案:https://stackoverflow.com/a/32265594/682317

复制到这里:

这就是答案:

$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);

这是之前使用我的答案进行的测试。

<前><代码>\r\n";
$日期=“01/02/2015”; //1月2日
$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Ymd');
echo "
\r\n";

echo "IT 区域设置
\r\n";
$日期=“01/02/2015”; //2月1日
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Ymd');
echo "
\r\n";

不幸的是,我无法获得赏金...:-)

These days there is IntlDateFormatter for this purpose, see this stackoverflow answer: https://stackoverflow.com/a/32265594/682317

Copied here:

This is the answer:

$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);

And this is the previous test working with my answer.

<?php
echo "EN locale<br>\r\n";
$date="01/02/2015"; //2th Jan
$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Y-m-d');
echo "<br>\r\n";

echo "IT locale<br>\r\n";
$date="01/02/2015"; //1th Feb
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Y-m-d');
echo "<br>\r\n";

Unfortunately I cannot earn my bounty... :-)

方圜几里 2024-12-05 02:12:04

在约会之前你需要将其翻译成英语。

创建一个包含西班牙语单词的数组,另一个包含 PHP 识别的相应英语翻译的数组。然后只需使用 $dateString 运行 str_ireplace() 即可。

像这样的东西应该有效:

$spanish = array("spanish1", "spanish2", "spanish3");
$english = array("en_translation_of_spanish1", "en_translation_spanish2", "en_translation_of_spanish3");
$dateString = str_ireplace($spanish, $english, 'Ayer, 16:08');

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:

$spanish = array("spanish1", "spanish2", "spanish3");
$english = array("en_translation_of_spanish1", "en_translation_spanish2", "en_translation_of_spanish3");
$dateString = str_ireplace($spanish, $english, 'Ayer, 16:08');
情绪操控生活 2024-12-05 02:12:04

手册 中,我看不到任何有关其他语言的信息。所以,你需要翻译它,正如 Zumi 所说

In Manual I can't see anything about others languages. So, you need translate it, as Zumi said

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文