1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

strtotime に妥当性の判断を任せてはいけない

Posted at

<?php


function test($date) {
	$work = date('Y/m/d', strtotime($date));
	echo $date === $work ? 'OK ' : "NG ($date -> $work)" ;
	echo "";
	echo "\n";
}

$testdata = [

	  '2008/02/29' 			// うるう年の年 ウチの息子の誕生日
	, '2015/02/29' 			// うるう年じゃない年 不正な日付にならずに次の日になる

	, '2015/03/32' 			// 不正な日付の扱いを受ける
	, '2015/06/31' 			// 不正な日付にならずに次の日になる 

	, '2015/06/30 23:59' 	// 正常 
	, '2015/06/30 24:00' 	// 正常 次の日になる
	, '2015/06/30 24:59' 	// ここまでは大丈夫
	, '2015/06/30 25:00' 	// 不正な日付の扱いを受ける

];

foreach ($testdata as $date) {
	test($date);
}

// OK 
// NG (2015/02/29 -> 2015/03/01)
// NG (2015/03/32 -> 1970/01/01)
// NG (2015/06/31 -> 2015/07/01)
// NG (2015/06/30 23:59 -> 2015/06/30)
// NG (2015/06/30 24:00 -> 2015/07/01)
// NG (2015/06/30 24:59 -> 2015/07/01)
// NG (2015/06/30 25:00 -> 1970/01/01)

妥当性の判断は別にちゃんとやる。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?