-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNiceDate.php
113 lines (101 loc) · 3.45 KB
/
NiceDate.php
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
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) <2014> <Divulgue Mania>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
include('libs/language.php');
/**
* Simple abstract class for make a nice date time
* @see https://github.com/offboard/Make-Nice-Time/blob/master/README.md#example-database
* @author Bruno Ribeiro <[email protected]>
* @version 0.1.7
*/
abstract class makeNiceTime extends src\language\Languages {
/**
* Create nice date time
*
* @param datetime $datetime Timestamp Format
* @param String $LimitDate Differences in days to show another format, use false to disable
* @param String $OutputFormat Output format if datetime interval exercise days - Standard timestamp format
*
* @access public
* @return string
*/
public static function MakeNew($datetime, $LimitDate = false, $OutputFormat = 'Y-m-d H:i:s') {
// Check version of PHP for initiate class
$msg = sprintf(self::_GetL('ERROPHP'), self::$PHPminVersion);
if (!self::checkMinVersionPHP()) {
error_log($msg);
die($msg);
}
// is necessary to transform the data into int
$etime = time() - self::_CheckTime($datetime);
// check limit
if ($LimitDate == true) {
return date($OutputFormat, self::_CheckTime($datetime));
}
// is just now ?
if ($etime < 1) {
return self::_GetL('CURRENT');
}
// generate nice date time
foreach (self::$values as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
$l = self::_GetL($str);
// check the amount is greater than or equal to 1
$result = ($r > 1) ? $r . ' ' . $l[1] : '1 ' . $l[0];
return sprintf(self::_GetL('MESSAGE'), $result);
}
}
}
/**
* Check the minimum required version of PHP
*
* @return boolean
*/
private static function checkMinVersionPHP() {
if (version_compare(PHP_VERSION, self::$PHPminVersion, '<')) {
return false;
}
return true;
}
/**
* If not numeric then convert data to unix timestamps
*
* @param mixed $time Date
*
* @access private
* @return integer
*/
private static function _CheckTime($time) {
$result = !is_int($time) ? strtotime($time) : $time;
return $result;
}
/**
* Time format is UNIX timestamp or
* PHP strtotime compatible strings
*
* @param Datetime $time1 Date used
* @param Datetime $time2 Limit time
*
* @access private
* @return boolean
*/
private static function dateDiff($time1, $time2) {
// If not numeric then convert texts to unix timestamps
return (self::_CheckTime($time1) > self::_CheckTime($time2)) ? true : false;
}
}