PHP-使用区域设置字符串解析日期时间 [英] PHP - Parse datetime with locale strings

查看:82
本文介绍了PHP-使用区域设置字符串解析日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析日期时间,例如"Ayer,16:08"(西班牙语为昨天,16:08").

I want to parse datetimes like 'Ayer, 16:08' which is 'Yesterday, 16:08' in spanish.

我已经尝试过了

$dateString = 'Ayer, 16:08';
setlocale(LC_ALL, 'es');
$time = strtotime($dateString);
echo date('d-m-Y H:i', $time);

但回声

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);

语言环境有问题吗?

谢谢

推荐答案

在创建日期之前,您需要将其翻译成英文.

You'll need to translate it into English before making the date.

创建一个包含西班牙语单词的数组,并创建一个具有相应英语翻译的数组,这是PHP可以识别的.然后只需使用$ dateString运行str_ireplace().

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');

这篇关于PHP-使用区域设置字符串解析日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆