summaryrefslogtreecommitdiff
path: root/time.pinc
blob: aa5421b782cb005707bd0c5a4c842f6916a18e3b (plain)
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
<?php
/*
**	$Id$
**
**	time.pinc - Time routines for ISO/Unix/Human dates
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
*/

/**
 * Get the current date
 * @see current_time, now
 * @return Current date in ISO (aka mysql) format: YYYY-MM-DD
 */
function current_date()
{
	return date('Y-m-d');
}


/**
 * Get the current time
 * @see current_date, now
 * @return Current time in ISO (aka mysql) format: HH:MM:SS
 */
function current_time()
{
	return date('H:i:s');
}


/**
 * Get the current date and time
 * @param $offset Optional offset in seconds (defaults to 0)
 * @return Current date and time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS
 * @see current_date, current_time
 */
function now($offset=0)
{
	return date('Y-m-d H:i:s', time()+$offset);
}


/**
 * Calculate the number of days since January 1, 1970 for a given ISO date
 * @see mysqldatetoseconds
 * @param $date Date in ISO format: YYYY-MM-DD
 * @param $date Time in ISO format: HH:MM:SS
 * @return The supplied date as number of day since the start of the epoch
 */
function mysqldatetounix($date, $time)
{
	$dfields = split('-', $date);
	$tfields = split(':', $time);
	$unixtime = intval(mktime($tfields[0], $tfields[1], $tfields[2], $dfields[1], $dfields[2], $dfields[0]));
	/* echo "$date $time --> " . $dfields[0] . "-" . $dfields[1] . "-" . $dfields[2] . " " . $tfields[0] . ":" . $tfields[1] . ":" . $tfields[2] . " ($unixtime sec)<br>\n"; */
	return $unixtime / 86400;
}


/**
 * Calculate the number of seconds since January 1, 1970 for a given ISO date
 * WARNING: Works only for years less than about 2038
 * @see mysqldatetoseconds, it_date_mysqltimestamptounix
 * @param $datetime Date/time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS
 * @return The supplied date as number of seconds since the start of the epoch
 */
function mysqldatetoseconds($datetime)
{
	$dt = split(' ', $datetime);
	$dfields = split("-", $dt[0]);
	$tfields = split(":", $dt[1]);
	$unixtime = intval(mktime($tfields[0], $tfields[1], $tfields[2], $dfields[1], $dfields[2], $dfields[0]));
	/* echo "$datetime --> " . $dfields[0] . "-" . $dfields[1] . "-" . $dfields[2] . " " . $tfields[0] . ":" . $tfields[1] . ":" . $tfields[2] . " ($unixtime)<br>\n"; */
	return $unixtime;
}


/**
 * Calculate the number of seconds since January 1, 1970 for a given mysql timestamp
 * (which is in format YYYYMMDDhhmmss). Ignores all non-numerical characters, so this
 * function can be used as a substitute for mysqldatetoseconds().
 * WARNING: Works only for years less than about 2038
 * @see mysqldatetoseconds, it_date_unixtomysqltimestamp
 * @param $ts Date/time in ISO (aka mysql) format: YYYYMMDDhhmmss
 * @return The supplied date as number of seconds since the start of the epoch
 */
function it_date_mysqltimestamptounix($ts)
{
	$ts = ereg_replace('[^0-9]+', '', $ts);
	return intval(mktime(substr($ts, 8, 2), substr($ts, 10, 2), substr($ts, 12, 2), substr($ts, 4, 2), substr($ts, 6, 2), substr($ts, 0, 4)));
}


/**
 * Convert the given UNIX timestamp to a mysql timestamp in format YYYYMMDDhhmmss.
 * WARNING: Works only for years less than about 2038
 * @see mysqldatetoseconds, it_date_mysqltimestamptounix
 * @param $ts Date/time in UNIX format (number of seconds since January 1, 1970)
 * @return The supplied date as YYYYMMDDhhmmss
 */
function it_date_unixtomysqltimestamp($ts)
{
	return strftime('%Y%m%d%H%M%S', $ts);
}


/**
 * Return the supplied date/time in human-readable Swiss format
 * WARNING: Works only for years less than about 2038
 * @see swisstimestamp, mysqldatetoseconds
 * @param $datetime Date/time in ISO (aka mysql) format: YYYY-MM-DD HH:MM:SS
 * @return The supplied date/time formatted as "14.08.1966 06:15"
 */
function swissdate($datetime)
{
	return strftime("%d.%m.%Y %H:%M", mysqldatetoseconds($datetime));
}


/**
 * Return the supplied UNIX timestamp in human-readable Swiss format
 * WARNING: Works only for years less than about 2038
 * @see swissdate, mysqldatetoseconds
 * @param $datetime Optional date/time in seconds from the beginning of the epoch (default is now)
 * @return The supplied date/time formatted as "14.08.1966 06:15"
 */
function swisstimestamp($datetime = null)
{
	return strftime('%d.%m.%Y %H:%M', isset($datetime) ? $datetime : time());
}

?>