summaryrefslogtreecommitdiff
path: root/lib/utils.php
blob: 31d8778ff565512690f1fd06998f249b6c4abb80 (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
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';

function json_response(mixed $data, string|null $message, int $code = 200)
{
    http_response_code($code);
    header('Content-Type: application/json');
    echo json_encode([
        'status_code' => $code,
        'message' => $message,
        'data' => $data
    ], JSON_UNESCAPED_SLASHES);
}

function generate_random_char_sequence(array $chars, int $length): string
{
    $o = "";

    for ($i = 0; $i < $length; $i++) {
        $o .= $chars[random_int(0, count($chars) - 1)];
    }

    return $o;
}

function format_timestamp(int $timestamp_secs)
{
    $days = (int) floor($timestamp_secs / (60.0 * 60.0 * 24.0));
    $hours = (int) floor(round($timestamp_secs / (60 * 60)) % 24);
    $minutes = (int) floor(round($timestamp_secs % (60 * 60)) / 60);
    $seconds = (int) 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" : "");
    }
}