blob: 495458049c2863ba3af4c585d5f291f028a6fb96 (
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
|
<?php
function generate_random_string(int $length): string
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$output = "";
for ($i = 0; $i < $length; $i++) {
$charindex = random_int(0, strlen($chars) - 1);
$output .= $chars[$charindex];
}
return $output;
}
function str_safe(string $s, int|null $max_length, bool $remove_new_lines = true): string
{
$output = $s;
if ($remove_new_lines) {
$output = str_replace(PHP_EOL, "", $output);
}
$output = htmlspecialchars($output);
$output = strip_tags($output);
if ($max_length) {
$output = substr($output, 0, $max_length);
}
$output = trim($output);
return $output;
}
function format_timestamp(int $timestamp_secs)
{
$days = floor($timestamp_secs / (60 * 60 * 24));
$hours = floor($timestamp_secs / (60 * 60) % 24);
$minutes = floor($timestamp_secs % (60 * 60) / 60);
$seconds = floor($timestamp_secs % 60);
if ($days == 0 && $hours == 0 && $minutes == 0) {
return "$seconds second" . ($seconds > 1 ? "s" : "");
} else if ($days == 0 && $hours == 0) {
return "$minutes minute" . ($minutes > 1 ? "s" : "");
} else if ($days == 0) {
return "$hours hour" . ($hours > 1 ? "s" : "");
} else {
return "$days day" . ($days > 1 ? "s" : "");
}
}
|