PHPのDateTimeクラス
PHPのDateTimeクラスは標準装備なのでお手軽で便利なのだけど、若干使い勝手が悪い。
なのでDateTimeを継承してちょっとした拡張をしとくと便利かと。
<?php class MyDateTime extends DateTime { function year () { return $this->format('Y'); } function month () { return $this->format('n'); } function day () { return $this->format('j'); } function hour () { return $this->format('H'); } function minute () { return $this->format('i'); } function second () { return $this->format('s'); } function week () { return $this->format('w'); } function week_str () { static $hash = array('日','月','火','水','木','金','土'); return $hash[$this->format('w')]; } function ymd ($sep='-') { return $this->format('Y'.$sep.'m'.$sep.'d'); } function ym ($sep='-') { return $this->format('Y'.$sep.'m'); } function next_days ($num=1) { $this->modify("+$num day"); return $this; } function prev_days ($num=1) { $this->modify("-$num day"); return $this; } function next_months ($num=1) { $this->modify("+$num month"); return $this; } function prev_months ($num=1) { $this->modify("-$num month"); return $this; } function next_years ($num=1) { $this->modify("+$num year"); return $this; } function prev_years ($num=1) { $this->modify("-$num year"); return $this; } function set_year($year) { $this->setDate($year,$this->month(),$this->day()); return $this; } function set_month($month) { $this->setDate($this->year(),$month,$this->day()); return $this; } function set_day($day) { $this->setDate($this->year(),$this->month(),$day); return $this; } // 月末に設定する function set_lday_of_month() { $this->setDate($this->year(),$this->month(),'01'); $this->modify('+1 month -1 day'); return $this; } // 引数で指定した日付までの年単位のリストを返す function year_range ($end_dt) { $start_dt = clone $this; $end_str = $end_dt->format('Y'); $dt = array(); while( $start_dt->format('Y') <= $end_str ) { $dt []= clone $start_dt; $start_dt->modify('+1 year'); } return $dt; } // 引数で指定した日付までの月単位のリストを返す function month_range ($end_dt) { $start_dt = clone $this; $end_str = $end_dt->format('Ym'); $dt = array(); while( $start_dt->format('Ym') <= $end_str ) { $dt []= clone $start_dt; $start_dt->modify('+1 month'); } return $dt; } // 引数で指定した日付までの日単位のリストを返す function day_range ($end_dt) { $start_dt = clone $this; $end_str = $end_dt->format('Ymd'); $dt = array(); while( $start_dt->format('Ymd') <= $end_str ) { $dt []= clone $start_dt; $start_dt->modify('+1 day'); } return $dt; } // YYYY01〜YYYY12までのリストを返す function month_list () { $start_dt = clone $this; $start_dt->setDate($this->year(),'01','01'); $end_dt = clone $start_dt; $end_dt->setDate($this->year(),'12','01'); return $start_dt->month_range($end_dt); } // YYYYMM01〜YYYYMM31(月によって変わる)までのリストを返す function day_list () { $start_dt = clone $this; $start_dt->setDate($this->year(),$this->month(),'01'); $end_dt = clone $start_dt; $end_dt->modify('+1 month -1 day'); return $start_dt->day_range($end_dt); } function hour_list () { $start_dt = clone $this; $start_dt->setTime(0,$this->minute(),$this->second()); $end_dt = clone $start_dt; $end_dt->setTime(23,$this->minute(),$this->second()); $dt = array(); while( $start_dt->format('YmdHis') <= $end_dt->format('YmdHis') ) { $dt []= clone $start_dt; $start_dt->modify('+1 hour'); } return $dt; } function copy() { return clone $this; } function __clone() {} function __toString(){ return $this->format('Y-m-d H:i:s'); } }
使用例
<?php require_once 'MyDateTime.class.php'; $now = new MyDateTime; // 現在の時間 $now->year(); // 年 $now->month(); // 月 $now->day(); // 日 $now->ymd(); // 年-月-日 $date = $now->copy(); // 複製作成(clone $nowと同じ意味) $date->next_months(3)->set_day(1); // 3ヶ月進めて、1日にセット
メソッドチェインができるので割と重宝してます。
ちなみにPHP5.3だとDateTime周りも色々と拡張されてるみたいですが、まだまだPHP5.2の環境も多いと思うのでこういうクラスを拵えておくと良いんじゃないでしょうか