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

4584c227ef4ea69e789e5f53e0476418?s=47 omoon
June 27, 2015

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

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

4584c227ef4ea69e789e5f53e0476418?s=128

omoon

June 27, 2015
Tweet

Transcript

  1. 3.
  2. 4.
  3. 8.
  4. 10.
  5. 17.
  6. 19.
  7. 25.

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

     new  Carbon('today');
 $today  =  Carbon::today();
 $today  =  Carbon::createFromDate(2015,  6,  27);
  8. 27.
  9. 31.
  10. 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
  11. 35.
  12. 37.

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

     //  false
 $today-­‐>isThursday();  //  false
 $today-­‐>isFriday();  //  false
 $today-­‐>isSaturday();  //  true
  13. 39.

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

         Carbon::WEDNESDAY,
        Carbon::THURSDAY,
        Carbon::FRIDAY,
        Carbon::SATURDAY,
        Carbon::SUNDAY,
 ]);
 $today-­‐>isWeekend();  //  ͍ͭͰ΋true
  14. 43.
  15. 48.
  16. 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
  17. 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
  18. 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
  19. 61.
  20. 66.
  21. 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
  22. 68.

    //  ݄͔̓Β೥຤·Ͱฏ೔͕Կ೔͋Δ͔
 $date1  =  Carbon::create(2015,  7,  1);
 $date2  =  Carbon::now()-­‐>endOfYear();


    $date1-­‐>diffInDaysFiltered(function(Carbon  $date)  {
        return  $date-­‐>isWeekday();
 },  $date2);  //  132
  23. 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

  24. 70.

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

     27,  'Europe/Copenhagen');
 echo  $date1-­‐>diffInHours($date2);  //  7
  25. 71.

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

     27,  'Europe/Copenhagen');
 echo  $date1-­‐>diffInHours($date2);  //  8
  26. 72.

    //  Daylight  Saving  Time
 Carbon::createFromDate(2015,  6,  27,  'Europe/Copenhagen')-­‐>dst;  //  true


    Carbon::createFromDate(2015,  12,  27,  'Europe/Copenhagen')-­‐>dst;  //  false  
  27. 75.

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

     =  2;
 const  WEDNESDAY  =  3;
 const  THURSDAY  =  4;
 const  FRIDAY  =  5;
 const  SATURDAY  =  6;
  28. 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;
  29. 78.
  30. 79.

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

     //  true
 $today-­‐>gt($yesterday);  //  true
 $yesterday-­‐>gt($today);  //  false
  31. 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();
 }
  32. 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();
  33. 92.

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

     =  Carbon::createFromDate(1973,  7,  31);
        $today  =  Carbon::today();
        $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
 }
  34. 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));
 }
  35. 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();
  36. 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));
        }
 }
  37. 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));
        }
 }
  38. 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));
        }
 }
  39. 100.
  40. 101.
  41. 106.