facebook twitter hatena line email

Php/zend framework/zend date

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

日付を配列に

require_once 'Zend/Date.php';
$datetime = '2010-11-10 12:34:56';
$date = new Zend_Date($datetime);
dump($date->toArray());
// Array(
// [day] => 11,
// [month] => 10,
// [year] => 2010,
// [hour] => 12,
// [minute] => 34,
// [second] => 56,
// [timezone] => 'JST',
// [timestamp] => 1286722800,
// [weekday] => 1,
// [dayofyear] => 283,
// [week] => 41,
// [gmtsecs] => 32400,
// )

配列日付を文字列に

$dates = array("year" => 2011, "month" => 10, "day" => 12);
$date = new Zend_Date();
$date->setOptions(array('format_type' => 'php'));
$date->set($dates);
print $date->toString('Y-m-d');
// 2010-10-12
// dump($date->toArray());
// 同上

正しい日付かチェック(限界値を超えた場合は桁が繰り上がる)

$dates = array("year" => 2011, "month" => 10, "day" => 12);
$date = new Zend_Date($dates);
$isValid = Zend_Date::isDate($date);

厳密な日付チェック

require_once 'Zend/Validate/Date.php';
$validate = new Zend_Validate_Date();
$isValid = $validate->isValid(sprintf("%04d-%02d-%02d", $dates['year'], $dates['month'], $dates['day'])));