1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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()";
}
}
?>