我在日期中使用类似7 october 2019
的字符串,但用波兰语表示7 sierpnia 2019
,我想将其更改为日期(Y-m-d)
我做了:
$day = '7 sierpnia 2019';
setlocale( LC_TIME, array('pl_PL.UTF-8','pl_PL@euro', 'pl_PL', 'polish'));
$date = DateTime::createFromFormat( 'j F Y', $day);
echo $date->format( 'Y-m-d');
但我收到此错误:
Fatal error: Call to a member function format() on boolean
我需要将其以Y-m-d
格式插入数据库。
[不确定这是否足以满足您的用例,但是下面的代码显示了一种方法,先将波兰的月份名称(假定为全名)翻译为英语,然后进行日期转换(sandbox demo:]]]
<?php $day = '7 sierpnia 2019'; $effective_day = preg_replace_callback('/(\s+)((stycz|lut|mar|kwie|maj|czerw|lip|sierp|wrzes|pazdzier|listopad|grud).*?)(\s+)/i', function ($matches) { switch ($matches[3]) { case 'stycz': return 'January'; case 'lut': return 'February'; case 'mar': return 'March'; case 'kwie': return 'April'; case 'maj': return 'May'; case 'czerw': return 'June'; case 'lip': return 'July'; case 'sierp': return 'August'; case 'wrzes': return 'September'; case 'pazdzier': return 'October'; case 'listopad': return 'November'; case 'grud': return 'December'; } return $matches[0]; }, $day); $date = DateTime::createFromFormat('j F Y', $effective_day); echo $date->format( 'Y-m-d');
参考:https://www.ics.uci.edu/~dan/genealogy/Miller/resource/polmonth.htm
IntlDateFormatter的通用解决方案。需要intl extension installed(即Debian / Ubuntu为sudo apt-get install php-intl
)。