将日期/时间字符串分解为assoc数组 [英] Explode date/time string into assoc array
问题描述
我有一个从数据库选项返回的字符串,该字符串允许用户设置其日期格式:
I have a string returning from a DB option, that allows the user to set her/his date format:
F j, Y
这将(对于今天)然后在我的 get_date()
函数:
which would (for today) then return the following value in my get_date()
function:
string 'September 24, 2012' (length=18)
现在我需要一个可拆分字符串的关联数组:
Now I need an associative array that splits that string:
array(
'day' => 24,
'month' => 'September',
'year' => 2012
)
由于用户不知道如何设置日期,我有一个问题,我不能简单地说 F =月,j =天,Y =年
。 php date()
函数的所有内容允许,在系统中允许。
As don't know how the user sets her/his date, I have the problem that I can't simply say F = month, j = day, Y = year
. Everything that the php date()
function allows, is allowed in the system.
问题:我该如何匹配那些?有没有我监督过的本地功能,可以告诉我某个东西是月/日/年/小时/小时还是...?
Question: How can I "match" those? Is there some native function that I've overseen, that tells me if something is a month/day/year/hour/...?
修改:最大我可以使用的PHP版本是PHP 5.2.1
The max. PHP version I can use is PHP 5.2.1
推荐答案
如果您使用的是PHP 5.3或更高版本,请使用 DateTime :: createFromFormat
:
If you have PHP 5.3 or later, use DateTime::createFromFormat
:
$date = DateTime::createFromFormat($inputFormat, $gotDate);
$result = array(
'day' => (int)$date->format('j'),
'month' => $date->format('F'),
'year' => (int)$date->format('Y')
);
这篇关于将日期/时间字符串分解为assoc数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!