date_create_from_format等价于PHP 5.2(或更低版本) [英] date_create_from_format equivalent for PHP 5.2 (or lower)
问题描述
我正在本地机器上使用PHP 5.3,需要解析英国的日期格式(dd / mm / yyyy)。我发现 strtotime
并没有使用该日期格式,所以我使用 date_create_from_format
而不是这样。 / p>
现在,我的问题是我的登台服务器正在运行PHP 5.2,而 date_create_from_format
在该版本上无效。 (它是一个共享的服务器,不会有一个线索如何升级到PHP 5.3)
所以有一个类似的功能 date_create_from_format可以使用的
定制方法或PHP本机?
如果 strptime
不可用那么这里是一个不同的想法。它类似于Col.Shrapnel的方法,而是使用 sscanf
将日期部分值解析成变量,并使用它们构造一个新的 DateTime
对象
列表($ day,$ month,$ year)= sscanf('12 / 04 / 2010','%02d /%02d /%04d');
$ datetime = new DateTime($ year- $ month- $ day);
echo $ datetime-> format('r');
I'm working with PHP 5.3 on my local machine and needed to parse a UK date format (dd/mm/yyyy). I found that strtotime
didn't work with that date format, so I used date_create_from_format
instead - which works great.
Now, my problem is that my staging server is running PHP 5.2, and date_create_from_format
doesn't work on that version. (It's a shared server, and wouldn't have a clue how to upgrade it to PHP 5.3)
So is there a similar function to date_create_from_format
that I can use? Bespoke or PHP native?
If strptime
is not available to you, then here is a different idea. It is similar to Col. Shrapnel's approach but instead uses sscanf
to parse the date-part values into variables and uses those to construct a new DateTime
object.
list($day, $month, $year) = sscanf('12/04/2010', '%02d/%02d/%04d');
$datetime = new DateTime("$year-$month-$day");
echo $datetime->format('r');
这篇关于date_create_from_format等价于PHP 5.2(或更低版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!