简体   繁体   English

如何从 PHP 中的缩写非英语格式获取标准化日期/时间?

[英]How to get standardised date/time from abbreviated non-English format in PHP?

I have an input date string of something like Sett21 , which is in Italian, and I need to get a standardized format such as 01/09/2021.我有一个类似Sett21的输入日期字符串,它是意大利语的,我需要得到一个标准化的格式,比如 01/09/2021。

Is there a way to create a date from an abbreviated non-English string?有没有办法从缩写的非英语字符串创建日期? I'm attempting to use Carbon and it can handle a date string abbreviated in English fine:我正在尝试使用 Carbon,它可以处理英文缩写的日期字符串:

$date = Carbon::create("Sept2021");

Output: Wednesday, September 1, 2021 12:00 AM输出: Wednesday, September 1, 2021 12:00 AM

However it fails the Italian abbreviated month Sett .但是它失败了意大利语缩写的月份Sett

Solution via an array with the Italian month names as in CBroe's comment.通过带有意大利月份名称的数组解决方案,如 CBroe 的评论中所示。

$input = 'Sett21';

$list = 'genn,febbr,mar,apr,magg,giugno,luglio,ag,sett,ott,nov,dic';
foreach(explode(',',$list) as $key => $month){
  $in = preg_replace('~'.$month.'~iu',($key+1)." ",$input);
  if($in !== $input) break;
}

$date = Carbon::createFromFormat('!m y',$in);
echo $date;
//or
$dateTime = DateTime::createFromFormat('!m y',$in);
echo "\n";
echo $dateTime->format('Y-m-d H:i:s');

Output:输出:

2021-09-01 00:00:00
2021-09-01 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM