Issues (326)

src/Lib/BaseFunctions.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
/**
14
 * returns date in SQL friendly format
15
 *
16
 * @param int $timestamp timestamp
17
 * @return string date string
18
 */
19
function bDate($timestamp = null): string
20
{
21
    if ($timestamp === null) {
22
        $timestamp = time();
23
    }
24
25
    return date('Y-m-d H:i:s', $timestamp);
26
}
27
28
/**
29
 * Converts a timestamp-entity into unix-timestamp
30
 *
31
 * @param int|string|\DateTimeInterface $timestamp to convert
32
 * @return int unix timestamp
33
 */
34
function dateToUnix($timestamp): int
35
{
36
    if (is_int($timestamp)) {
37
        return $timestamp;
38
    }
39
40
    if (is_object($timestamp)) {
41
        return $timestamp->getTimestamp();
42
    }
43
44
    if ($timestamp !== null) {
0 ignored issues
show
The condition $timestamp !== null is always true.
Loading history...
45
        $unix = strtotime($timestamp);
46
        if ($unix < 0 || $unix === false) {
47
            throw new \RuntimeException(
48
                "Can't convert timestamp $timestamp to unix-timestamp.",
49
                1524230476
50
            );
51
        }
52
53
        return $unix;
54
    }
55
56
    throw new \RuntimeException();
57
}
58
59
/**
60
 * timestamp to iso
61
 *
62
 * @param int|string|\DateTimeInterface $timestamp to convert
63
 * @return string
64
 */
65
function dateToIso($timestamp): string
66
{
67
    return date('c', dateToUnix($timestamp));
68
}
69