JavaScript 日期处理类库 http://momentjs.cn/
<?php Carbon 是php的日期处理类库(A simple PHP API extension for DateTime.)。 Carbon 继承了PHP的 Datetime 类,所以 Carbon 中没有涉及到的,但在 Datetime 中已经实现的方法都是可以使用的。 $ composer require nesbot/carbon require 'path/to/Carbon.php'; use Carbon\Carbon; printf("Now: %s", Carbon::now()); $carbon = new Carbon(); // equivalent to Carbon::now() $carbon = new Carbon('first day of January 2008', 'America/Vancouver'); echo get_class($carbon); // 'Carbon\Carbon' $carbon = Carbon::now(-5); 你将会喜欢上用 parse() 方法来代替原有繁琐的构造方式 echo (new Carbon('first day of December 2008'))->addWeeks(2); // 2008-12-15 00:00:00 echo Carbon::parse('first day of December 2008')->addWeeks(2); // 2008-12-15 00:00:00 类似 now() 这样直接返回 Carbon 实例的方法还有 today(), tomorrow(),yesterday(), 他们都接受一个 timezone 类型的参数,最后得到的结果时间部分都是 00:00:00 $now = Carbon::now(); echo $now; // 2016-06-24 15:18:34 $today = Carbon::today(); echo $today; // 2016-06-24 00:00:00 $tomorrow = Carbon::tomorrow('Europe/London'); echo $tomorrow; // 2016-06-25 00:00:00 $yesterday = Carbon::yesterday(); echo $yesterday; // 2016-06-23 00:00:00 Carbon::createFromFormat($format, $time, $tz); $dt = Carbon::parse('1975-05-21 22:23:00.123456'); echo $dt->micro; // 123456 echo $dt->copy()->micro; // 123456 获取PHP支持的有效时间取值范围:最早时间、最晚时间 echo Carbon::maxValue(); // '2038-01-18 22:14:07' echo Carbon::minValue(); $product = new SeasonalProduct(100); Carbon::setTestNow(Carbon::parse('first day of March 2000')); echo $product->getPrice(); // 100 Carbon::setTestNow(Carbon::parse('first day of December 2000')); echo $product->getPrice(); // 200 Carbon::setTestNow(Carbon::parse('first day of May 2000')); echo $product->getPrice(); // 100 Carbon::setTestNow(); 一些相关的用法也可以得到一个模拟的 now 实例,返回相应的模拟数据。 以下是当前支持的时间转换字 this net last this next last tomorrow yesterday + - first last ago 获取器通过PHP的 __get() 方式实现。可以直接通过一下方式直接获取到属性的值。 $dt = Carbon::parse('2012-9-5 23:26:11.123789'); // These getters specifically return integers, ie intval() var_dump($dt->year); // int(2012) var_dump($dt->month); // int(9) var_dump($dt->day); // int(5) var_dump($dt->hour); // int(23) var_dump($dt->minute); // int(26) var_dump($dt->second); // int(11) var_dump($dt->micro); // int(123789) var_dump($dt->dayOfWeek); // int(3) var_dump($dt->dayOfYear); // int(248) var_dump($dt->weekOfMonth); // int(1) var_dump($dt->weekOfYear); // int(36) var_dump($dt->daysInMonth); // int(30) var_dump($dt->timestamp); // int(1346901971) var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(41) calculated vs now in the same tz var_dump($dt->quarter); // int(3) // Returns an int of seconds difference from UTC (+/- sign included) var_dump(Carbon::createFromTimestampUTC(0)->offset); // int(0) var_dump(Carbon::createFromTimestamp(0)->offset); // int(-18000) // Returns an int of hours difference from UTC (+/- sign included) var_dump(Carbon::createFromTimestamp(0)->offsetHours); // int(-5) // Indicates if day light savings time is on var_dump(Carbon::createFromDate(2012, 1, 1)->dst); // bool(false) var_dump(Carbon::createFromDate(2012, 9, 1)->dst); // bool(true) // Indicates if the instance is in the same timezone as the local timezone var_dump(Carbon::now()->local); // bool(true) var_dump(Carbon::now('America/Vancouver')->local); // bool(false) // Indicates if the instance is in the UTC timezone var_dump(Carbon::now()->utc); // bool(false) var_dump(Carbon::now('Europe/London')->utc); // bool(false) var_dump(Carbon::createFromTimestampUTC(0)->utc); // bool(true) // Gets the DateTimeZone instance echo get_class(Carbon::now()->timezone); // DateTimeZone echo get_class(Carbon::now()->tz); // DateTimeZone // Gets the DateTimeZone instance name, shortcut for ->timezone->getName() echo Carbon::now()->timezoneName; // America/Toronto echo Carbon::now()->tzName; // America/Toronto Setters 通过PHP的 __set() 方法实现。值得注意的是,通过这种方式设置时间戳时,时区不会相对于时间戳而改变。如果需要改变时区的话,需要针对时区单独设置。 $dt = Carbon::now(); $dt->year = 1975; $dt->month = 13; // would force year++ and month = 1 $dt->month = 5; $dt->day = 21; $dt->hour = 22; $dt->minute = 32; $dt->second = 5; $dt->timestamp = 169957925; // This will not change the timezone // Set the timezone via DateTimeZone instance or string $dt->timezone = new DateTimeZone('Europe/London'); $dt->timezone = 'Europe/London'; $dt->tz = 'Europe/London'; 所有类似 toXXXString() 这样的方法都依赖于DateTime::format()。__toString() 方法允许 Carbon 实例被打印时以一种可读性更好的方式被打印出来。 $dt = Carbon::create(1975, 12, 25, 14, 15, 16); var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString() echo $dt->toDateString(); // 1975-12-25 echo $dt->toFormattedDateString(); // Dec 25, 1975 echo $dt->toTimeString(); // 14:15:16 echo $dt->toDateTimeString(); // 1975-12-25 14:15:16 echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM // ... of course format() is still available echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM echo $dt->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST 如果要判断当前实例对应的时间,是否在其他两个实例对应的时间之间,可以用 between() 方法。 如果提供了第三个参数,并且为 true,将会进行 >= 和 <= 的操作。如果为 false,会进行 > 和 < 的操作。该参数默认是 true 。 $first = Carbon::create(2012, 9, 5, 1); $second = Carbon::create(2012, 9, 5, 5); var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true) var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true) var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false) $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
https://blog.csdn.net/for_happy123/article/details/52921089