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. $today  =  new  Carbon();
 $today  =  new  Carbon('2015-­‐06-­‐27');
 $today  =

     new  Carbon('today');
 $today  =  Carbon::today();
 $today  =  Carbon::createFromDate(2015,  6,  27);
  2. 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
  3. $today-­‐>isSunday();  //  false
 $today-­‐>isMonday();  //  false
 $today-­‐>isTuesday();  //  false
 $today-­‐>isWednesday();

     //  false
 $today-­‐>isThursday();  //  false
 $today-­‐>isFriday();  //  false
 $today-­‐>isSaturday();  //  true
  4. Carbon::setWeekendDays([
        Carbon::MONDAY,
        Carbon::TUESDAY,
  

         Carbon::WEDNESDAY,
        Carbon::THURSDAY,
        Carbon::FRIDAY,
        Carbon::SATURDAY,
        Carbon::SUNDAY,
 ]);
 $today-­‐>isWeekend();  //  ͍ͭͰ΋true
  5. $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
  6. 
 $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
  7. 
 $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
  8. $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
  9. //  ݄͔̓Β೥຤·Ͱฏ೔͕Կ೔͋Δ͔
 $date1  =  Carbon::create(2015,  7,  1);
 $date2  =  Carbon::now()-­‐>endOfYear();


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

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

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

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

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


    Carbon::createFromDate(2015,  12,  27,  'Europe/Copenhagen')-­‐>dst;  //  false  
  14. const  SUNDAY  =  0;
 const  MONDAY  =  1;
 const  TUESDAY

     =  2;
 const  WEDNESDAY  =  3;
 const  THURSDAY  =  4;
 const  FRIDAY  =  5;
 const  SATURDAY  =  6;
  15. 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;
  16. $today  =  Carbon::today();
 $yesterday  =  Carbon::yesterday();
 $today-­‐>lt($yesterday);  //  false
 $yesterday-­‐>lt($today);

     //  true
 $today-­‐>gt($yesterday);  //  true
 $yesterday-­‐>gt($today);  //  false
  17. 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();
 }
  18. 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();
  19. ̍೥ʹ̍౓͔͠௨Βͳ͍ςετ public  function  testTodayIsMyBirthday()
 {          $myBirthday

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

     31));
        $myBirthday  =  Carbon::createFromDate(1973,  7,  31);
        $today  =  Carbon::today();
        $this-­‐>assertTrue($today-­‐>isBirthday($myBirthday));
 }
  21. 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();
  22. 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));
        }
 }
  23. 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));
        }
 }
  24. 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));
        }
 }