Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

omoon
June 27, 2015

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

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

omoon

June 27, 2015
Tweet

More Decks by omoon

Other Decks in Technology

Transcript

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

    View full-size slide

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

    View full-size slide

  3. ࠓ೔ͷ͸ͳ͠

    View full-size slide

  4. $BSCPO஌ͬͯΔਓʁ

    View full-size slide

  5. #SJBO/FTCJUU

    View full-size slide

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

    {  

    View full-size slide

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

    {  

    View full-size slide

  8. ೔࣌Ͱۤ͠Μͩաڈ

    View full-size slide

  9. date  
    mktime  
    DateTime

    View full-size slide

  10. ͑͑ײ͡ʹ΍ͬͯ΍

    View full-size slide

  11. ͱʹ͔͑͑͘ײ͡

    View full-size slide

  12. $date  =  new  Carbon();

    echo  $date;

    View full-size slide

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

    View full-size slide

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

    $date  =  new  Carbon();

    echo  $date;

    View full-size slide

  15. 2015೥6݄27೔  14࣌45෼17ඵͬͪΌʂ

    View full-size slide

  16. ࡞Γํ৭ʑ

    View full-size slide

  17. $today  =  new  Carbon();

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

    $today  =  new  Carbon('today');

    $today  =  Carbon::today();

    $today  =  Carbon::createFromDate(2015,  6,  27);

    View full-size slide

  18. $yesterday  =  new  Carbon('yesterday');

    $yesterday  =  new  Carbon('2015-­‐06-­‐26');

    $yesterday  =  Carbon::yesterday();

    View full-size slide

  19. Carbon::now();

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. 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

    View full-size slide

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

    View full-size slide

  25. $today  =  Carbon::now();

    $today-­‐>isToday();  //  true

    View full-size slide

  26. $today-­‐>isSunday();  //  false

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

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

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

    $today-­‐>isThursday();  //  false

    $today-­‐>isFriday();  //  false

    $today-­‐>isSaturday();  //  true

    View full-size slide

  27. $today-­‐>isWeekday();  //  false

    $today-­‐>isWeekend();  //  true

    View full-size slide

  28. Carbon::setWeekendDays([

           Carbon::MONDAY,

           Carbon::TUESDAY,

           Carbon::WEDNESDAY,

           Carbon::THURSDAY,

           Carbon::FRIDAY,

           Carbon::SATURDAY,

           Carbon::SUNDAY,

    ]);

    $today-­‐>isWeekend();  //  ͍ͭͰ΋true

    View full-size slide

  29. $today-­‐>isTomorrow();  //  false

    $today-­‐>isYesterday();  //false

    View full-size slide

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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. $myBirthday  =  Carbon::parse('1973-­‐7-­‐31');

    $yourBirthday  =  Carbon::parse('1990-­‐6-­‐27');

    $today-­‐>isBirthday($myBirthday);  //  false

    $today-­‐>isBirthday($yourBirthday);  //  true

    View full-size slide

  34. $myBirthday  =  Carbon::parse('1973-­‐7-­‐31');

    $myBirthday-­‐>age;  //  41

    View full-size slide

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

    View full-size slide

  36. $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

    View full-size slide

  37. $today-­‐>subDay();

    $today-­‐>subMonth();

    $today-­‐>subYear();

    $today-­‐>subHour();

    $today-­‐>subMinute();

    $today-­‐>subSecond();

    View full-size slide

  38. $today-­‐>addDays(5);

    $today-­‐>addMonths(5);

    $today-­‐>addYears(5);

    $today-­‐>addHours(5);

    $today-­‐>addMinutes(5);

    $today-­‐>addSeconds(5);

    View full-size slide

  39. $today-­‐>subDays(5);

    $today-­‐>subMonths(5);

    $today-­‐>subYears(5);

    $today-­‐>subHours(5);

    $today-­‐>subMinutes(5);

    $today-­‐>subSeconds(5);

    View full-size slide

  40. ͜ΕͰ΋͍͍
    $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);

    View full-size slide

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

    View full-size slide


  42. $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

    View full-size slide


  43. $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

    View full-size slide

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

    View full-size slide

  45. pSTUMBTUOUIܥ

    View full-size slide

  46. Կ൪໨ͷԿ༵೔

    View full-size slide

  47. $today-­‐>firstOfMonth(Carbon::MONDAY);  //  2015-­‐06-­‐01  00:00:00

    $today-­‐>nthOfMonth(3,  Carbon::MONDAY);  //  2015-­‐06-­‐15  00:00:00

    View full-size slide

  48. $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);

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  51. $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

    View full-size slide

  52. //  ݄͔̓Β೥຤·Ͱฏ೔͕Կ೔͋Δ͔

    $date1  =  Carbon::create(2015,  7,  1);

    $date2  =  Carbon::now()-­‐>endOfYear();

    $date1-­‐>diffInDaysFiltered(function(Carbon  $date)  {

           return  $date-­‐>isWeekday();

    },  $date2);  //  132

    View full-size slide


  53. //  ݄͔̓Β೥຤·Ͱ̏೔͓͖ʹͦͷ೔͕ि຤Ͱͳ͍৔߹͸ಇ͘

    $date1  =  Carbon::create(2015,  7,  1);

    $date2  =  Carbon::now()-­‐>endOfYear();

    $date1-­‐>diffFiltered(\Carbon\CarbonInterval::days(3),  function(Carbon  $date)  {

           return  $date-­‐>isWeekday();

    },  $date2);  //  45


    View full-size slide

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

    $date2  =  Carbon::createFromDate(2015,  6,  27,  'Europe/Copenhagen');

    echo  $date1-­‐>diffInHours($date2);  //  7

    View full-size slide

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

    $date2  =  Carbon::createFromDate(2015,  12,  27,  'Europe/Copenhagen');

    echo  $date1-­‐>diffInHours($date2);  //  8

    View full-size slide

  56. //  Daylight  Saving  Time

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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  59. const  SUNDAY  =  0;

    const  MONDAY  =  1;

    const  TUESDAY  =  2;

    const  WEDNESDAY  =  3;

    const  THURSDAY  =  4;

    const  FRIDAY  =  5;

    const  SATURDAY  =  6;

    View full-size slide

  60. 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;

    View full-size slide

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

    View full-size slide

  62. $today  =  Carbon::today();

    $yesterday  =  Carbon::yesterday();

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

    $yesterday-­‐>lt($today);  //  true

    $today-­‐>gt($yesterday);  //  true

    $yesterday-­‐>gt($today);  //  false

    View full-size slide

  63. MFTTUIBOHSFBUFSUIBO

    View full-size slide

  64. MFTTUIBOHSFBUFSUIBO

    View full-size slide

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

    View full-size slide

  66. FRVBM
    OPUFRVBM

    View full-size slide

  67. FRVBM
    OPUFRVBM

    View full-size slide

  68. $yesterday-­‐>lte($today);  //  true

    $yesterday-­‐>gte($today);  //  false

    View full-size slide

  69. MFTTUIBOFRVBM
    HSFBUFSUIBOFRVBM

    View full-size slide

  70. MFTTUIBOFRVBM
    HSFBUFSUIBOFRVBM

    View full-size slide

  71. 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();

    }

    View full-size slide

  72. 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();

    View full-size slide

  73. ࢲ͕ࢥ͏Ұ൪ͷັྗ

    View full-size slide

  74. Carbon::setTestNow();

    View full-size slide

  75. ̍೥ʹ̍౓͔͠௨Βͳ͍ςετ
    public  function  testTodayIsMyBirthday()

    {  
           $myBirthday  =  Carbon::createFromDate(1973,  7,  31);

           $today  =  Carbon::today();

           $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));

    }

    View full-size slide

  76. ͍ͭͰ΋௨Δςετʹʂ
    public  function  testTodayIsMyBirthday()

    {

           Carbon::setTestNow(Carbon::createFromDate(2015,  7,  31));

           $myBirthday  =  Carbon::createFromDate(1973,  7,  31);

           $today  =  Carbon::today();

           $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));

    }

    View full-size slide

  77. 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();

    View full-size slide

  78. ͸·ΓͲ͜Ζ

    View full-size slide

  79. TFU5FTU/PX
    TFU͗͢͠໰୊

    View full-size slide

  80. 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));

           }

    }

    View full-size slide

  81. 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));

           }

    }

    View full-size slide

  82. 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));

           }

    }

    View full-size slide

  83. $BSCPO%BUJOH

    View full-size slide

  84. 3BEJPDBSCPO%BUJOH

    View full-size slide

  85. ์ࣹੑ୸ૉ೥୅ଌఆ

    View full-size slide

  86. ์ࣹੑ୸ૉ೥୅ଌఆ

    View full-size slide

  87. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide