-
Notifications
You must be signed in to change notification settings - Fork 0
/
_functions.php
126 lines (104 loc) · 3.31 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
<?php
function require_directory($path) {
$files = glob($path . "/*.php"); // return array files
$dirs = glob($path . "/*", GLOB_ONLYDIR); // return array files
foreach ($files as $phpFile) {
require_once("$phpFile");
}
foreach ($dirs as $dir) {
$dir_files = glob($dir . "/*.php"); // return array files
foreach ($dir_files as $phpFile) {
require_once("$phpFile");
}
}
}
function include_directory($path) {
$files = glob($path . "/*.php"); // return array files
$dirs = glob($path . "/*", GLOB_ONLYDIR); // return array files
foreach ($files as $phpFile) {
include("$phpFile");
}
foreach ($dirs as $dir) {
$dir_files = glob($dir . "/*.php"); // return array files
foreach ($dir_files as $phpFile) {
include("$phpFile");
}
}
}
function seconds_to_timestamp($s): string {
$timestamp = gmdate("H:i:s", $s);
if (substr($timestamp, 0, 2) == "00")
$timestamp = substr($timestamp, 4);
return $timestamp;
}
function string_to_timestamp($s): string {
return date("Y-m-d H:i:s", strtotime($s));
}
function strtodate($s) {
return date('d M Y', strtotime($s));
}
function string_to_year($s): string {
return date('Y', strtotime($s));
}
function time_ago($datetime, $max_units = 1) {
$i = 0;
$distant_timestamp = strtotime($datetime);
$time = time() - $distant_timestamp;
$tokens = [
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
];
$responses = [];
while ($i < $max_units && $time > 0) {
foreach ($tokens as $unit => $text) {
if ($time < $unit) {
continue;
}
$i++;
$numberOfUnits = floor($time / $unit);
$responses[] = $numberOfUnits . ' ' . $text . (($numberOfUnits > 1) ? 's' : '');
$time -= ($unit * $numberOfUnits);
break;
}
}
if (!empty($responses)) {
return implode(', ', $responses) . ' ago';
}
return 'Just now';
}
function redirect($url = null): void {
if (!$url)
$url = $_SERVER["HTTP_REFERER"];
header("Location: " . $url);
}
function absURI_to_relURI(string $URI) {
$root = realpath("");
$rootCharLen = strlen($root);
$rootPos = strpos($URI, $root);
$path = $URI;
if($rootPos !== false)
$path = substr($URI, strpos($URI,$root) + $rootCharLen + 1);
return $path;
}
function shortenString(string $string, int $maxLength) : string {
$string = substr($string,0,$maxLength - 3);
if(strlen($string) >= $maxLength - 3)
$string .= '...';
return $string;
}
function consoleLog($var, $key=null) {
$html = "";
$html .= "<script> /* consoleLog script */ ";
$html .= "console.log(\"Type: ". gettype($var) .", Key: ". $key ."\"); ";
if(gettype($var) === "boolean" || gettype($var) === "integer" || gettype($var) === "double" || gettype($var) === "string")
$html .= "console.log('" . $var . "'); ";
if(gettype($var) === "array" || gettype($var) === "object" || gettype($var) === "resource")
$html .= "console.log('" . json_encode($var, JSON_HEX_TAG) . "'); ";
$html .= "</script>";
echo $html;
}