summaryrefslogtreecommitdiff
path: root/includes/functions/functions_date.php
blob: 6073410322e7297e070f75a5b3fc54ae3953a096 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Date Functions that can be used by any page in webtrees
*
* webtrees: Web based Family History software
 * Copyright (C) 2010 webtrees development team.
 *
 * Derived from PhpGedView
* Copyright (C) 2002 to 2009 PGV Development Team.  All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @package webtrees
* @version $Id$
*/

if (!defined('WT_WEBTREES')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

define('WT_FUNCTIONS_DATE_PHP', '');

/**
* translate gedcom age string
*
* Examples:
* 4y 8m 10d.
* Chi
* INFANT
*
* @param string $agestring gedcom AGE field value
* @param bool $show_years;
* @return string age in user language
* @see http://homepages.rootsweb.com/~pmcbride/gedcom/55gcch2.htm#AGE_AT_EVENT
*/
function get_age_at_event($agestring, $show_years) {
	return preg_replace(
		array(
			'/\bchi(ld)?\b/i',
			'/\binf(ant)?\b/i',
			'/\bsti(llborn)?\b/i',
			'/(\d+)y/ie',
			'/(\d+)m/ie',
			'/(\d+)d/ie',
			'/(\d+)w/ie'
		),
		array(
			WT_I18N::translate('Child'),
			WT_I18N::translate('Infant'),
			WT_I18N::translate('Stillborn'),
			($show_years || preg_match('/[dm]/', $agestring)) ? "WT_I18N::plural('%d year', '%d years', '$1' , '$1')" : '$1',
			"WT_I18N::plural('%d month', '%d months', '$1' , '$1')",
			"WT_I18N::plural('%d day', '%d days', '$1' , '$1')",
			"WT_I18N::plural('%d week', '%d weeks', '$1' , '$1')"
		),
		$agestring
	);
}

/**
* Parse a time string into its different parts
* @param string $timestr the time as it was taken from the TIME tag
* @return array returns an array with the hour, minutes, and seconds
*/
function parse_time($timestr)
{
	$time = explode(':', $timestr.':0:0');
	$time[0] = min(((int) $time[0]), 23); // Hours: integer, 0 to 23
	$time[1] = min(((int) $time[1]), 59); // Minutes: integer, 0 to 59
	$time[2] = min(((int) $time[2]), 59); // Seconds: integer, 0 to 59
	$time["hour"] = $time[0];
	$time["minutes"] = $time[1];
	$time["seconds"] = $time[2];

	return $time;
}

////////////////////////////////////////////////////////////////////////////////
// Convert a unix timestamp into a formated date-time value, for logs, etc.
// We can't just use date("$DATE_FORMAT- $TIME_FORMAT") as this doesn't
// support internationalisation.
// Don't attempt to convert into other calendars, as not all days start at
// midnight, and we can only get it wrong.
////////////////////////////////////////////////////////////////////////////////
function format_timestamp($time) {
	global $DATE_FORMAT, $TIME_FORMAT;

	// PHP time formatting is english only.
/* TODO: cannot use this yet - the "a" in "a.m." gets refomatted by date()
 * Need to handle all of the time codes ourselves.
	if (strpos('%a', $TIME_FORMAT)!==false || strpos('%A', $TIME_FORMAT)!==false) {
		$fmt=date('His', $time);
		if ($fmt=='000000') {
			$TIME_FORMAT=str_replace('%A', WT_I18N::translate('MIDNIGHT'), $TIME_FORMAT);
			$TIME_FORMAT=str_replace('%a', WT_I18N::translate('midnight'), $TIME_FORMAT);
		} elseif ($fmt=='120000') {
			$TIME_FORMAT=str_replace('%A', WT_I18N::translate('NOON'), $TIME_FORMAT);
			$TIME_FORMAT=str_replace('%a', WT_I18N::translate('noon'), $TIME_FORMAT);
		} elseif ($fmt>'120000') {
			$TIME_FORMAT=str_replace('%A', WT_I18N::translate('P.M.'), $TIME_FORMAT);
			$TIME_FORMAT=str_replace('%a', WT_I18N::translate('p.m.'), $TIME_FORMAT);
		} else {
			$TIME_FORMAT=str_replace('%A', WT_I18N::translate('A.M.'), $TIME_FORMAT);
			$TIME_FORMAT=str_replace('%a', WT_I18N::translate('a.m.'), $TIME_FORMAT);
		}
	}
*/

	return
		PrintReady(timestamp_to_gedcom_date($time)->Display(false, $DATE_FORMAT).
		'<span class="date"> - '.date(str_replace('%', '', $TIME_FORMAT), $time).'</span>');
}

////////////////////////////////////////////////////////////////////////////////
// Convert a unix-style timestamp into a julian-day
////////////////////////////////////////////////////////////////////////////////
function timestamp_to_jd($time) {
	return timestamp_to_gedcom_date($time)->JD();
}

////////////////////////////////////////////////////////////////////////////////
// Convert a unix-style timestamp into a WT_Date object
////////////////////////////////////////////////////////////////////////////////
function timestamp_to_gedcom_date($time) {
	return new WT_Date(strtoupper(date('j M Y', $time)));
}

////////////////////////////////////////////////////////////////////////////////
// Get the current timestamp of the client, not the server
////////////////////////////////////////////////////////////////////////////////
function client_time() {
	if (isset($_SESSION["timediff"])) {
		return time()-$_SESSION["timediff"];
	} else {
		return time();
	}
}

?>