[英]Convert Month Name into Number With Different Languages?
I want to convert Month Name into Number format with different Languages. 我想将月份名称转换为具有不同语言的数字格式。 I have written one code that code is working on my localhost but its not working on live server its returning false value.
我编写了一个代码,该代码可在我的本地主机上运行,但不能在实时服务器上运行,它返回假值。 And also for some months its working and its not working for Dezembro , outubro its not working.
而且几个月以来,它对于Dezembro 仍然有效,并且不起作用,而outubro则不起作用。 How i can solve this issue ?
我该如何解决这个问题? Could you please help me to solve this one ??
您能帮我解决这个问题吗?
$visit_date = "7 Dezembro, 2019";
$split_date = explode(" ", $visit_date);
$visit_day = sprintf("%02d", $split_date[0]);
$visit_month = $split_date[1];
if (substr($visit_month, -1) == ',')
{
$visit_month = substr($visit_month, 0, -1);
$visit_year = $split_date[2];
}
else {
$visit_year = $split_date[3];
}
var_dump($visit_month);
$visit_month = month_to_number($visit_month, 'pt_PT');
var_dump($visit_month);
// convert visit date month full name to month number for all languages
function month_to_number($month, $locale_set= 'pt_PT' )
{
$month = mb_convert_case($month, MB_CASE_LOWER, 'UTF-8');
$locale =
setlocale(LC_TIME, '0');
setlocale(LC_TIME, $locale_set.'.UTF-8');
$month_number = FALSE;
for ($i = 1; $i <= 12; $i++)
{
$time_month = mktime(0, 0, 0, $i, 1, 1970);
$short_month = date('M', $time_month);
$short_month_lc = strftime('%b', $time_month);
if (stripos($month, $short_month) === 0 OR
stripos($month, $short_month_lc) === 0)
{
$month_number = sprintf("%02d", $i);
break;
}
}
setlocale(LC_TIME, $locale); // return locale back
return $month_number;
}
If you can integrate the external class dt try this: 如果您可以集成外部类dt,请尝试以下操作:
$visit_date = "7 Dezembro, 2019";
dt::setDefaultLanguage('pt');
$dt = dt::create($visit_date);
echo $dt->format('Y-m-d');//2019-12-07
Internally, the class creates a translation table for every month when setting the language. 在内部,该类在设置语言时会为每个月创建一个翻译表。 The IntlDateFormatter class is used for this.
IntlDateFormatter类用于此目的。
Note: The IntlDateFormatter class works independently of the server's local settings. 注意:IntlDateFormatter类的工作独立于服务器的本地设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.