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

CakePHP3で時間を
扱いたかったお話 / Fukuoka.php 201904

CakePHP3で時間を
扱いたかったお話 / Fukuoka.php 201904

CakePHP3で時間を
扱いたかったお話
~ DateIntervalとカスタムタイプ ~


Fukuoka.php Vol.29
2019/04

スライド内URLとか

cakephp-time-interval
https://github.com/nojimage/cakephp-time-interval

CakePHP公式の日本語話者向けSlackチャンネル開設のご案内 - Qiita
https://qiita.com/chinpei215/items/3c116171c5308365c314

データベースの基本 #独自の型を作成する - CakePHP Book 3.7
https://book.cakephp.org/3.0/ja/orm/database-basics.html#adding-custom-database-types

PHP: DateInterval - Manual
https://www.php.net/manual/ja/class.dateinterval.php

MySQL :: MySQL 5.6 リファレンスマニュアル :: 11.3.2 TIME 型 https://dev.mysql.com/doc/refman/5.6/ja/time.html

B63ed943582d5b36a78f3be3c4d659e2?s=128

Takashi Nojima

April 26, 2019
Tweet

Transcript

  1. CakePHP3Ͱ࣌ؒΛ
 ѻ͍͔͓ͨͬͨ࿩ ~ DateIntervalͱΧελϜλΠϓ ~
 Fukuoka.php Vol.29 2019.04

  2. About Me ໊લɿ໺ౡོʢͷ͡·͔ͨ͠ʣ ॴଐɿΠϥεςΟοΫίϯαϧλϯπגࣜձࣾ
 IUUQTFMBTUJDDPOTVMUBOUTDPNʲٻਓதʂʳ
 ‣ CMPHIUUQTQIQUJQTDPN ‣ (JUIVC5XJUUFSOPKJNBHF

  3. େࣄͳ͜ͱ͸࢝Ίʹ ‣ $BLF1)1ެࣜ4MBDL೔ຊޠνϟϯωϧ $BLF1)1ެࣜͷ೔ຊޠ࿩ऀ޲͚4MBDLνϟϯωϧ։ઃͷ͝Ҋ಺2JJUB IUUQTRJJUBDPNDIJOQFJJUFNTDDD

  4. ࿩͢͜ͱ ‣ $BLF1)1Ͱ࣌ؒΛѻ͏ͨΊʹϓϥάΠϯΛ
 ॻ͍͓ͨ࿩

  5. લஔ͖ Ή͔͠Ή͔͠ɺ࡞ۀ࣌ؒΛه࿥͢Δػೳ͕ඞཁͳҊ͕݅ ͋Γ·ͨ͠ ‣ σʔλϕʔε͸.Z42- ‣ ࡞ۀ։࢝࣌ࠁɺ࡞ۀऴྃ࣌ࠁ͸5*.&45".1ܕͰ͍͍ΑͶ ‣ ࡞ۀ࣌ؒ͸ɺ։࢝ऴྃ࣌ࠁ͔ΒٻΊΕΔ͚Ͳιʔτͱ͔͍͔ͨ͠ Β5*.&ܕͰϑΟʔϧυ࡞͓͚ͬͯ͹͍͍Μ͡ΌͶʢ೴ࢮ

    ͱ҆қʹ࣌ؒͷ֨ೲʹ5*.&ܕΛ࢖༻ͯ͠͠·ͬͨ͜ͱ͕ൃ୺Ͱ͢
  6. ORM CakePHP ͷ ORM ʹΑΔܕม׵ DB Entity
 Parameter Input newEntity


    patchEntity find save
  7. ࣌ࠁͱͯ͠ѻΘΕΔ ‣ 'SP[FO5JNFΠϯελϯεʹม׵͞ΕΔ
 ʢ೔෇෦෼͸ݱࡏ೔ʹͳΔʣ ‣ ·Ͱ͔͠ѻ͑ͳ͍ ‣ ࣌ؒΛ௒͑Δͱʁ ‣ ೔਺͕Ճࢉ͞ΕΔ

    CakePHP Ͱ TIMEܕ ͸
  8. CakePHP Ͱ TIMEܕ ͸ DB: '26:15:45' PHP: '2019-04-27 02:15:45' JSON:

    '2019-04-27T02:15:45+09:00' ˡ'SP[FO5JNF
  9. None
  10. ͜͏͍ͨ͠ DB: '26:15:45' PHP: '26:15:45' JSON: '26:15:45' ˡ࣌ؒΛද͢ܕʹม׵͞Ε͍ͯΔͱ͍͍ 1)1Ͱ࣌ؒΛද͢ܕͬͯͳʹ͔͋ͬͨ͸ͣʜ

  11. ࣌ؒͷִؒʢ૬ର࣌ؒʣΛදͨ͢ΊͷΫϥε %BUF5JNFEJ⒎ ͳͲͰੜ੒Ͱ͖Δ IUUQTXXXQIQOFUNBOVBMKBDMBTTEBUFJOUFSWBMQIQ DateInterval

  12. DateInterval new DateInterval('P1M2DT3H4M5S'); // 1ϲ݄ͱ2೔ͱ3࣌ؒ4෼5ඵ new DateInterval('PT26H3M4S'); // 26࣌ؒ3෼4ඵ ISO8601Ͱఆٛ͞ΕΔ࣌ؒهड़Λ͋ͨ͑Δ͜ͱͰ΋࡞੒Ͱ͖Δ

  13. ‣ %BUF5JNFBEE΍TVCʹ༩͑ͯɺ࣌ؒΛܭࢉ͢Δ͜ ͱ͕Ͱ͖Δ ‣ %BUF*OUFSWBMGPSNBU ͰܗࣜΛࢦఆͯ͠ग़ྗͰ͖Δ ʘ࢖͑ͦ͏ʗ DateInterval

  14. ͱ͍͏Θ͚Ͱ

  15. $BLF1)1Ͱ 5*.&ܕϑΟʔϧυΛ %BUF*OUFSWBMͱͯ͠ѻ͍͍ͨ

  16. Ͳ͏΍Δͷʁ

  17. ‣ σʔλϕʔεͷجຊಠࣗͷܕΛ࡞੒͢Δ
 IUUQTCPPLDBLFQIQPSHKBPSNEBUBCBTFCBTJDTIUNMBEEJOHDVTUPN EBUBCBTFUZQFT ΧελϜλΠϓ l΋͋͠ͳ͕ͨ$BLF1)1ʹ࣮૷͞Ε͍ͯͳ͍ɺσʔλ ϕʔεಠࣗͷܕ͕ඞཁͳ৔߹ɺ$BLF1)1ͷܕγεςϜ ʹ৽ͨͳܕΛ௥Ճ͢Δ͜ͱ͕Ͱ͖·͢ɻz

  18. a$BLFa%BUBCBTFa5ZQFΛܧঝͯ͠ɺͭͷϝιου Λ࣮૷͠·͢ɻ ‣ UP1)1
 σʔλϕʔε͔ΒಡΈࠐΜͩ஋Λ1)1Ͱѻ͏ܗʹม׵͢Δɻ ‣ UP%BUBCBTF
 1)1Ͱѻ͍ͬͯΔ஋ΛσʔλϕʔεͰड͚ೖΕՄೳͳ
 ஋ʹม׵͢Δɻ ΧελϜλΠϓͷ࡞Γํ

  19. ‣ NBSTIBM
 OFX&OUJUZ΍QBUDI&OUJUZ࣌ʹݺͼग़͞Εɺೖྗ͞Εͨ஋Λਖ਼ن Խ͢Δɻ ‣ UP4UBUFNFOU
 1)1Ͱѻ͍ͬͯΔ஋ʹԠͨ͡ɺ42-ʹ౉͢ͱ͖ͷܗࣜΛฦ͢ɻ ΧελϜλΠϓͷ࡞Γํ UP1)1ɺUP%BUBCBTF͸࠷௿ݶඞཁɺNBSTIBMɺ UP4UBUFNFOU͸ඞཁʹԠ࣮ͯ͡૷͢Δɻ

  20. ͭͬͯ͘Έͨ

  21. cakephp-time-interval https://github.com/nojimage/cakephp-time-interval

  22. ͭͷܕϚοϓΛఏڙ ‣ UJNF@JOUFSWBM
 σʔλϕʔε্Ͱܗࣜʢ.Z42-ͷ5*.&ܕͳͲʣͰ ֨ೲ͍ͯ͠ΔσʔλΛ5JNF*OUFSWBMΠϯελϯεͱͯ͠औΓग़͢ ‣ UJNF@JOUFSWBM@JOU
 σʔλϕʔε্Ͱඵ਺Λ*/5&(&3ܕͱͯ֨͠ೲ͍ͯ͠ΔσʔλΛ 5JNF*OUFSWBMΠϯελϯεͱͯ͠औΓग़͢ cakephp-time-interval

  23. 5JNF*OUFSWBMΫϥε ‣ %BUF*OUFSWBMΛܧঝ ‣ ࣌ؒදࣔʹಛԽʢ೔෇ظؒ͸ߟྀ͠ͳ͍ʣ ‣ จࣈྻԽɺ+40/γϦΞϥΠζՄೳ ‣ ඵ਺ΛऔಘՄೳ cakephp-time-interval

    ৄ͍͠࢖͍ํ͸3&"%.&ͱςετίʔυΛݟͯͶ
  24. 5JNF*OUFSWBM5ZQF ‣ UP1)1
 5JNF*OUFSWBMΠϯελϯε ‣ UP%BUBCBTF
 5JNF*OUFSWBMΠϯελϯε ΧελϜλΠϓͷ࣮૷

  25. 5JNF*OUFSWBM5ZQF ‣ NBSTIBM
 ࣌ؒจࣈྻɺඵ਺ɺ%BUF*OUFSWBMΠϯελϯε
 5JNF*OUFSWBMΠϯελϯε ‣ UP4UBUFNFOU
 1%01"3".@453 ΧελϜλΠϓͷ࣮૷

  26. 5JNF*OUFSWBM"T*OU5ZQF ‣ UP1)1
 ඵ਺ JOU 5JNF*OUFSWBMΠϯελϯε ‣ UP%BUBCBTF
 5JNF*OUFSWBMΠϯελϯεඵ਺ JOU

    ΧελϜλΠϓͷ࣮૷
  27. 5JNF*OUFSWBM"T*OU5ZQF ‣ NBSTIBM
 ࣌ؒจࣈྻɺඵ਺ɺ%BUF*OUFSWBMΠϯελϯε
 5JNF*OUFSWBMΠϯελϯε ‣ UP4UBUFNFOU
 1%01"3".@*/5 ΧελϜλΠϓͷ࣮૷

  28. $table->newEntity([ 'time1' => '01:00:00', 'time2' => '02:00', // 02:00:00 ͱͯ͠ղऍ

    'time3' => 10800, // 10800sec = 03:00:00 'time4' => new DateInterval('PT4H15M45S'), // 04:15:45 'time5' => $a->diff($b), // $a ͱ $b ͸ DateTimeͷΠϯελϯε ]); TimeInterval΁ͷม׵
  29. (string)$timeInterval; // จࣈྻΩϟετ '00:00:00' json_encode($timeInterval); // JSONม׵ '00:00:00' $timeInterval->toSeconds(); //

    ඵ਺औಘ (new DateTime)->add($timeInterval); // OK TimeIntervalΫϥεͷศརͳ࢖͍ํ $BLF1)1͚ͩͰͳ͘୯ମͰ΋࢖͑·͢
  30. // จࣈྻ͔Β $timeInterval = TimeInterval::createFromString('00:00:00'); // DateIntervalΠϯελϯε͔Β $timeInterval = TimeInterval::createFromDateInterval($a->diff($b));

    // ඵ਺͔Β $timeInterval = TimeInterval::createFromSeconds(3600); TimeIntervalΫϥεͷศརͳ࢖͍ํ 5JNF*OUFSWBMΠϯελϯεͷ࡞Γํ
  31. cakephp-time-interval https://github.com/nojimage/cakephp-time-interval

  32. ͓·͚

  33. ‣ 5*.&஋ͷൣғ͸ɺ
 ͔ΒͰ͢ɻ ‣ σϑΥϧτͰ͸ɺ5*.&ൣғ֎ʹ͋Δ͕ɺͦΕҎ֎͸ ༗ޮͳ஋͸ɺൣғͷ΋ͬͱ΋͍ۙऴ఺ʹ੾Γམͱ͞Ε ·͢ɻͨͱ͑͹ɺͱ͸ɺͦ ΕͧΕͱʹม׵͞Ε·͢ɻ MySQL Ͱ

    TIMEܕ https://dev.mysql.com/doc/refman/5.6/ja/time.html
  34. .Z42-Ͱִ࣌ؒؒͱͯ͠5*.&ܕ࢖༻͢Δͷ͸ ΞϯνύλʔϯͰ͢Ͷʜ

  35. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠