解析日期到零件的功能 [英] Function to parse date to parts

查看:146
本文介绍了解析日期到零件的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中有什么好的功能可以将日期解析成指定格式的部分吗?

Is there any nice function in PHP to parse the date into parts given the specified format?

some_function('20112000', 'dmY') 
some_function('2000-11-20', 'Y-m-d')
some_function('2000 text 11-20', 'Y text m-d')

array('d'=> 20,'m'=> 11,'Y'=> 2000)案例,

(假设格式与 date()函数兼容)

我使用PHP 5.1.9

这也可以使用 Zend_Date ,但它使用不同的格式比 date()(例如 ddMMyyyy

I use PHP 5.1.9
This is also possible using Zend_Date, but it uses different formats than date() (e.g. ddMMyyyy)

推荐答案

使用PHP> = 5.3, DateTime :: createFromFormat() 可以根据指定的格式解析日期。

With PHP >= 5.3, the DateTime::createFromFormat() allows one to parse a date according to a specified format.

之后,您将获得 DateTime 对象,最多可以使用它来获取您想要的数组。

After that, you get a DateTime object, and up to your to work with it in order to get the array you want.

这篇关于解析日期到零件的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆