forked from brusselopole/Worldopole
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
134 lines (113 loc) · 3.97 KB
/
functions.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
########################################################################
// Human Time Ago
// @param $timestamp => timestamp (mandatory)
// @param $locales => locales (mandatory)
//
// Return time ago at human format (eg: 2 hours ago)
########################################################################
function time_ago($timestamp, $locales)
{
// Set up our variables.
$minute_in_seconds = 60;
$hour_in_seconds = $minute_in_seconds * 60;
$day_in_seconds = $hour_in_seconds * 24;
$week_in_seconds = $day_in_seconds * 7;
$month_in_seconds = $day_in_seconds * 30;
$year_in_seconds = $day_in_seconds * 365;
// current time
$now = time();
// Calculate the time difference between the current time reference point and the timestamp we're comparing.
// The difference is defined negative, when in the future.
$time_difference = $now - $timestamp;
// Calculate the time ago using the smallest applicable unit.
if ($time_difference < $hour_in_seconds) {
$difference_value = abs(round($time_difference / $minute_in_seconds));
$difference_label = 'MINUTE';
} elseif ($time_difference < $day_in_seconds) {
$difference_value = abs(round($time_difference / $hour_in_seconds));
$difference_label = 'HOUR';
} elseif ($time_difference < $week_in_seconds) {
$difference_value = abs(round($time_difference / $day_in_seconds));
$difference_label = 'DAY';
} elseif ($time_difference < $month_in_seconds) {
$difference_value = abs(round($time_difference / $week_in_seconds));
$difference_label = 'WEEK';
} elseif ($time_difference < $year_in_seconds) {
$difference_value = abs(round($time_difference / $month_in_seconds));
$difference_label = 'MONTH';
} else {
$difference_value = abs(round($time_difference / $year_in_seconds));
$difference_label = 'YEAR';
}
// plural
if ($difference_value != 1) {
$difference_label = $difference_label.'S';
}
if ($time_difference <= 0) {
// Present
return sprintf($locales->TIME_LEFT, $difference_value.' '.$locales->$difference_label);
} else {
// Past
return sprintf($locales->TIME_AGO, $difference_value.' '.$locales->$difference_label);
}
}
########################################################################
// Percent calculator
// @param $val => int (mandatory)
// @param $val_total => int (mandatory)
//
// Return pourcent from total
########################################################################
function percent($val, $val_total)
{
$count1 = $val_total / $val;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count;
}
########################################################################
// File version (unix timestamp)
// @param $url => string (mandatory)
//
// Return $url with last_modified unix timestamp before suffix
########################################################################
function auto_ver($url)
{
$path = pathinfo($url);
$ver = '.'.filemtime(SYS_PATH.'/'.$url).'.';
echo $path['dirname'].'/'.preg_replace('/\.(css|js)$/', $ver."$1", $path['basename']);
}
########################################################################
// File age in secs
// @param $filepath => string (mandatory)
//
// Return file age of file in secs, PHP_INT_MAX if file doesn't exist
########################################################################
function file_update_ago($filepath)
{
if (is_file($filepath)) {
$filemtime = filemtime($filepath);
$now = time();
$diff = $now - $filemtime;
return $diff;
}
// file doesn't exist yet!
return PHP_INT_MAX;
}
########################################################################
// Only keep data after $timestamp in $array (compared to 'timestamp' key)
// @param $array => array (mandatory)
// @param $timestamp => int (mandatory)
//
// Return trimmed array
########################################################################
function trim_stats_json($array, $timestamp)
{
foreach($array as $key => $value) {
if ($value['timestamp'] < $timestamp) {
unset($array[$key]);
}
}
return $array;
}