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

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

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

TwitterID:@rukiadia

December 11, 2020
Tweet

More Decks by TwitterID:@rukiadia

Other Decks in Programming

Transcript

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

    string Ҿ਺ͷtimestampΛɺҾ਺ͷϑΥʔϚοτจࣈྻʢ$formatʣͰϑΥʔϚοτͯ͠ɺจࣈྻΛฦ͢ɻ echo date("Y-m-d H:i:s”); // 2020-12-12 15:55:00
  2. GPSNBUύϥϝʔλจࣈྻʮ೔ʯᶃ จࣈྻ આ໌ ໭Γ஋ͷྫ % ࡾจࣈͷςΩετܗࣜ ͷ༵೔ɻ .POɺ4VO E ೋܻͷ਺ࣈͷ೔ɻ

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

    ਺஋දݱ͞Ε༵ͨ೔ɻ ʢ݄༵೔ʣʙʢ೔༵೔ʣ Xʢ8ͷখจࣈʣ ༵೔ͷ਺஋ ʢ೔༵೔ʣʙʢ౔༵೔ʣ ͉ʢ;ͷখจࣈʣ ೥ؒͷ௨ࢉ೔ɻ θϩ͔Β࢝·Δɻ ʙ ˞Ӟ೥ͷ৔߹͸·Ͱ
  4. GPSNBUύϥϝʔλจࣈྻʮ೥ʯ จࣈྻ આ໌ ໭Γ஋ͷྫ : ܻͷ਺ࣈͷ೥ ɺ Z ܻͷ਺ࣈͷ೥ ɺ

    - Ӟ೥Ͱ͋Δ͔Ͳ͏͔ ͳΒӞ೥ ͳΒӞ೥Ͱ͸ͳ͍ Pʢ0ͷখจࣈʣ *40ܗࣜͷि൪߸ ͔Βܾఆ͞Εͨ೥ ɺ ˞஋͕૝ఆͱζϨΔ৔߹͕͋Δ
  5. GPSNBUύϥϝʔλจࣈྻʮ࣌ʯᶄ จࣈྻ આ໌ ໭Γ஋ͷྫ ( ࣌ؒ୯Ґͷ࣌ɻ θϩ͸͔ͭͳ͍ɻ ʙ H ࣌ؒ୯Ґͷ࣌ɻ

    θϩ͸͔ͭͳ͍ɻ ʙ ) ࣌ؒ୯Ґͷ࣌ɻ θϩ͕ͭ͘ɻ ʙ I ࣌ؒ୯Ґͷ࣌ɻ θϩ͸ͭ͘ɻ ʙ
  6. GPSNBUύϥϝʔλจࣈྻλΠϜκʔϯࣝผࢠ จࣈྻ આ໌ ໭Γ஋ͷྫ F λΠϜκʔϯࣝผࢠ "TJB5PLZP *ʢ*ͷେจࣈʣ αϚʔλΠϜத͔Ͳ͏͔ ͳΒαϚʔλΠϜதɻ

    ͳΒͦ͏Ͱ͸ͳ͍ɻ 0 (.5ͱͷ࣌ࠩɻ࣌ؒͱ෼ͷ ؒʹίϩϯ͕ೖΒͳ͍ɻ l z 1 (.5ͱͷ࣌ࠩɻ࣌ؒͱ෼ͷ ؒʹίϩϯ͕ೖΔɻ l z 5 λΠϜκʔϯུশ +45
  7. ༨ஊ౾஌ࣝ // 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 !ʯ จࣈྻΛΤεέʔϓ͢Ε͹ɺϑΥʔϚοτจࣈྻͱͯ͠ݟͳ͞Εͳ͘ͳΓ·͢ɻ
  8. TUSUPUJNFؔ਺ strtotime ( string $datetime [, int $now = time()

    ] ) : int ӳจܗࣜͷ೔෇ΛUnixλΠϜελϯϓʢintܕʣʹม׵͢Δγϯϓϧͳؔ਺ɻ Ҿ਺ʹ࢖༻Ͱ͖Δ$datetimeʢ೔෇ͱ࣌ࠁͷॻࣜʣ͸DateTimeɺDateTimeImmutable Ͱڞ௨ͷ΋ͷ͕࢖༻Մೳɻ <?php var_dump(strtotime('2020-12-10') < strtotime(‘now')); // ͜ͷॻ͖ํͰൺֱΛͨ͠ܦݧ͕͋Δਓ͸ଟ͍͸ͣ
  9. ೔෇΍࣌ࠁͷՃݮࢉ <?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 ػೳࣗମ͸ඇৗʹγϯϓϧ
  10. %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
  11. λΠϜελϯϓͷมߋ <?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ΑΓ΋ ࣗ༝౓͕ߴ͍
  12. ೔෇ͱ࣌ࠁͷॻࣜ࣌ࠁ $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
  13. ೔෇ͱ࣌ࠁͷॻࣜ೔෇ $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’);
  14. ೔෇ͱ࣌ࠁͷॻࣜෳ߹తͳॻࣜ $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
  15. ೔෇ͱ࣌ࠁͷॻࣜ૬ରతͳॻࣜᶃ /* 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
  16. ೔෇ͱ࣌ࠁͷॻࣜ૬ରతͳॻࣜᶄ $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
  17. ૝ఆ֎ͷղऍ $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’); // ??
  18. ૝ఆ֎ͷղऍछ໌͔͠ $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
  19. ༨ஊαϚʔλΠϜͷ᠘ // 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
  20. ͜Ε·Ͱͷ೔෇ൺֱ // dateಉ࢜Λൺֱͨ͠Γɺstrtotimeؔ਺ͷ݁Ռಉ࢜Λൺֱͨ͠Γ $targetDate = date('2020-12-12'); var_dump(strtotime($targetDate) < strtotime(‘now’)); //

    MEMO: strtotime͸UnixλΠϜελϯϓΛฦ͢ͷͰɺλΠϜκʔϯ͸UTCݻఆʹͳͬͯ͠·͏ɻ // MEMO: date͸ฦΓ஋͕StringͳͷͰɺฦΓ஋ͷՃ޻͕ෆศɻ
  21. ͜Ε͔Βͷ೔෇ൺֱᶃ $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ΦϒδΣΫτಉ࢜ ΛൺֱՄೳʂʂ
  22. ͜Ε͔Βͷ೔෇ൺֱᶄ // 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ʁ
  23. ͜Ε͔Βͷ೔෇ൺֱᶄ $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
  24. ͜Ε͔Βͷ೔෇ൺֱᶅ $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 ࠩ෼΋ݕग़Մೳʂʂ
  25. Πϛϡʔλϒϧͳ%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'); // ??
  26. Πϛϡʔλϒϧͳ%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 Πϯελϯεͷঢ়ଶ͕ෆม
  27. ೔ຊޠ΁ͷϩʔΧϥΠζʲૉͷ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ʢ౔༵೔ʣ // ͜ΕΒΛ༵೔ʹม׵͢Δؔ਺Λࣗ࡞͢Δํ๏΋ΞϦ
  28. ೔ຊޠ΁ͷϩʔΧϥΠζʲ$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; // ౔༵೔ ೔ຊޠͰදࣔͰ͖Δʂʂ
  29. ςετίʔυͷϞοΫΞοϓΛ͢Δ // ςετίʔυ࣮ߦલʹ೔࣌Λݻఆ͠ɺظ଴஋Λ੍ޚ͢Δɻ 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Ͱ΋ಉ༷ͷػೳ͕ ఏڙ͞Ε͍ͯΔͷͰ҆৺ʂ