[英]How do I convert another language date format to English language format (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 class 可以在這里提供幫助。 但是,日期格式必須合適。
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.