summaryrefslogtreecommitdiff
path: root/app/Functions/FunctionsDate.php
blob: 1a6cf66ded9ac6aca8b72caeb51f67fa515d11cd (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
<?php
namespace Fisharebest\Webtrees\Functions;

/**
 * webtrees: online genealogy
 * Copyright (C) 2015 webtrees development team
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\I18N;

/**
 * Class FunctionsDate - common functions
 */
class FunctionsDate {
	/**
	 * @param string $age_string
	 * @param bool $show_years
	 *
	 * @return string
	 */
	public static function getAgeAtEvent($age_string, $show_years) {
		switch (strtoupper($age_string)) {
			case 'CHILD':
				return I18N::translate('Child');
			case 'INFANT':
				return I18N::translate('Infant');
			case 'STILLBORN':
				return I18N::translate('Stillborn');
			default:
				return preg_replace_callback(
					array(
						'/(\d+)([ymwd])/',
					),
					function ($match) use ($age_string, $show_years) {
						switch ($match[2]) {
							case 'y':
								if ($show_years || preg_match('/[dm]/', $age_string)) {
									return I18N::plural('%s year', '%s years', $match[1], I18N::digits($match[1]));
								} else {
									return I18N::digits($match[1]);
								}
							case 'm':
								return I18N::plural('%s month', '%s months', $match[1], I18N::digits($match[1]));
							case 'w':
								return I18N::plural('%s week', '%s weeks', $match[1], I18N::digits($match[1]));
							case 'd':
								return I18N::plural('%s day', '%s days', $match[1], I18N::digits($match[1]));
						}
					},
					$age_string
				);
		}
	}

	/**
	 * Convert a unix timestamp into a formatted date-time value, for logs, etc.
	 * Don't attempt to convert into other calendars, as not all days start at
	 * midnight, and we can only get it wrong.
	 *
	 * @param int $time
	 *
	 * @return string
	 */
	public static function formatTimestamp($time) {
		$time_fmt = I18N::timeFormat();
		// PHP::date() doesn't do I18N.  Do it ourselves....
		preg_match_all('/%[^%]/', $time_fmt, $matches);
		foreach ($matches[0] as $match) {
			switch ($match) {
				case '%a':
					$t = gmdate('His', $time);
					if ($t == '000000') {
						$time_fmt = str_replace($match, /* I18N: time format “%a” - exactly 00:00:00 */
							I18N::translate('midnight'), $time_fmt);
					} elseif ($t < '120000') {
						$time_fmt = str_replace($match, /* I18N: time format “%a” - between 00:00:01 and 11:59:59 */
							I18N::translate('a.m.'), $time_fmt);
					} elseif ($t == '120000') {
						$time_fmt = str_replace($match, /* I18N: time format “%a” - exactly 12:00:00 */
							I18N::translate('noon'), $time_fmt);
					} else {
						$time_fmt = str_replace($match, /* I18N: time format “%a” - between 12:00:01 and 23:59:59 */
							I18N::translate('p.m.'), $time_fmt);
					}
					break;
				case '%A':
					$t = gmdate('His', $time);
					if ($t == '000000') {
						$time_fmt = str_replace($match, /* I18N: time format “%A” - exactly 00:00:00 */
							I18N::translate('Midnight'), $time_fmt);
					} elseif ($t < '120000') {
						$time_fmt = str_replace($match, /* I18N: time format “%A” - between 00:00:01 and 11:59:59 */
							I18N::translate('A.M.'), $time_fmt);
					} elseif ($t == '120000') {
						$time_fmt = str_replace($match, /* I18N: time format “%A” - exactly 12:00:00 */
							I18N::translate('Noon'), $time_fmt);
					} else {
						$time_fmt = str_replace($match, /* I18N: time format “%A” - between 12:00:01 and 23:59:59 */
							I18N::translate('P.M.'), $time_fmt);
					}
					break;
				default:
					$time_fmt = str_replace($match, I18N::digits(gmdate(substr($match, -1), $time)), $time_fmt);
			}
		}

		return self::timestampToGedcomDate($time)->display() . '<span class="date"> - ' . $time_fmt . '</span>';
	}

	/**
	 * Convert a unix-style timestamp into a Date object
	 *
	 * @param int $time
	 *
	 * @return Date
	 */
	public static function timestampToGedcomDate($time) {
		return new Date(strtoupper(gmdate('j M Y', $time)));
	}
}