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

今こそ理解する PHPの日時計算 / Understand date manipulation of PHP

今こそ理解する PHPの日時計算 / Understand date manipulation of PHP

438799c739f93d00abc30fa2da33c543?s=128

TwitterID:@rukiadia

December 11, 2020
Tweet

Transcript

  1. ࠓͦ͜ཧղ͢Δ 1)1ͷ೔࣌ܭࢉ ʮ1)1$POGFSFODF+BQBO3FCPSOʯ IUUQTQIQDPOQIQHSKQ

  2. ࣗݾ঺հ w ࢁాɹ঵ʢ΍·ͩɹ͠ΐ͏ʣ w ίωώτגࣜձࣾͰαʔόʔαΠυΤϯδχ ΞΛ΍͓ͬͯΓ·͢ɻ w Ұ൪ಘҙͳͷ͸ɺ1)1Ͱ͸ͳ͘$44Ͱ͢ɻ

  3. ૣ଎Ͱ͕͢

  4. 1)1ͷ೔෇ܭࢉͬͯ Ͳ͏͢Δͷ͕ϕετͳΜͩΖ͏ɾɾʁ

  5. ʮQIQ೔෇ʯʮQIQ೔෇ܭࢉʯ Ͱݕࡧͨ͠ܦݧ͸͋Γ·ͤΜ͔ʁ

  6. TUSUPUJNFʁ EBUF EBUFUJNFʁ

  7. ࠓ೔ͷൃදͰɺ࣮૷ͷ໎͍Λ গ͠Ͱ΋ݮΒͤΕ͹خ͍͠Ͱ͢ɻ

  8. ຊ೔࿩͢͜ͱ w EBUFؔ਺ͱϑΥʔϚοτॻࣜʹ͍ͭͯ w %BUF5JNFΫϥεɺ%BUF5JNF*NNVUBCMFΫϥεʹ͍ͭͯ w ೔෇΍࣌ࠁͷϥΠϒϥϦʹ͍ͭͯ

  9. ͦΕͰ͸ɺຊฤελʔτͰ͢ɻ

  10. EBUFؔ਺ͱ ϑΥʔϚοτॻࣜ

  11. ·ͣ͸EBUFؔ਺ʹ͍ͭͯ஌Γ·͠ΐ͏

  12. EBUFؔ਺ʹ͍ͭͯ date (string $format [, int $timestamp = time()]) :

    string Ҿ਺ͷtimestampΛɺҾ਺ͷϑΥʔϚοτจࣈྻʢ$formatʣͰϑΥʔϚοτͯ͠ɺจࣈྻΛฦ͢ɻ echo date("Y-m-d H:i:s”); // 2020-12-12 15:55:00
  13. ͱ͜ΖͰϑΥʔϚοτจࣈྻͬͯʁ

  14. GPSNBUύϥϝʔλจࣈྻͱ͸ʁ w ͦͷ໊ͷ௨Γɺ೔෇ͷϑΥʔϚοτʹ༻͍Δɻ w ޙड़͢Δ%BUF5JNFɺ%BUF5JNF*NNVUBCMFΫϥεͰ΋࢖༻Մೳɻ w ೔ɺिɺ݄ɺ೥ɺ࣌ɺλΠϜκʔϯͳͲ༷ʑͳछྨ͕͋Δɻ ˠ࣍ϖʔδҎ߱Ͱઆ໌͍͖ͯ͠·͢ɻ ࢀߟIUUQTXXXQIQOFUNBOVBMKBEBUFUJNFGPSNBUQIQ

  15. GPSNBUύϥϝʔλจࣈྻʮ೔ʯᶃ จࣈྻ આ໌ ໭Γ஋ͷྫ % ࡾจࣈͷςΩετܗࣜ ͷ༵೔ɻ .POɺ4VO E ೋܻͷ਺ࣈͷ೔ɻ

    ઌ಄ʹθϩ͕ͭ͘ɻ ɺ K ೋܻͷ਺ࣈͷ೔ɻ ઌ಄ʹθϩ͕͔ͭͳ͍ɻ ɺ Mʢ-ͷখจࣈʣ ϑϧεϖϧܗࣜͷ༵೔ɻ 4VOEBZɺ.POEBZɺ 5VFTEBZɾɾ
  16. GPSNBUύϥϝʔλจࣈྻʮ೔ʯᶄ จࣈྻ આ໌ ໭Γ஋ͷྫ 4 ӳޠܗࣜͷং਺ɻ ΞϧϑΝϕοτจࣈɻ TUɺOEɺSEɺUI / *40ܗࣜͰ

    ਺஋දݱ͞Ε༵ͨ೔ɻ ʢ݄༵೔ʣʙʢ೔༵೔ʣ Xʢ8ͷখจࣈʣ ༵೔ͷ਺஋ ʢ೔༵೔ʣʙʢ౔༵೔ʣ ͉ʢ;ͷখจࣈʣ ೥ؒͷ௨ࢉ೔ɻ θϩ͔Β࢝·Δɻ ʙ ˞Ӟ೥ͷ৔߹͸·Ͱ
  17. GPSNBUύϥϝʔλจࣈྻʮिʯ LP จࣈྻ આ໌ ໭Γ஋ͷྫ 8ʢେจࣈʣ *40ܗࣜͰ ݄༵೔࢝·Γͷि൪߸ ʢ೥ͷୈि໨ʣ ※

    ISO-8601ܗࣜͷ࢓༷ʹบ͕͋Γ·͢ɻ࣍ͷεϥΠυͰղઆ͠·͢ɻ
  18. िͱ༵೔ͷܾΊ͝ͱʢ*40ʣ w ͢΂ͯͷि͸࢝·Γ͸ʮ݄༵೔ʯ w ͋Δ೥ͷ࠷ॳͷि͸ͦͷ೥ͷ࠷ॳͷ໦༵೔ΛؚΉिɻ w ྫ͸ʮՐ༵೔ʯɻि൪߸͸͍ͭ͘ʁ w ೥݄೔͸ʮ౔༵೔ʯ

  19. GPSNBUύϥϝʔλจࣈྻʮ݄ʯᶃ จࣈྻ આ໌ ໭Γ஋ͷྫ ' ϑϧεϖϧͷ݄ɻ +BOVBSZʙ%FDFNCFS . จࣈܗࣜͷ݄ɻ +BOʙ%FD

  20. GPSNBUύϥϝʔλจࣈྻʮ݄ʯᶄ จࣈྻ આ໌ ໭Γ஋ͷྫ N ਺ࣈͰ݄Λදݱɻ ઌ಄ʹθϩ͕ͭ͘ɻ ʙ O ਺ࣈͰ݄Λදݱɻ

    ઌ಄ʹθϩ͕͔ͭͳ͍ɻ ʙ U ࢦఆ݄ͨ͠ͷ݄਺ ɺɺɺ
  21. GPSNBUύϥϝʔλจࣈྻʮ೥ʯ จࣈྻ આ໌ ໭Γ஋ͷྫ : ܻͷ਺ࣈͷ೥ ɺ Z ܻͷ਺ࣈͷ೥ ɺ

    - Ӟ೥Ͱ͋Δ͔Ͳ͏͔ ͳΒӞ೥ ͳΒӞ೥Ͱ͸ͳ͍ Pʢ0ͷখจࣈʣ *40ܗࣜͷि൪߸ ͔Βܾఆ͞Εͨ೥ ɺ ˞஋͕૝ఆͱζϨΔ৔߹͕͋Δ
  22. GPSNBUύϥϝʔλจࣈྻʮ࣌ʯᶃ จࣈྻ આ໌ ໭Γ஋ͷྫ B ޕલɺ·ͨ͸ޕޙ ʢখจࣈʣ BNɺQN " ޕલɺ·ͨ͸ޕޙ

    ʢେจࣈʣ ".ɺ1. # 4XBUDIΠϯλʔωοτ࣌ؒ ʙ
  23. GPSNBUύϥϝʔλจࣈྻʮ࣌ʯᶄ จࣈྻ આ໌ ໭Γ஋ͷྫ ( ࣌ؒ୯Ґͷ࣌ɻ θϩ͸͔ͭͳ͍ɻ ʙ H ࣌ؒ୯Ґͷ࣌ɻ

    θϩ͸͔ͭͳ͍ɻ ʙ ) ࣌ؒ୯Ґͷ࣌ɻ θϩ͕ͭ͘ɻ ʙ I ࣌ؒ୯Ґͷ࣌ɻ θϩ͸ͭ͘ɻ ʙ
  24. GPSNBUύϥϝʔλจࣈྻʮ࣌ʯᶅ จࣈྻ આ໌ ໭Γ஋ͷྫ J ෼ɻθϩ͕ͭ͘ɻ ʙ T ඵɻθϩ͕ͭ͘ɻ ʙ

    V ϚΠΫϩඵ  W ϛϦඵ ˞1)1Ҏ߱  ˞͢΂ͯখจࣈ
  25. GPSNBUύϥϝʔλจࣈྻλΠϜκʔϯࣝผࢠ จࣈྻ આ໌ ໭Γ஋ͷྫ F λΠϜκʔϯࣝผࢠ "TJB5PLZP *ʢ*ͷେจࣈʣ αϚʔλΠϜத͔Ͳ͏͔ ͳΒαϚʔλΠϜதɻ

    ͳΒͦ͏Ͱ͸ͳ͍ɻ 0 (.5ͱͷ࣌ࠩɻ࣌ؒͱ෼ͷ ؒʹίϩϯ͕ೖΒͳ͍ɻ l z 1 (.5ͱͷ࣌ࠩɻ࣌ؒͱ෼ͷ ؒʹίϩϯ͕ೖΔɻ l z 5 λΠϜκʔϯུশ +45
  26. ϑΥʔϚοτจࣈྻΛ၆ᛌͨ͠ײ૝ w ࢖͏ػձ͕͋·Γͳͦ͞͏ͳจࣈྻ͕͍͔ͭ͋͘Δɻ w ೔ຊࠃ಺ͳΒɺαϚʔλΠϜͷҙࣝ͸ෆཁɻ w େจࣈখจࣈͰҙຯ͕େ͖͘มΘΔจࣈ͕΍΍͍͜͠ɻ w ʮPʯͱʮ0ʯͱ͔ɻ w

    ༵೔΍݄Λ೔ຊޠදهͰग़ྗͰ͖ͳͯ͘ෆศɻ
  27. บ͕͋Δͱײ͡Δͱ͜Ζ w ͕͔ͭ͘Ͳ͏͔ͷҧ͍ w ೔ͷʮEʯʮKʯɺ݄ͷʮNʯʮOʯ w िͷ࢝·Γ͕ʮ೔༵೔ʯ͔ʮ݄༵೔ʯͷͭͷύλʔϯ w *40ܗࣜͷʮ/ʯͩͱʮ݄༵೔ʯ w

    64"ํࣜͷʮXʯͩͱʮ೔༵೔ʯ w ˞1)1͕ѱ͍Θ͚Ͱ͸ͳ͘ɺ೔෇ͱ࣌ࠁͷදه͕ͦ΋ͦ΋ෳࡶɻ
  28. ༨ஊ౾஌ࣝ // MEMO: จࣈྻΛϑΥʔϚοτจࣈྻͱͯ͠ѻ͍ͨ͘ͳ͍৔߹ͷখٕ // 2020೥12݄12೔ʹ࣮ߦ date_default_timezone_set('Asia/Tokyo'); echo date("\T\o\d\a\y \i\s

    Y-m-d l \!"); // ʮToday is 2020-12-12 Saturday !ʯ จࣈྻΛΤεέʔϓ͢Ε͹ɺϑΥʔϚοτจࣈྻͱͯ͠ݟͳ͞Εͳ͘ͳΓ·͢ɻ
  29. %BUF5JNFΫϥε %BUF5JNF*NNVUBCMFΫϥεͷ࿩

  30. ਖ਼௚ݴ͏ͱɺ ͜ͷ2ͭΛ࢖͓ͬͯ͘ͷ͕҆ṛ ޙड़͠·͕͢ɺ ׬શͰ͸ͳ͍Ͱ͢ɻ

  31. TUSUPUJNFؔ਺ strtotime ( string $datetime [, int $now = time()

    ] ) : int ӳจܗࣜͷ೔෇ΛUnixλΠϜελϯϓʢintܕʣʹม׵͢Δγϯϓϧͳؔ਺ɻ Ҿ਺ʹ࢖༻Ͱ͖Δ$datetimeʢ೔෇ͱ࣌ࠁͷॻࣜʣ͸DateTimeɺDateTimeImmutable Ͱڞ௨ͷ΋ͷ͕࢖༻Մೳɻ <?php var_dump(strtotime('2020-12-10') < strtotime(‘now')); // ͜ͷॻ͖ํͰൺֱΛͨ͠ܦݧ͕͋Δਓ͸ଟ͍͸ͣ
  32. %BUF5JNFΫϥε ೔෇࣌ࠁΛఆٛ͢ΔΫϥεͰ͢ɻ ೔෇΍࣌ࠁͷՃݮࢉɺൺֱɺλΠϜκʔϯઃఆɺϑΥʔϚοτग़ྗͳͲ͕ߦ͑Δϝιου͕࣮૷͞Ε͍ͯ·͢ɻ <?php $date = new DateTime('now', new DateTimeZone('Asia/Tokyo'));

    $date->add(new DateInterval(‘P2Y3D')); $date->modify('+1 day'); echo $date->format('Y-m-d H:i:s'); ઌఔઆ໌ͨ͠ ϑΥʔϚοτจࣈྻ
  33. %BUF5JNFΫϥεͷศརͳͱ͜Ζʂ w ೔෇΍࣌ࠁͷՃݮࢉɺλΠϜελϯϓͷมߋɻ w EBUFؔ਺ͱಉ͡ϑΥʔϚοτܗࣜɻ w %BUF5JNFΦϒδΣΫτಉ࢜ΛൺֱՄೳͰɺࠩ෼ݕग़΋Մೳɻ

  34. ̍ͭͣͭݟ͍͖ͯ·͠ΐ͏

  35. ೔෇΍࣌ؒͷՃݮࢉ

  36. ೔෇΍࣌ࠁͷՃݮࢉ <?php $date = new DateTime(‘2020-12-12’); // DateTimeΦϒδΣΫτʹ2೥ͱ3೥ΛՃ͑Δɻ $date->add(new DateInterval(‘P2Y3D'));

    // DateTimeΦϒδΣΫτ͔Β1೥ɺ1ϲ݄ɺ1೔ΛҾ͘ɻ $date->sub(new DateInterval('P1Y1M1D')); echo $date->format(‘Y-m-d'); // 2021-11-14 w %BUF5JNFBEE w %BUF5JNFTVC ػೳࣗମ͸ඇৗʹγϯϓϧ
  37. %BUF*OUFSWBMΫϥεͱ͸ʁ

  38. %BUF*OUFSWBMΫϥεͱ͸ ೔෇ͷִؒΛදݱ͢ΔΫϥεͰ͢ɻ࢖͑Δํࣜ͸೥ɺ݄ɺ೔ɺ࣌ؒɺ෼ɺඵɺϚΠΫϩඵͳͲɻ P(Periodͷུ) Y(೥) M(݄) D(೔) Hʢ࣌ʣIʢ෼ʣSʢඵʣFʢϚΠΫϩඵʣ ʮେ͖͍୯Ґ͸ࠨʹએݴʯʮ਺஋͸୯Ґͷલʹॻ͘ʯͱ͍͏2ͭͷϧʔϧ͕͋Γ·͢ɻ <?php $date

    = new DateTime('2020-12-12 15:00:00', new DateTimeZone('Asia/Tokyo')); $date->add(new DateInterval('P1Y2M3D')); // 1೥ɺ2ϲ݄ɺ3೔ΛՃࢉ $date->add(new DateInterval('PT6H4M10S')); // 6࣌ؒɺ4෼ɺ10ඵΛՃࢉ echo $date->format('Y-m-d H:i:s.u'); // 2022-02-15 21:04:10.000000
  39. λΠϜελϯϓͷมߋ <?php $date = new DateTime( '2020-12-12', new DateTimeZone('Asia/Tokyo') );

    // $modifierʢ೔෇/࣌ࠁ จࣈྻʣΛҾ਺ʹΘͨ͢ $date->modify('last day of next month'); echo $date->format(‘Y-m-d'); // 2021-01-31 w %BUF5JNFNPEJGZ BEEɺTVCΑΓ΋ ࣗ༝౓͕ߴ͍
  40. ೔෇ͱ࣌ࠁͷॻࣜʹ͍ͭͯ w ࣌ࠁͷॻࣜ w ೔෇ͷॻࣜ w ෳ߹తͳॻࣜ w ૬ରతͳॻࣜ

  41. ೔෇ͱ࣌ࠁͷॻࣜ࣌ࠁ $date = new DateTime(‘T151130'); // ೔෇Λলུ͢Δͱɺࠓͷ೔෇ʹͳΔ echo $date->format('Y-m-d H:i:s’);

    // 2020-12-12 15:11:30 $date = new DateTime(‘7:19:19 PM’); // ώϡʔϚϯϦʔμϒϧ echo $date->format('Y-m-d H:i:s’); // 2020-12-12 19:19:19 $date = new DateTime(‘2010'); // ※ ඇਪ঑ echo $date->format('Y-m-d H:i:s’); // 2020-12-12 00:00:00
  42. ೔෇ͱ࣌ࠁͷॻࣜ೔෇ $date = new DateTime('2020-12-12'); echo $date->format('Y-m-d H:i:s’); // 2020-12-12

    00:00:00 $date = new DateTime('December 12th, 2020’); // ӳޠݍͷදه echo $date->format('Y-m-d H:i:s’); $date = new DateTime(’12/12/20'); // ΞϝϦΧࣜͷ೥݄೔ʢޙड़͠·͕͢ɺ᠘͕͋Γ·͢ʣ echo $date->format('Y-m-d H:i:s’);
  43. ೔෇ͱ࣌ࠁͷॻࣜෳ߹తͳॻࣜ $date = new DateTime('2020-12-12 15:00:00’); // MySQLͷDateॻࣜ echo $date->format('Y-m-d

    H:i:s’); $date = new DateTime(‘2020-W51'); // ISO ͷ೥ɺि൪߸ echo $date->format('Y-m-d H:i:s’); // 2020-12-14 00:00:00 $date = new DateTime('12/Dec/2020:15:00:00 +0900’); // Apacheϩάʹ͓͚Δ࣌ࠁॻࣜ echo $date->format('Y-m-d H:i:s’); // 2020-12-12 15:00:00
  44. ೔෇ͱ࣌ࠁͷॻࣜ૬ରతͳॻࣜᶃ /* modifyؔ਺ʹ࢖͏ͷ͸ɺجຊతʹ͜ͷॻࣜͰ͢ */ $date = new DateTime('last day’); //

    ʮ࠷ޙͷ೔ʯͰ͸ͳ͘ʮࡢ೔ʯ echo $date->format(‘Y-m-d’); // 2020-12-11 $date = new DateTime('last day of Feb 2020’); // ʮࡢ೔ʯͰ͸ͳ͘ʮ݄ͷ࠷ऴ೔ʯ echo $date->format(‘Y-m-d'); // 2020—02-29 $date = new DateTime('2020-12-12'); $date->modify('-1 week’); echo $date->format(‘Y-m-d’); // 2020-12-05
  45. ೔෇ͱ࣌ࠁͷॻࣜ૬ରతͳॻࣜᶄ $date = new DateTime('2020-12-12'); $date->modify(‘Monday'); // dayname ʮ࣍ʹ΍ͬͯ͘Δࢦఆ͞Ε༵ͨ೔ʯ※᠘͕͋Γ·͢ echo

    $date->format('Y-m-d l’); // 2020-12-14 Monday $date = new DateTime(‘2020-12-12’); // ౔༵೔ echo $date->format(‘Monday this week’); // weekday + last/this/next week echo $date->format('Y-m-d l'); // 2020-12-07 Monday $date = new DateTime('2020-12-12'); $date->modify('Monday +7 weeks’); // ॻࣜΛ૊Έ߹ΘͤΔ͜ͱ΋Մೳ echo $date->format('Y-m-d l’); // 2021-02-01 Monday
  46. ศརͳͷ͸ؒҧ͍ͳ͍Ͱ͢ɻ Ͱ͕͢ɺ᠘΋͋Γ·͢ɻ

  47. ૝ఆ֎ͷղऍ $date = new DateTime(‘2500'); echo $date->format('Y-m-d H:i:s’); // ??

    $date = new DateTime('2020-01-31'); $date->modify('+1 month’); echo $date->format(‘Y-m-d'); // ?? $date = new DateTime('00/07/20'); echo $date->format('Y-m-d’); // ??
  48. ૝ఆ֎ͷղऍछ໌͔͠ $date = new DateTime(‘2500’); // 2459Λ௒͑Δͱɺʮ೥ʯͩͱݟͳ͞ΕΔɻ echo $date->format('Y-m-d H:i:s’);

    // 2500-12-12 15:00:00 $date = new DateTime(‘2020-01-31'); $date->modify('+1 month'); echo $date->format(‘Y-m-d'); // 2020–02-31͕ΦʔόʔϑϩʔΛىͯ͜͠ɺ2020-03-02ʹͳΔɻ // ΞϝϦΧࣜͷ೔෇ղऍͱݟͳ͞ΕΔ $date = new DateTime(’00/07/20'); // 2020-00-07 echo $date->format('Y-m-d’); // 2019-12-07 ɾZZNNEE ɾNNEEZ
  49. ༨ஊαϚʔλΠϜͷ᠘ // MEMO: ΞϝϦΧͷ2020೥ͷαϚʔλΠϜͷऴΘΓ͸ɺ11݄1೔ͷਂ໷2࣌ // ޕલ2͔࣌Βޕલ1࣌ʹɺ1͚࣌ؒͩ໭Γ·͢ $dt = new DateTimeImmutable(

    '2020-11-01 01:00:00', new DateTimeZone('America/New_York') ); echo $dt->add(new DateInterval('PT10H'))->format('Y-m-d H:i:s’); // 2020-11-01 10:00:00 echo $dt->modify('+10 hours')->format('Y-m-d H:i:s’); // 2020-11-01 11:00:00
  50. ެࣜυΩϡϝϯτʹࡌ͍ͬͯΔ࿩ ͹͔ΓͳͷͰɺ͝Ұಡ͋Εʂ

  51. %BUF5JNFΦϒδΣΫτͷൺֱ

  52. ͜Ε·Ͱͷ೔෇ൺֱ // dateಉ࢜Λൺֱͨ͠Γɺstrtotimeؔ਺ͷ݁Ռಉ࢜Λൺֱͨ͠Γ $targetDate = date('2020-12-12'); var_dump(strtotime($targetDate) < strtotime(‘now’)); //

    MEMO: strtotime͸UnixλΠϜελϯϓΛฦ͢ͷͰɺλΠϜκʔϯ͸UTCݻఆʹͳͬͯ͠·͏ɻ // MEMO: date͸ฦΓ஋͕StringͳͷͰɺฦΓ஋ͷՃ޻͕ෆศɻ
  53. ͜Ε͔Βͷ೔෇ൺֱᶃ $dt = new DateTime('2020-12-25'); var_dump($dt < new DateTime(‘2020-12-12‘)); //

    bool(false) $dt = new DateTime(‘2020-12-10'); var_dump($dt < new DateTime(‘2020-12-12’)); // bool(true) %BUF5JNFΦϒδΣΫτಉ࢜ ΛൺֱՄೳʂʂ
  54. ͜Ε͔Βͷ೔෇ൺֱᶄ // Question: ΠίʔϧͰൺֱͯ͠΋ͪΌΜͱಈ͘ʁ $dt = new DateTime('2020-12-12 15:00:00'); var_dump($dt

    == new DateTime('2020-12-12 15:00:00’)); // ?? var_dump($dt === new DateTime('2020-12-12 15:00:00’)); // ?? USVFʁGBMTFʁ
  55. ͜Ε͔Βͷ೔෇ൺֱᶄ $dt = new DateTime('2020-12-12 15:00:00'); var_dump($dt == new DateTime('2020-12-12

    15:00:00’)); // true var_dump($dt === new DateTime('2020-12-12 15:00:00’)); // false // MEMO: === ͸ಉ͡Ϋϥεͷಉ͡ΠϯελϯεΛൺֱͨ͠৔߹Ͱͳ͍ͱɺtrueʹ͸ͳΒͳ͍ɻ // https://www.php.net/manual/ja/language.oop5.object-comparison.php
  56. ͜Ε͔Βͷ೔෇ൺֱᶅ $dt = new DateTime('2020-12-12'); $diff = $dt->diff(new DateTime('last day

    of Dec 2020')); echo $diff->format(‘%a’); // 19 echo $diff->format('%R%a days'); // +19 days // a: diffؔ਺ͷ݁Ռʹ࢖༻ͨ͠৔߹ɺ૯೔਺ // R: ϓϥε͔ϚΠφε // https://www.php.net/manual/ja/dateinterval.format.php ࠩ෼΋ݕग़Մೳʂʂ
  57. %BUF5JNF*NNVUBCMFΫϥε

  58. Πϛϡʔλϒϧͳ%BUF5JNFΫϥε $dt = new DateTimeImmutable('2020-12-12'); echo $dt->modify('+1 week’)->format(‘Y-m-d’); // ??

    echo $dt->modify('+1 day’)->format(‘Y-m-d'); // ?? $dt = new DateTime('2020-12-12'); echo $dt->modify('+1 week’)->format('Y-m-d'); // ?? echo $dt->modify('+1 day’)->format('Y-m-d'); // ??
  59. Πϛϡʔλϒϧͳ%BUF5JNFΫϥε $dt = new DateTimeImmutable('2020-12-12'); echo $dt->modify('+1 week’)->format(‘Y-m-d’); // 2020-12-19

    echo $dt->modify('+1 day’)->format(‘Y-m-d'); // 2020-12-13 $dt = new DateTime('2020-12-12'); echo $dt->modify('+1 week’)->format('Y-m-d'); // 2020-12-19 echo $dt->modify('+1 day’)->format('Y-m-d'); // 2020-12-20 Πϯελϯεͷঢ়ଶ͕ෆม
  60. த਎ͷՃ޻͕ෆཁͳΒ %BUF5JNF*NNVUBCMFΫϥε

  61. ೔෇΍࣌ࠁͷϥΠϒϥϦ

  62. %BUF5JNFΫϥεͰ ࿫͑ͳ͍͜ͱ΋࣮͸͋Γ·͢ɻ

  63. %BUF5JNFΫϥεͰͰ͖ͳ͍͜ͱ w ೔ຊޠ΁ͷϩʔΧϥΠζ w ςετίʔυ࣮૷࣌ͷϞοΫΞοϓ

  64. ͦΕΒΛղܾ͢ΔϥΠϒϥϦ܊ w $BSCPO w IUUQTHJUIVCDPNCSJBOOFTCJUU$BSCPO w $BLF1)1$ISPOPT w IUUQTHJUIVCDPNDBLFQIQDISPOPT w

    ˞$BLF1)1ʹݶఆ͞Εͨ࿩ଟΊͳͷͰɺࠓճ͸$BSCPOͷ࿩ΛϝΠ ϯʹ͠·͢
  65. ೔ຊޠ΁ͷϩʔΧϥΠζʲૉͷ1)1ฤʳ // ೔ຊޠද͍͚ࣔͨ͠ͲͰ͖ͳ͍ $dt = new DateTime('2020-12-12', new DateTimeZone('Asia/Tokyo')); echo

    $dt->format('Y-m-d F l’); // 2020-12-12 December Saturday $dt->format(’N w’); // 6 6 // MEMO: N͸1ʢ݄༵೔ʣʙ7ʢ೔༵೔ʣɺw͸1ʢ೔༵೔ʣʙ6ʢ౔༵೔ʣ // ͜ΕΒΛ༵೔ʹม׵͢Δؔ਺Λࣗ࡞͢Δํ๏΋ΞϦ
  66. ೔ຊޠ΁ͷϩʔΧϥΠζʲ$BSCPOฤʳ // MEMO: Carbon v2.42.0ͷ࣮ߦ݁Ռ $carbon = new Carbon('2020-12-12'); $date

    = $carbon->locale('ja'); echo $date->locale(); // ja echo $date->monthName; // 12݄ echo $date->dayName; // ౔༵೔ ೔ຊޠͰදࣔͰ͖Δʂʂ
  67. %BUF5JNFͱͷޓ׵ੑ͸͋Δͷʁ w ͋Γ·͢ɻ%BUF5JNFΫϥεͱ%BUF5JNF*NNVUBCMFΫϥεͷৼΔ෣ ͍͸ͦͷ··࢖͑·͢ɻ w $BSCPO͸%BUF5JNFΫϥεͷ֦ுɻ w $BLF1)1$ISPOPT͸%BUF5JNF*NNVUBCMFΫϥεͷ֦ுɻ w ࡶͳදݱΛ͢ΔͱɺͭͷϥΠϒϥϦ͸%BUF5JNFͷ্Ґޓ׵Ͱ͢ɻ

  68. ༨ஊ$BLF1)1͕$BSCPOΛ࢖ͬͯͳ͍ཧ༝͸ʁ w $BLF1)1͸·Ͱ$BSCPOΛ࢖͍ͬͯͨɻ w ͔͠͠ɺ$BSCPO͸όʔδϣϯܥͷࠒ͸ϛϡʔλϒϧͳΦϒδΣΫτ ͔͠࡞Εͳ͔ͬͨɻ w $ISPOPT͕%BUF5JNF*NNVUBCMFΫϥεͷ֦ு࿏ઢͰ։ൃ͞Εͨͷ͸ ͦͷӨڹɻ w

    $BSCPOܥͰ͸ΠϛϡʔλϒϧͳΦϒδΣΫτΛѻ͑ΔΑ͏ʹͳͬ ͍ͯΔɻ
  69. ͯ͞ɺ͕࣍࠷ޙͷτϐοΫͰ͢

  70. ೔͕࣌བྷΉςετίʔυ

  71. ೔͕࣌བྷΉςετίʔυ͸೉͍͠ w ྫ࣌Λա͗Δલ͸ʮ͜Μʹͪ͸ʯɻ࣌Λա͗ͨޙ͸ʮ͜Μ͹ Μ͸ʯΛฦؔ͢਺͕͋Δͱ͠·͢ɻ w ͜ͷؔ਺ͷςετίʔυΛ࣌ʹಈ͔͢ͱʮ͜Μʹͪ͸ʯ͕ظ଴஋ ʹͳΓ·͕͢ɺ࣌ʹಈ͔͢ͱʮ͜Μ͹Μ͸ʯ͕ظ଴஋ʹͳΓ·͢ɻ w ࢀߟIUUQTUXBEBIBUFOBCMPHKQFOUSZEFTJHOGPSUFTUBCJMJUZ

  72. ςετίʔυ͸ RepeatableͰͳ͚Ε͹ͳΒͳ͍

  73. ςετίʔυͷϞοΫΞοϓΛ͢Δ // ςετίʔυ࣮ߦલʹ೔࣌Λݻఆ͠ɺظ଴஋Λ੍ޚ͢Δɻ Carbon::setTestNow(new Carbon('2020-12-12 17:00:00’)); $result = greet(); $this->assertEquals($result,

    ‘͜Μʹͪ͸'); // True Carbon::setTestNow(new Carbon('2020-12-12 19:00:00')); $result = greet(); $this->assertEquals($result, ‘͜Μ͹Μ͸'); // False $ISPOPTͰ΋ಉ༷ͷػೳ͕ ఏڙ͞Ε͍ͯΔͷͰ҆৺ʂ
  74. %BUF5JNFΫϥε ͍͚֮͑ͯͨͩ·͔ͨ͠ʁ

  75. ·ͱΊ

  76. ·ͱΊ w ೔࣌Λѻ͏࣌͸%BUF5JNF*NNVUBCMFΫϥεΛ࢖͏΂͠ɻ w ެࣜυΩϡϝϯτͰʮϑΥʔϚοτจࣈྻʯ΍ʮ೔෇΍࣌ࠁͷॻࣜʯ ͷบΛ஌͓ͬͯ͘΂͠ɻ w αϚʔλΠϜͷ࿩΋ॻ͍ͯ͋Γ·͢ɻ w %BUF5JNFΫϥεͷ஌ࣝ͸ϥΠϒϥϦͰ΋ͦͷ··׆͔ͤΔɻ

    w ੵۃతʹϥΠϒϥϦΛཔΔ΂͠ɻ
  77. ࢿྉ΍ൃදʹର͢ΔϑΟʔυόοΫΛ ͓଴͓ͪͯ͠Γ·͢ɻ

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