vascowhite/datefactory

DateTime 对象的工厂类。

v1.1.1 2016-10-12 10:22 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:25:54 UTC


README

#DateFactory 用于创建 \DateTime 对象的工厂,并检查提供的日期字符串是否与提供的格式匹配。

##安装 使用 composer 安装,在 composer.json 中添加以下内容:

"require": {
    "vascowhite/datefactory": "v1.1.x"
}

##原因 因为 \DateTime::createFromFormat() 会做愚蠢的事情,而我需要一个对用户提供的日期进行服务器端验证的方法。

##DateFactory 只有一个公开的方法

####DateFactory::getDate() 签名:

DateTime DateFactory::getDate(String $date, String $format, [String $timeZone])

参数 $date 是表示日期/时间的字符串,例如 '2014-14 14:12:10'。 $format 格式字符串。可用格式是 \DateTime::createFromFormat() 可用的任何格式。 $timeZone 可选。来自 此列表 的有效时区标识符。

示例

$dateTime = DateFactory::getDate('2014-12-25 12:00:00',  'Y-m-d H:i:s', 'Europe/London');

返回值 返回一个 DateTime 对象或在失败时返回 false。