php - 如何将其他语言的日期格式转换为英文格式(Y-m-d)?
我在搜刮一个博客网站,之前一切都很正常,但是现在博客网站改成了多语言,现在搜刮日期和时间都出现了问题。所以帮我解决这个问题。
之前我曾经用不同的方式从博客网站上获取日期格式,如下图。
Thursday, 31 October 2019
Thursday, September 10, 2020
这对我来说很好,因为我可以用下面的方式转换到PHP中。
$fetched_date = $info['date']; // which is 'Thursday, 31 October 2019'
$time_stamp = strtotime($fetched_date);
$new_date = date('Y-m-d', $time_stamp);
// It was gave me this date '2019-10-31',
// which is perfect for me, because i can store it to my DB.
但自从博客网站改成多语言后,我偶尔会得到另一种语言的日期,如下所示。那我该如何转换呢?
गुरुवार, १८ जून, २०२० // mr_IN
ગુરુવાર, 10 સપ્ટેમ્બર, 2020 // gu_IN
所以当我用这种日期格式尝试的时候,它给我的日期是错误的,我根本用不上。 比如。
$fetched_date = $info['date']; // which is 'गुरुवार, १८ जून, २०२०'
$time_stamp = strtotime($fetched_date);
$new_date = date('Y-m-d', $time_stamp);
// It was gave me this date '1970-01-01', which is wrong and i can't store to my DB.
// it should be "2020-06-18"
而当我做刮痧的时候,也会得到一个语言代码,比如。mr_IN
, gu_IN
我想在PHP中转换日期,并将其存储在MySQL中,我该怎么做呢?
【回答】:
IntlDateFormatter类可以在这里帮助我。但是,日期格式必须适合。
$dateString = 'गुरुवार, १८ जून, २०२०';
$timeZone = new DateTimeZone("UTC");
$fmt = new IntlDateFormatter(
'mr_IN',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
$timeZone,
IntlDateFormatter::GREGORIAN
);
$ts = $fmt->parse($dateString);
$dateTime = date_create("today",$timeZone)->setTimeStamp($ts);
echo $dateTime->format("Y-m-d"); //2020-06-18