Issues (536)

src/Helpers/date.php (5 issues)

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
use BlitzPHP\Utilities\Date;
13
14
/**
15
 * FONCTIONS DE MANIPULATION DES DATES
16
 *
17
 * @credit	<a href="https://codeigniter.com">CodeIgniter 4.2 - date_helper</a>
18
 */
19
if (! function_exists('now')) {
20
    /**
21
     * Get "now" time
22
     *
23
     * Returns Date::now()->getTimestamp() based on the timezone parameter or on the
24
     * app.timezone setting
25
     *
26
     * @return Date|int
27
     */
28
    function now(?string $timezone = null, bool $returnObject = true)
29
    {
30
        $timezone = $timezone === null || $timezone === '' ? config('app.timezone') : $timezone;
31
32
        if ($returnObject) {
33
            return Date::now($timezone);
34
        }
35
36
        if ($timezone === 'local' || $timezone === date_default_timezone_get()) {
37
            return Date::now()->getTimestamp();
38
        }
39
40
        $time = Date::now($timezone);
41
        sscanf(
42
            $time->format('j-n-Y G:i:s'),
43
            '%d-%d-%d %d:%d:%d',
44
            $day,
45
            $month,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $month seems to be never defined.
Loading history...
46
            $year,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $year seems to be never defined.
Loading history...
47
            $hour,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hour seems to be never defined.
Loading history...
48
            $minute,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $minute seems to be never defined.
Loading history...
49
            $second
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $second seems to be never defined.
Loading history...
50
        );
51
52
        return mktime($hour, $minute, $second, $month, $day, $year);
53
    }
54
}
55
56
if (! function_exists('timezone_select')) {
57
    /**
58
     * Generates a select field of all available timezones
59
     *
60
     * Returns a string with the formatted HTML
61
     *
62
     * @param string $class   Optional class to apply to the select field
63
     * @param string $default Default value for initial selection
64
     * @param int    $what    One of the DateTimeZone class constants (for listIdentifiers)
65
     * @param string $country A two-letter ISO 3166-1 compatible country code (for listIdentifiers)
66
     *
67
     * @throws Exception
68
     */
69
    function timezone_select(string $class = '', string $default = '', int $what = DateTimeZone::ALL, ?string $country = null): string
70
    {
71
        $timezones = DateTimeZone::listIdentifiers($what, $country);
72
73
        $buffer = "<select name='timezone' class='{$class}'>\n";
74
75
        foreach ($timezones as $timezone) {
76
            $selected = ($timezone === $default) ? 'selected' : '';
77
            $buffer .= "<option value='{$timezone}' {$selected}>{$timezone}</option>\n";
78
        }
79
80
        return $buffer . ("</select>\n");
81
    }
82
}
83