Pro Yearly is on sale from $80 to $50! »

素晴らしき Carbon の世界 〜あなたも今日から日時マスター〜

4584c227ef4ea69e789e5f53e0476418?s=47 omoon
June 27, 2015

素晴らしき Carbon の世界 〜あなたも今日から日時マスター〜

June 27, 2015 PHPカンファレンス福岡2015の発表資料です。

4584c227ef4ea69e789e5f53e0476418?s=128

omoon

June 27, 2015
Tweet

Transcript

  1. ૉ੖Β͖͠$BSCPOͷੈք ʙ͋ͳͨ΋ࠓ೔͔Β೔࣌Ϛελʔʙ 1)1ΧϯϑΝϨϯε෱Ԭ +VOF  4PUBSP0.63" !PNPPO

  2. "CPVU.F w4PUBSP0.63" wେࡕ͔Βདྷ·ͨ͠ w,BOTBJ1)16TFST(SPVQ4UB⒎ w1)1ྺ͸೥΄Ͳʢ࠷ۙ͸-BSBWFMʣ wPNPPOPSH w!PNPPO

  3. None
  4. None
  5. "CPVU.F

  6. "CPVU.F

  7. ࠓ೔ͷ͸ͳ͠

  8. $BSCPO

  9. $BSCPO஌ͬͯΔਓʁ

  10. None
  11. #SJBO/FTCJUU

  12. %BUF5JNFΫϥεΛܧঝ class  Carbon  extends  DateTime
 {  

  13. %BUF5JNFΫϥεΛܧঝ class  Carbon  extends  DateTime
 {  

  14. ೔࣌Ͱۤ͠Μͩաڈ

  15. date   mktime   DateTime

  16. ͑͑ײ͡ʹ΍ͬͯ΍

  17. None
  18. ͱʹ͔͑͑͘ײ͡

  19. Ͱ͸·ͣ

  20. $date  =  new  Carbon();
 echo  $date;

  21. 2015-­‐06-­‐27  14:40:15

  22. Carbon::setToStringFormat(          'Y೥n݄j೔  H࣌i෼sඵͬͪΌʂ'   );
 $date

     =  new  Carbon();
 echo  $date;
  23. 2015೥6݄27೔  14࣌45෼17ඵͬͪΌʂ

  24. ࡞Γํ৭ʑ

  25. $today  =  new  Carbon();
 $today  =  new  Carbon('2015-­‐06-­‐27');
 $today  =

     new  Carbon('today');
 $today  =  Carbon::today();
 $today  =  Carbon::createFromDate(2015,  6,  27);
  26. $yesterday  =  new  Carbon('yesterday');
 $yesterday  =  new  Carbon('2015-­‐06-­‐26');
 $yesterday  =

     Carbon::yesterday();
  27. ޷Έ͸

  28. Carbon::now();

  29. Carbon::now()-­‐>next(Carbon::SATURDAY);

  30. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  31. UPܥ

  32. $today-­‐>format('Y-­‐m-­‐d  H:i');   //  2015-­‐06-­‐27  14:50

  33. echo  $today-­‐>toDateString();  //  2015-­‐06-­‐27
 echo  $today-­‐>toFormattedDateString();  //  Jun  27,  201


    echo  $today-­‐>toTimeString();  //  17:52:10
 echo  $today-­‐>toDateTimeString();  //  2015-­‐06-­‐27  17:53:01
 echo  $today-­‐>toDayDateTimeString();  //  Sat,  Jun  27,  2015  5:53  PM
 echo  $today-­‐>toAtomString();  //  2015-­‐06-­‐27T17:53:28+09:00
 echo  $today-­‐>toCookieString();  //  Saturday,  27-­‐Jun-­‐2015  17:53:38  JST
 echo  $today-­‐>toIso8601String();  //  2015-­‐06-­‐27T17:53:53+0900
 echo  $today-­‐>toRfc822String();  //  Sat,  27  Jun  15  17:54:04  +0900
 echo  $today-­‐>toRfc850String();  //  Saturday,  27-­‐Jun-­‐15  17:54:15  JST
 echo  $today-­‐>toRfc1036String();  //  Sat,  27  Jun  15  17:54:27  +0900
 echo  $today-­‐>toRfc1123String();  //  Sat,  27  Jun  2015  17:54:40  +0900
 echo  $today-­‐>toRfc2822String();  //  Sat,  27  Jun  2015  17:54:50  +0900
 echo  $today-­‐>toRfc3339String();  //  2015-­‐06-­‐27T17:55:02+09:00
 echo  $today-­‐>toRssString();  //  Sat,  27  Jun  2015  17:55:23  +0900
 echo  $today-­‐>toW3cString();  //  2015-­‐06-­‐27T17:55:35+09:00
  34. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  35. JTܥ

  36. $today  =  Carbon::now();
 $today-­‐>isToday();  //  true

  37. $today-­‐>isSunday();  //  false
 $today-­‐>isMonday();  //  false
 $today-­‐>isTuesday();  //  false
 $today-­‐>isWednesday();

     //  false
 $today-­‐>isThursday();  //  false
 $today-­‐>isFriday();  //  false
 $today-­‐>isSaturday();  //  true
  38. $today-­‐>isWeekday();  //  false
 $today-­‐>isWeekend();  //  true

  39. Carbon::setWeekendDays([
        Carbon::MONDAY,
        Carbon::TUESDAY,
  

         Carbon::WEDNESDAY,
        Carbon::THURSDAY,
        Carbon::FRIDAY,
        Carbon::SATURDAY,
        Carbon::SUNDAY,
 ]);
 $today-­‐>isWeekend();  //  ͍ͭͰ΋true
  40. $today-­‐>isTomorrow();  //  false
 $today-­‐>isYesterday();  //false

  41. $today-­‐>isFuture();  //  false
 $today-­‐>isPast();  //  false

  42. $today-­‐>isLeapYear();  //  false

  43. ൺ΂Δܥ

  44. $today-­‐>isSameDay($today);  //  true

  45. $myBirthday  =  Carbon::parse('1973-­‐7-­‐31');
 $yourBirthday  =  Carbon::parse('1990-­‐6-­‐27');
 $today-­‐>isBirthday($myBirthday);  //  false
 $today-­‐>isBirthday($yourBirthday);

     //  true
  46. $myBirthday  =  Carbon::parse('1973-­‐7-­‐31');
 $myBirthday-­‐>age;  //  41

  47. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  48. BEETVCܥ

  49. $today-­‐>addDay();  //  2015-­‐06-­‐28  10:44:30
 $today-­‐>addMonth();  //  2015-­‐07-­‐28  10:44:44
 $today-­‐>addYear();  //

     2016-­‐07-­‐28  10:45:09
 $today-­‐>addHour();  //  2016-­‐07-­‐28  11:46:17
 $today-­‐>addMinute();  //  2016-­‐07-­‐28  11:47:36
 $today-­‐>addSecond();  //  2016-­‐07-­‐28  11:47:51
  50. $today-­‐>subDay();
 $today-­‐>subMonth();
 $today-­‐>subYear();
 $today-­‐>subHour();
 $today-­‐>subMinute();
 $today-­‐>subSecond();

  51. $today-­‐>addDays(5);
 $today-­‐>addMonths(5);
 $today-­‐>addYears(5);
 $today-­‐>addHours(5);
 $today-­‐>addMinutes(5);
 $today-­‐>addSeconds(5);

  52. $today-­‐>subDays(5);
 $today-­‐>subMonths(5);
 $today-­‐>subYears(5);
 $today-­‐>subHours(5);
 $today-­‐>subMinutes(5);
 $today-­‐>subSeconds(5);

  53. ͜ΕͰ΋͍͍ $today-­‐>addDay(5);
 $today-­‐>addMonth(5);
 $today-­‐>addYear(5);
 $today-­‐>addHour(5);
 $today-­‐>addMinute(5);
 $today-­‐>addSecond(5);   $today-­‐>subDay(5);
 $today-­‐>subMonth(5);


    $today-­‐>subYear(5);
 $today-­‐>subHour(5);
 $today-­‐>subMinute(5);
 $today-­‐>subSecond(5);
  54. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  55. TUBSUFOEܥ

  56. 
 $today-­‐>startOfDay();  //  2015-­‐06-­‐27  00:00:00
 $today-­‐>startOfWeek();  //  2015-­‐06-­‐22  00:00:00
 $today-­‐>startOfMonth();

     //  2015-­‐06-­‐01  00:00:00
 $today-­‐>startOfYear();  //  2015-­‐01-­‐01  00:00:00
 $today-­‐>startOfDecade();  //  2010-­‐01-­‐01  00:00:00
 $today-­‐>startOfCentury();  //  2000-­‐01-­‐01  00:00:00
  57. 
 $today-­‐>endOfCentury();  //  2099-­‐12-­‐31  23:59:59
 $today-­‐>endOfDecade();  //  2019-­‐12-­‐31  23:59:59
 $today-­‐>endOfYear();

     //  2015-­‐12-­‐31  23:59:59
 $today-­‐>endOfMonth();  //  2015-­‐06-­‐30  23:59:59
 $today-­‐>endOfWeek();  //  2015-­‐06-­‐28  23:59:59
 $today-­‐>endOfDay();  //  2015-­‐06-­‐27  23:59:59
  58. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  59. pSTUMBTUOUIܥ

  60. Կ൪໨ͷԿ༵೔

  61. None
  62. $today-­‐>firstOfMonth(Carbon::MONDAY);  //  2015-­‐06-­‐01  00:00:00
 $today-­‐>nthOfMonth(3,  Carbon::MONDAY);  //  2015-­‐06-­‐15  00:00:00

  63. $today-­‐>firstOfMonth(Carbon::MONDAY);
 $today-­‐>firstOfQuarter(Carbon::MONDAY);
 $today-­‐>firstOfYear(Carbon::MONDAY);
 $today-­‐>lastOfMonth(Carbon::MONDAY);
 $today-­‐>lastOfQuarter(Carbon::MONDAY);
 $today-­‐>lastOfYear(Carbon::MONDAY);
 $today-­‐>nthOfMonth(3,  Carbon::MONDAY);
 $today-­‐>nthOfQuarter(3,  Carbon::MONDAY);


    $today-­‐>nthOfYear(3,  Carbon::MONDAY);
  64. $today-­‐>nthOfMonth(5,  Carbon::MONDAY);  //  false

  65. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  66. EJ⒎ܥ

  67. $date1  =  Carbon::parse('2015-­‐7-­‐1');
 $date2  =  Carbon::now()-­‐>endOfYear();
 $date1-­‐>diffInSeconds($date2);  //  15897599
 $date1-­‐>diffInMinutes($date2);

     //  264959
 $date1-­‐>diffInHours($date2);  //  4415
 $date1-­‐>diffInDays($date2);  //  183
 $date1-­‐>diffInWeeks($date2);  //  26
 $date1-­‐>diffInYears($date2);  //  0   $date1-­‐>diffForHumans($date2);  //  6  months  before
  68. //  ݄͔̓Β೥຤·Ͱฏ೔͕Կ೔͋Δ͔
 $date1  =  Carbon::create(2015,  7,  1);
 $date2  =  Carbon::now()-­‐>endOfYear();


    $date1-­‐>diffInDaysFiltered(function(Carbon  $date)  {
        return  $date-­‐>isWeekday();
 },  $date2);  //  132
  69. 
 //  ݄͔̓Β೥຤·Ͱ̏೔͓͖ʹͦͷ೔͕ि຤Ͱͳ͍৔߹͸ಇ͘
 $date1  =  Carbon::create(2015,  7,  1);
 $date2  =

     Carbon::now()-­‐>endOfYear();
 $date1-­‐>diffFiltered(\Carbon\CarbonInterval::days(3),  function(Carbon  $date)  {
        return  $date-­‐>isWeekday();
 },  $date2);  //  45

  70. $date1  =  Carbon::createFromDate(2015,  6,  27,  'Asia/Tokyo');
 $date2  =  Carbon::createFromDate(2015,  6,

     27,  'Europe/Copenhagen');
 echo  $date1-­‐>diffInHours($date2);  //  7
  71. $date1  =  Carbon::createFromDate(2015,  12,  27,  'Asia/Tokyo');
 $date2  =  Carbon::createFromDate(2015,  12,

     27,  'Europe/Copenhagen');
 echo  $date1-­‐>diffInHours($date2);  //  8
  72. //  Daylight  Saving  Time
 Carbon::createFromDate(2015,  6,  27,  'Europe/Copenhagen')-­‐>dst;  //  true


    Carbon::createFromDate(2015,  12,  27,  'Europe/Copenhagen')-­‐>dst;  //  false  
  73. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  74. ڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  75. const  SUNDAY  =  0;
 const  MONDAY  =  1;
 const  TUESDAY

     =  2;
 const  WEDNESDAY  =  3;
 const  THURSDAY  =  4;
 const  FRIDAY  =  5;
 const  SATURDAY  =  6;
  76. const  YEARS_PER_CENTURY  =  100;
 const  YEARS_PER_DECADE  =  10;
 const  MONTHS_PER_YEAR

     =  12;
 const  WEEKS_PER_YEAR  =  52;
 const  DAYS_PER_WEEK  =  7;
 const  HOURS_PER_DAY  =  24;
 const  MINUTES_PER_HOUR  =  60;
 const  SECONDS_PER_MINUTE  =  60;
  77. ͑͑ײ͡γϦʔζ wUPܥ wJTܥ wBEETVCܥ wTUBSUFOEܥ wpSTUMBTUOUIܥ wEJ⒎ܥ wڭ͑ͯ͘Εͯ͋Γ͕ͱ͏ܥ

  78. ൺֱ

  79. $today  =  Carbon::today();
 $yesterday  =  Carbon::yesterday();
 $today-­‐>lt($yesterday);  //  false
 $yesterday-­‐>lt($today);

     //  true
 $today-­‐>gt($yesterday);  //  true
 $yesterday-­‐>gt($today);  //  false
  80. MFTTUIBOHSFBUFSUIBO

  81. MFTTUIBOHSFBUFSUIBO

  82. $today-­‐>eq($today);  //  true   $today-­‐>ne($today);  //  false

  83. FRVBM OPUFRVBM

  84. FRVBM OPUFRVBM

  85. $yesterday-­‐>lte($today);  //  true
 $yesterday-­‐>gte($today);  //  false

  86. MFTTUIBOFRVBM HSFBUFSUIBOFRVBM

  87. MFTTUIBOFRVBM HSFBUFSUIBOFRVBM

  88. NJONBY $date1  =  Carbon::create(2015,  3,  11);
 $date2  =  Carbon::create(1999,  3,

     11);
 
 if  ($date1-­‐>gt($date2)  ===  true)  {
        echo  $date1-­‐>toDateTimeString();
 }
 
 if  ($date1-­‐>lt($date2)  ===  true)  {
        echo  $date1-­‐>toDateTimeString();
 }
  89. NJONBY $date1  =  Carbon::create(2015,  3,  11);
 $date2  =  Carbon::create(1999,  3,

     11);
 
 if  ($date1-­‐>gt($date2)  ===  true)  {
        echo  $date1-­‐>toDateTimeString();
 }
 echo  $date1-­‐>max($date2)-­‐>toDateTimeString();
 
 if  ($date1-­‐>lt($date2)  ===  true)  {
        echo  $date1-­‐>toDateTimeString();
 }
 echo  $date1-­‐>min($date2)-­‐>toDateTimeString();
  90. ࢲ͕ࢥ͏Ұ൪ͷັྗ

  91. Carbon::setTestNow();

  92. ̍೥ʹ̍౓͔͠௨Βͳ͍ςετ public  function  testTodayIsMyBirthday()
 {          $myBirthday

     =  Carbon::createFromDate(1973,  7,  31);
        $today  =  Carbon::today();
        $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
 }
  93. ͍ͭͰ΋௨Δςετʹʂ public  function  testTodayIsMyBirthday()
 {
        Carbon::setTestNow(Carbon::createFromDate(2015,  7,

     31));
        $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
        $today  =  Carbon::today();
        $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
 }
  94. class  SeasonalProduct   {          protected  $price;

             public  function  __construct($price)          {                  $this-­‐>price  =  $price;          }          public  function  getPrice()  {                  $multiplier  =  1;                  if  (Carbon::now()-­‐>month  ==  12)  {                          $multiplier  =  2;                  }                  return  $this-­‐>price  *  $multiplier;          }   }   $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();
  95. ͸·ΓͲ͜Ζ

  96. TFU5FTU/PX TFU͗͢͠໰୊

  97. TFU5FTU/PX TFU͗͢͠໰୊ class  carbonTest  extends  PHPUnit_Framework_TestCase
 {
      public

     function  testTodayIsMyBirthday()
        {
                Carbon::setTestNow(Carbon::createFromDate(2015,  7,  31));
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
        }
 
        public  function  testTodayIsNotMyBirthday()
        {
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertFalse($today-­‐>isBirthday($myBirthday));
        }
 }
  98. TFU5FTU/PX TFU͗͢͠໰୊ class  carbonTest  extends  PHPUnit_Framework_TestCase
 {
      public

     function  testTodayIsMyBirthday()
        {
                Carbon::setTestNow(Carbon::createFromDate(2015,  7,  31));
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
        }
 
        public  function  testTodayIsNotMyBirthday()
        {
                Carbon::setTestNow(Carbon::createFromDate(2015,  7,  30));
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertFalse($today-­‐>isBirthday($myBirthday));
        }
 }
  99. TFU5FTU/PX TFU͗͢͠໰୊ class  carbonTest  extends  PHPUnit_Framework_TestCase
 {
      

     public  function  setUp()
        {
                Carbon::setTestNow();
        }
 
        public  function  testTodayIsMyBirthday()
        {
                Carbon::setTestNow(Carbon::createFromDate(2015,  7,  31));
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
        }
 
        public  function  testTodayIsNotMyBirthday()
        {
                $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
                $today  =  Carbon::today();
                $this-­‐>assertFalse($today-­‐>isBirthday($myBirthday));
        }
 }
  100. ࠷ޙʹ

  101. $BSCPO

  102. $BSCPO%BUJOH

  103. 3BEJPDBSCPO%BUJOH

  104. ์ࣹੑ୸ૉ೥୅ଌఆ

  105. ์ࣹੑ୸ૉ೥୅ଌఆ

  106. None
  107. ͋Γ͕ͱ͏͍͟͝·ͨ͠