cerbero / date
一个无框架且易于使用的日期处理工具。
Requires
- php: >=5.4.0
Requires (Dev)
- phpspec/phpspec: ~2.0
README
一个无框架且易于使用的日期处理工具。
一键安装
从您项目的根目录的终端运行以下命令
composer require cerbero/date
准备
如果您是Laravel用户,您可以在 app/config/app.php 中添加以下别名以在所有地方使用此工具
'aliases' => array( 'Date' => 'Cerbero\Date', )
否则,在您想要使用它的类顶部添加此use
语句
use Cerbero\Date;
用法
创建一个或多个DateTime实例
允许将字符串、数组和DateTime对象作为参数。您还可以传递无限多个参数。
根据给定参数的数量返回一个或多个DateTime实例。
// pass any English textual date to get an instance of DateTime $DateTime = Date::create('10 May 2000'); // you can pass an array as argument to create an array of DateTime objects $array = Date::create(array('now', 'next Thursday')); // or even unlimited arguments, an array is returned as well $array = Date::create('2 March 2010', 'now', 'last Tuesday');
检查一个字符串是否为有效日期
$true = Date::isValid('5 October 1980'); // 13 is recognized as month in English dates, so it's not valid $false = Date::isValid('13/01/2005');
格式化一个或多个日期
允许将字符串、数组或DateTime对象作为第一个参数。
根据给定参数的数量返回一个或多个格式化的字符串。
// '1999-01-30' $string = Date::format('30 January 1999', 'Y-m-d'); // '1st April, 2014' $string = Date::format(new DateTime('2014-04-01'), 'jS F, Y'); // array('12/11/10', '01/04/14') $array = Date::format(array('Nov-12-10', new DateTime('2014-04-01')), 'd/m/y');
计算两个日期之间的差距
允许将字符串和DateTime对象作为参数。日期的顺序无关紧要。
虽然gap()
方法返回一个包含差距详情的数组,但其他方法返回一个整数
// array('years' => 0, 'months' => 11, 'days' => 30, 'hours' => 14, 'minutes' => 48, 'seconds' => 57) $array = Date::gap('2000-01-01 09:11:03', '1 January 2001'); // 3600 $int = Date::gapInSeconds('January 1st, 2000', '2000-01-01 01:00:00'); // 60 $int = Date::gapInMinutes(new DateTime('2000-01-01'), '2000-01-01 01:00:00'); // 0 $int = Date::gapInHours('2000-01-01 00:59:00', '2000-01-01 00:00:00'); // 14 $int = Date::gapInDays('last week', 'next week'); // 13 $int = Date::gapInMonths('August 2014', '2013 July'); // 9 $int = Date::gapInYears('5 Jan 2010', new DateTime('2000-02-05'));
计算一个或多个日期的时间戳
允许将字符串、数组或DateTime对象作为参数。
根据给定参数的数量返回一个或多个整数时间戳。
// 1214870400 (GMT) $int = Date::timestamp('July 1st, 2008'); // 1276560000 (GMT) $int = Date::timestamp(new DateTime('2010-06-15')); // array(1214870400, 1276560000) (GMT) $array = Date::timestamp(array('July 1st, 2008', new DateTime('2010-06-15')), 'd/m/y');
比较两个日期
允许将字符串和DateTime对象作为参数。它可以比较它们的类型。
// check if the first date is Less Than the second one $false = Date::lt('2007 June', new DateTime('2007-06-01')); // check if the first date is Less Than or Equals the second one $true = Date::lte(new DateTime('2007-06-01'), '2007 June'); // check if the first date EQuals the second one $true = Date::eq('2007 June', new DateTime('2007-06-01')); // check if the first date does Not EQual the second one $false = Date::neq(new DateTime('2007-06-01'), '2007 June'); // check if the first date is Greater Than or Equals the second one $true = Date::gte('2007 June', new DateTime('2007-06-01')); // check if the first date is Greater Than the second one $false = Date::gt(new DateTime('2007-06-01'), '2007 June');
找到最早的日期
允许将字符串、数组和DateTime对象作为参数。您还可以传递无限多个参数。
返回具有最早日期的字符串或DateTime实例。
// '03/10/1998' $string = Date::earliest(array('03/10/1998', new DateTime('2006-12-09'), 'yesterday')); // the DateTime with the date '2009-03-06' $DateTime = Date::earliest('next Wednesday', new DateTime('2009-03-06'), '19 Nov 2010');
找到最新的日期
允许将字符串、数组和DateTime对象作为参数。您还可以传递无限多个参数。
返回具有最新日期的字符串或DateTime实例。
// 'tomorrow' $string = Date::latest('tomorrow', '10 January 2015', new DateTime('2014-11-04')); // the DateTime with the date '2016-01-01' $DateTime = Date::latest(array('last Friday', new DateTime('2016-01-01'), '10/06/80'));
找到所有早于一个日期的日期
允许将字符串和DateTime对象作为第一个参数。
只返回作为第二个参数传递的字符串和DateTime实例,这些实例的日期早于第一个参数。
// array(DateTime('2000-09-16'), 'May 1st, 2009') $array = Date::before('yesterday', array(new DateTime('2000-09-16'), 'tomorrow', 'May 1st, 2009')); // array(DateTime('2009-03-06'), '19 Nov 2010') $array = Date::before(new DateTime('2014-01-01'), array(new DateTime('2009-03-06'), '19 Nov 2010', 'July 1st, 2015'));
找到所有晚于一个日期的日期
允许将字符串和DateTime对象作为第一个参数。
只返回作为第二个参数传递的字符串和DateTime实例,这些实例的日期晚于第一个参数。
// array(DateTime('2007-10-28'), 'next Monday') $array = Date::after('March 1879', array(new DateTime('2007-10-28'), 'next Monday', 'February 1879')); // array(DateTime('2003-02-14'), 'now') $array = Date::after(new DateTime('2002-02-20'), array(new DateTime('2003-02-14'), 'now', '12/31/2001'));
按升序排序日期
允许将字符串、数组和DateTime对象作为参数。您还可以传递无限多个参数。
返回一个数组,其中包含按升序排序的字符串和DateTime实例。
// array('Apr-17-1790', 'June 2008', new DateTime('2015-02-01')) $array = Date::sort('June 2008', new DateTime('2015-02-01'), 'Apr-17-1790'); // array(new DateTime('2014-11-22'), 'yesterday', 'next Wednesday') $array = Date::sort(array('next Wednesday', new DateTime('2014-11-22'), 'yesterday'));
按降序排序日期
允许将字符串、数组和DateTime对象作为参数。您还可以传递无限多个参数。
返回一个数组,其中包含按降序排序的字符串和DateTime实例。
// array(new DateTime('2015-02-01'), 'June 2008', 'Apr-17-1790') $array = Date::reverse('June 2008', new DateTime('2015-02-01'), 'Apr-17-1790'); // array('next Wednesday', 'yesterday', new DateTime('2014-11-22')) $array = Date::reverse(array('next Wednesday', new DateTime('2014-11-22'), 'yesterday'));
创建一个日期范围
允许将字符串和DateTime对象作为第一和第二个参数。您可以设置一个步长。
返回一个包含从起始日期到结束日期(包括)之间所有DateTime实例的数组。
// array of 22 DateTime instances, from 2008-06-10 to 2008-07-01 included $array = Date::range('10-June 2008', new DateTime('July 1st, 2008')); // array of 11 DateTime instances, only even dates from 2008-06-10 to 2008-06-30 included $array = Date::range('10-June 2008', new DateTime('July 1st, 2008'), 2);
检查一个日期是否在数组中
允许将字符串和DateTime对象作为第一个参数。
$true = Date::inArray('03/06/09', array(new DateTime('2009-03-06'), '19 Nov 2010')); $true = Date::inArray(new DateTime('2004-09-21'), array('last week', '21 Sep 2004')); $false = Date::inArray('now', array('yesterday', 'tomorrow'));