<?php /** * PostgresUtil * 日付の書式を変更するのに利用するクラス * 全部staticで利用する。 * $Id: PostgresUtil.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $ * @author iimuro * @access public * @package jp.aimslib2.database.postgres */ class PostgresUtil extends AbstractDatabaseUtil{ /** * PostgreSQLのTimestamp形式で渡された時刻をUNIXタイムスタンプにして返します。 * Use convertDb2UnixTimestamp * @depricated * @param string $dbtime PostgreのTimestamp形式で表された時刻 * @return int UNIXタイムスタンプ */ public static function toTimestamp($dbtime) { return PostgresUtil::convertDb2UnixTimestamp($dbtime); } /** * PostgreSQLのTimestamp形式で渡された時刻をUNIXタイムスタンプにして返します。 * * @access public * @param string $dbtime PostgreのTimestamp形式で表された時刻 * @return int UNIXタイムスタンプ */ public static function convertDb2UnixTimestamp($dbtime) { $timestamp = 0; if (preg_match("/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/", $dbtime, $match)) { $year = $match[1]; $month = $match[2]; $day = $match[3]; $hour = $match[4]; $minute = $match[5]; $second = $match[6]; //$timestamp = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); } else if (preg_match("/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+)/", $dbtime, $match)) { $year = $match[1]; $month = $match[2]; $day = $match[3]; $hour = $match[4]; $minute = $match[5]; $second = 0; //$timestamp = mktime($match[4], $match[5], 0, $match[2], $match[3], $match[1]); } else if (preg_match("/^(\d+)-(\d+)-(\d+)/", $dbtime, $match)) { $year = $match[1]; $month = $match[2]; $day = $match[3]; $hour = 0; $minute = 0; $second = 0; $timestamp = mktime(0, 0, 0, $match[2], $match[3], $match[1]); } else { $year = 1970; $month = 1; $day = 1; $hour = 0; $minute = 0; $second = 0; } // if (($year < 1970) && ($year > 1910)) { // // // 両方に60たして差分を取得。2000年があるので、日にち-1 // $tmp1 = mktime(0, 0, 0, $month, $day, $year + 60); // $tmp2 = mktime(0, 0, 0, 1, 1, 1970 + 60); // $diff_sec = $tmp2 - $tmp1; // // // ErrorLogger::doOutput("RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR" . $dbtime, 0); // ErrorLogger::doOutput("CONVERTINGG........." . $diff_sec, 0); // // // 1日の秒数 // //$day_sec = 60 * 60 * 24; // //$diff_sec -= $day_sec; // $timestamp = mktime($hour, $minute, $second, 1, 1, 1970) - $diff_sec; // } else { // $timestamp = mktime($hour, $minute, $second, $month, $day, $year); // } $timestamp = PostgresUtil::modMkTime($hour, $minute, $second, $month, $day, $year); return $timestamp; } public static function modMkTime($hour, $minute, $second, $month, $day, $year) { if (($year < 1970) && ($year > 1910)) { // 両方に60たして差分を取得。2000年があるので、日にち-1 $tmp1 = mktime(0, 0, 0, $month, $day, $year + 60); $tmp2 = mktime(0, 0, 0, 1, 1, 1970 + 60); $diff_sec = $tmp2 - $tmp1; // 1日の秒数 //$day_sec = 60 * 60 * 24; //$diff_sec -= $day_sec; $timestamp = mktime($hour, $minute, $second, 1, 1, 1970) - $diff_sec; } else { $timestamp = mktime($hour, $minute, $second, $month, $day, $year); } return $timestamp; } /** * PostgreSQLのTimestamp形式で渡された時刻を * PHPのgetdateで返される配列の形にして返します。 * * @access public * @param string $dbtime PostgreのTimestamp形式で表された時刻 * @return array PHPのgetdateで返される配列の形で表された時刻 */ public static function toDate($dbtime) { $date = array(); $date = getdate(PostgresUtil::convertDb2UnixTimestamp($dbtime)); return $date; } /** * PHPのgetdateで返される配列の形で表された時刻、または * UNIXタイムスタンプで表された時刻を * PostgreのTimestamp形式にして返します。 * use convertUnix2DbTimestamp * @depricated * @param mixed $time PHPのgetdateで返される形の時刻またはUNIXタイムスタンプ * @return string PostgreのTimestamp形式での時刻 */ public static function toDBTimestamp($time) { return PostgresUtil::convertUnix2DbTimestamp($time); } /** * PHPのgetdateで返される配列の形で表された時刻、または * UNIXタイムスタンプで表された時刻を * PostgreのTimestamp形式にして返します。 * * @access public * @param mixed $time PHPのgetdateで返される形の時刻またはUNIXタイムスタンプ * @return string PostgreのTimestamp形式での時刻 */ public static function convertUnix2DbTimestamp($time) { if (is_array($time)) { $datetime = date("Y-m-d H:i:s", mktime($time["hours"],$time["minutes"],$time["seconds"],$time["mon"],$time["mday"],$time["year"])); } else { $datetime = date("Y-m-d H:i:s", $time); } return $datetime; } /** * PostgreSQLにおける現時間の表現を返します。 */ public static function getCurrent() { return "now()"; } } ?>