🍑
[PHP]strtotimeを使わずに前日、翌日をもとめる
概要
- strtotimeだと、2038年問題があるため、代わりにDatetimeを使って前日、翌日を計算してみたので覚書き
コード
testDate.php
<?php
$lastDate = '2022/02/28';
$nextDateObj = new DateTime($lastDate.'+1 day');
$nextDate = $nextDateObj->format('Y/m/d');
echo $lastDate."の翌日は".$nextDate."です\n";
$firstDate = '2024/03/01';
$prevDateObj = new DateTime($firstDate .'-1 day');
$prevDate = $prevDateObj->format('Y/m/d');
echo $firstDate."の前日は".$prevDate."です\n";
出力結果
実行コマンドと出力結果は以下の通り
$ php testDate.php
2022/02/28の翌日は2022/03/01です
2024/03/01の前日は2024/02/29です
うまくいきました
Discussion
You know, when it comes to mobile development, I've really seen some impressive strides forward. It's fascinating how we've tackled tricky issues like date calculations. Back in the day, I used to rely on strtotime, but boy, did it give me headaches, especially when dealing with dates beyond 2038.
But now, in my personal opinion, things have gotten much better. With tools like Datetime at our disposal, we can calculate the previous and next day without breaking a sweat. It's a relief not to have that 2038 problem hanging over our heads anymore. This just shows how modern mobile development practices have evolved to prioritize reliability, and I'm all for it: https://andersenlab.com/services/mobile-development