忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/26 21:02 |
paluの忘却録『phpで特定の日付からある日数分前の日付一覧を取得する』の感想
http://d.hatena.ne.jp/palu0707/20131030/1383143532

とりあえずgetDay()を実行してみる。

Notice: Undefined variable: lenght in C:\xampp\test.php on line 33
Notice: Undefined offset: 6 in C:\xampp\test.php on line 50
Notice: Undefined offset: 5 in C:\xampp\test.php on line 50
Notice: Undefined offset: 4 in C:\xampp\test.php on line 50
Notice: Undefined offset: 3 in C:\xampp\test.php on line 50
Notice: Undefined offset: 2 in C:\xampp\test.php on line 50
Notice: Undefined offset: 1 in C:\xampp\test.php on line 50

array(3) {
  ["year"]=>
  string(4) "2013"
  ["month"]=>
  array(2) {
    [0]=>
    int(10)
    [1]=>
    string(2) "11"
  }
  ["day"]=>
  array(7) {
    [0]=>
    int(6)
    [6]=>
    int(31)
    [5]=>
    int(31)
    [4]=>
    int(31)
    [3]=>
    int(31)
    [2]=>
    int(31)
    [1]=>
    int(31)
  }
}
line 50はforの行です。
これを実行したのは2013/11/06。
月またぎの処理に異常があるようです。
修正しましょう。
というか、こういう処理はビルトインに任せるべきで、わざわざ自力でやる意味は全くありません。
<?php
	function getDay($year = NULL, $month = NULL, $day = NULL, $length = NULL) {
		if(!$year)  {$year   = date('Y');}
		if(!$month) {$month  = date('m');}
		if(!$day)   {$day    = date('d');}
		if(!$length){$length = 6;}
		
		// 終了日
		$end = new DateTime($year.'-'.$month.'-'.$day, new DateTimeZone('Asia/Tokyo'));
		// 開始日
		$start = clone $end;
		$start->modify($length . ' days ago');
		// DatePeriodにして返す
		return new DatePeriod($start, new DateInterval('P1D'), $end);
	}
	
	// 使い方
	$datePeriod = getDay();
	foreach($datePeriod as $key=>$val){
		print(($key+1) . '日目:' . $val->format('Y年m月d日'));
	}
跡形が無くなってしまった。
閏年なんてこちら側では一切考慮する必要はなく、DateTimeがよしなに処理してくれます。
返り値はDatePeriodなので、返ってきた側での使い方も非常に簡単です。
PR


2013/11/06 23:08 | Comments(0) | PHP

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<PHP5.5.0 「第14回オフラインリアルタイムどう書くの問題」をPHPで解く | HOME | ショートカットメニュー:Dust: An Elysian Tail>>
忍者ブログ[PR]