aboutsummaryrefslogtreecommitdiff
path: root/classes/BusinessAdmin.php
blob: 7acb4caf3b0a2cdfdf88f2782276a4dc0f0cdd68 (plain) (blame)
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
<?php
/**
 * This file provides the BusinessAdmin class
 *
 * @author Camil Staps
 *
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * 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/>.
 */

/**
 * Provides basic functions like adding elements to the database
 */
class BusinessAdmin {
	/**
	 * Format a date nicely
	 *
	 * @param int $timestamp                  The UNIX timestamp to format
	 * @param bool $with_time                 If false, only the date is returned; if false, also the time
	 * @param bool $full_date                 If true, a year will be outputted even if the date is in the current year
	 * @param bool $relatively                Whether or not to show the date relatively (e.g. '1 day ago')
	 *
	 * @return string The formatted date
	 */
	public static function formatDate($timestamp, $with_time = true, $full_date = false, $relatively = false) {
		if ($relatively) {
			$delta  = time() - $timestamp;
			$second = 1;
			$minute = 60 * $second;
			$hour   = 60 * $minute;
			$day    = 24 * $hour;
			$month  = 30 * $day;
			$year   = 365 * $day;
			if ($delta < 2 * $minute)
				return 'just now';
			if ($delta < 45 * $minute)
				return floor($delta / $minute) . ' minutes ago';
			if ($delta < 90 * $minute)
				return 'an hour ago';
			if ($delta < 24 * $hour)
				return floor($delta / $hour) . ' hours ago';
			if ($delta < 48 * $hour)
				return 'yesterday';
			if ($delta < 30 * $day)
				return floor($delta / $day) . ' days ago';

			$output = [];
			$years = floor($delta / $year);
			if ($years != 0)
				$output[] = $years == 1 ? 'one year' : $years . ' years';
			$delta -= $years * $year;
			$months = floor($delta / $month);
			if ($months != 0)
				$output[] = $months == 1 ? 'one month' : $months . ' months';
			return implode(', ', $output);
		}

		if (date('Y', $timestamp) == 1970)
			return 'never';
		if (date('d/m/Y') == date('d/m/Y', $timestamp))
			return 'today';

		$output = '';
		if (date('Y') != date('Y', $timestamp) || $full_date)
			$output .= date('Y-', $timestamp);
		if (date('d/m/Y') != date('d/m/Y', $timestamp) || $full_date)
			$output .= date('m-d', $timestamp);
		if ($with_time)
			$output .= date(' H:i', $timestamp);

		return $output;
	}
}