介绍
<?php
namespace Carbon;
class Carbon extends \DateTime
{
// code here
}
使用
-
Carbon::parse()
解析 DateTime 默认显示当前时间,可传入时间格式字符串
Carbon::setLocale('de');
设置时区 中国就是'zh'
echo Carbon::now()->addYear()->diffForHumans(); // in 1 Jahr
Carbon::now();
参数传时区,默认是本地设置的时区Carbon::setTestNow($knownDate);
设置模式,设置好后,Carbon::now()
就是设置的时间值Carbon::parse('first day of March 2000')
参数类似
first day of December 2000
first day of this month
last day of next month
first day of previous month还可以这样玩
echo new Carbon('tomorrow'); // 2001-05-22 00:00:00 ... notice the time !
echo new Carbon('yesterday'); // 2001-05-20 00:00:00
echo new Carbon('next wednesday'); // 2001-05-23 00:00:00
echo new Carbon('last friday'); // 2001-05-18 00:00:00
echo new Carbon('this thursday'); // 2001-05-24 00:00:00获取时区
echo Carbon::now()->timezoneName; // America/Toronto
echo Carbon::now()->tzName; // America/TorontoDateTime类。这些结果都可以用
Carbon::parse()
解析出来
$dt = Carbon::now();
// $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
echo $dt->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $dt->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $dt->toIso8601String(); // 1975-12-25T14:15:16-0500
echo $dt->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $dt->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $dt->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $dt->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $dt->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toW3cString(); // 1975-12-25T14:15:16-05:00
- 一些简单的判断,返回
true
orfalse
$dt = Carbon::now();
echo $dt->isWeekday(); // 是不是周末
echo $dt->isWeekend(); // 是不是工作日
echo $dt->isYesterday(); //是不是昨天
echo $dt->isToday(); // 是不是今天
echo $dt->isTomorrow(); // 明天
echo $dt->isFuture(); // 未来,只要时间比现在晚不管是明天后天都是true
echo $dt->isPast(); // 过去,时间比现在早都是true
echo $dt->isLeapYear(); // 闰年
- 比较两个时间的大小
// 取 min
$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
echo $dt1->min($dt2); // 2012-01-01 00:00:00
// 取 max
$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
echo $dt1->max($dt2); // 2014-01-30 00:00:00
// now is the default param 默认当前时间
$dt1 = Carbon::create(2000, 1, 1, 0, 0, 0);
echo $dt1->max();
- 加法和减法
$dt = Carbon::create(2012, 1, 31, 0);
echo $dt->toDateTimeString(); // 2012-01-31 00:00:00
// 年
echo $dt->addYears(5); // 2017-01-31 00:00:00
echo $dt->addYear(); // 2018-01-31 00:00:00
echo $dt->subYear(); // 2017-01-31 00:00:00
echo $dt->subYears(5); // 2012-01-31 00:00:00
// 月
echo $dt->addMonths(60); // 2017-01-31 00:00:00
echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps
echo $dt->subMonth(); // 2017-02-03 00:00:00
echo $dt->subMonths(60); // 2012-02-03 00:00:00
// 日
echo $dt->addDays(29); // 2012-03-03 00:00:00
echo $dt->addDay(); // 2012-03-04 00:00:00
echo $dt->subDay(); // 2012-03-03 00:00:00
echo $dt->subDays(29); // 2012-02-03 00:00:00
// 星期
echo $dt->addWeekdays(4); // 2012-02-09 00:00:00
echo $dt->addWeekday(); // 2012-02-10 00:00:00
echo $dt->subWeekday(); // 2012-02-09 00:00:00
echo $dt->subWeekdays(4); // 2012-02-03 00:00:00
// 周
echo $dt->addWeeks(3); // 2012-02-24 00:00:00
echo $dt->addWeek(); // 2012-03-02 00:00:00
echo $dt->subWeek(); // 2012-02-24 00:00:00
echo $dt->subWeeks(3); // 2012-02-03 00:00:00
// 小时
echo $dt->addHours(24); // 2012-02-04 00:00:00
echo $dt->addHour(); // 2012-02-04 01:00:00
echo $dt->subHour(); // 2012-02-04 00:00:00
echo $dt->subHours(24); // 2012-02-03 00:00:00
// 分钟
echo $dt->addMinutes(61); // 2012-02-03 01:01:00
echo $dt->addMinute(); // 2012-02-03 01:02:00
echo $dt->subMinute(); // 2012-02-03 01:01:00
echo $dt->subMinutes(61); // 2012-02-03 00:00:00
// 秒
echo $dt->addSeconds(61); // 2012-02-03 00:01:01
echo $dt->addSecond(); // 2012-02-03 00:01:02
echo $dt->subSecond(); // 2012-02-03 00:01:01
echo $dt->subSeconds(61); // 2012-02-03 00:00:00
- 比较两个时间的差值
echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0
// 创建时间
$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
echo $dtOttawa->diffInHours($dtVancouver); // 3
// 相差几小时
echo $dtOttawa->diffInHours($dtVancouver, false); // 3 第二个参数是可选的,再次表明如果你想返回值绝对值还是相对值,可能有一个-(负面)签署如果传入日期小于当前实例。 这将默认为true,返回绝对值。 比较是在UTC。
echo $dtVancouver->diffInHours($dtOttawa, false); // -3
// 相差几天
$dt = Carbon::create(2012, 1, 31, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 31
echo $dt->diffInDays($dt->copy()->subMonth(), false); // -31
// 相差几天
$dt = Carbon::create(2012, 4, 30, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 30
echo $dt->diffInDays($dt->copy()->addWeek()); // 7
// 相差几分钟
$dt = Carbon::create(2012, 1, 1, 0);
echo $dt->diffInMinutes($dt->copy()->addSeconds(59)); // 0
echo $dt->diffInMinutes($dt->copy()->addSeconds(60)); // 1
echo $dt->diffInMinutes($dt->copy()->addSeconds(119)); // 1
echo $dt->diffInMinutes($dt->copy()->addSeconds(120)); // 2
// 相差几天
echo $dt->addSeconds(120)->secondsSinceMidnight(); // 120
- 时间过滤
// 筛选出这段时间内有几个周末
$dt = Carbon::create(2014, 1, 1);
$dt2 = Carbon::create(2014, 12, 31);
$daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) {
return $date->isWeekend();
}, $dt2);
echo $daysForExtraCoding; // 104
// 算一天分钟数为 0 的数量(可以自定义时间间隔)
$dt = Carbon::create(2014, 1, 1)->startOfDay();
$dt2 = $dt->copy()->endOfDay();
$littleHandRotations = $dt->diffFiltered(CarbonInterval::minute(), function(Carbon $date) {
return $date->minute === 0;
}, $dt2);
echo $littleHandRotations; // 24
// 其他的一些过滤函数
// others that are defined
// diffInYears(), diffInMonths(), diffInWeeks()
// diffInDays(), diffInWeekdays(), diffInWeekendDays()
// diffInHours(), diffInMinutes(), diffInSeconds()
// secondsSinceMidnight(), secondsUntilEndOfDay()
- 常见的表达方法
// The most typical usage is for comments
// The instance is the date the comment was created and its being compared to default now()
echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago
echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 seconds from now
echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago
echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 weeks
- 天始,天末 月始,月末 年始,年末 一个十年始,十年末(2010-2020)
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfDay(); // 2012-01-31 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfDay(); // 2012-01-31 23:59:59
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfMonth(); // 2012-01-01 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfMonth(); // 2012-01-31 23:59:59
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfYear(); // 2012-01-01 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfYear(); // 2012-12-31 23:59:59
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfDecade(); // 2010-01-01 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfDecade(); // 2019-12-31 23:59:59
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfCentury(); // 2000-01-01 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfCentury(); // 2099-12-31 23:59:59
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfWeek(); // 2012-01-30 00:00:00
var_dump($dt->dayOfWeek == Carbon::MONDAY); // bool(true) : ISO8601 week starts on Monday
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->endOfWeek(); // 2012-02-05 23:59:59
var_dump($dt->dayOfWeek == Carbon::SUNDAY); // bool(true) : ISO8601 week ends on Sunday
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->next(Carbon::WEDNESDAY); // 2012-02-01 00:00:00
var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true)
//
$dt = Carbon::create(2012, 1, 1, 12, 0, 0);
echo $dt->next(); // 2012-01-08 00:00:00
//
$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->previous(Carbon::WEDNESDAY); // 2012-01-25 00:00:00
var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true)
//
$dt = Carbon::create(2012, 1, 1, 12, 0, 0);
echo $dt->previous(); // 2011-12-25 00:00:00
// average
$start = Carbon::create(2014, 1, 1, 0, 0, 0);
$end = Carbon::create(2014, 1, 30, 0, 0, 0);
echo $start->average($end); // 2014-01-15 12:00:00
// 其他的函数
// others that are defined that are similar
// firstOfMonth(), lastOfMonth(), nthOfMonth()
// firstOfQuarter(), lastOfQuarter(), nthOfQuarter()
// firstOfYear(), lastOfYear(), nthOfYear()
- 时间间隔 (可以在时间过滤处使用)
// 很好懂不解释了
echo CarbonInterval::year(); // 1 year
echo CarbonInterval::months(3); // 3 months
echo CarbonInterval::days(3)->seconds(32); // 3 days 32 seconds
echo CarbonInterval::weeks(3); // 3 weeks
echo CarbonInterval::days(23); // 3 weeks 2 days
echo CarbonInterval::create(2, 0, 5, 1, 1, 2, 7); // 2 years 5 weeks 1 day 1 hour 2 minutes 7 seconds