Time   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A database() 0 19 4
A HTTP() 0 18 3
1
<?php
2
3
namespace FFMVC\Helpers;
4
5
/**
6
 * Time and Date Helper Class.
7
 *
8
 * @author Vijay Mahrra <[email protected]>
9
 * @copyright (c) Copyright 2015 Vijay Mahrra
10
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
11
 */
12
class Time extends \Prefab
13
{
14
    /**
15
     * format a database-specific date/time string.
16
     *
17
     * @param optional int    $unixtime the unix time (null = now)
18
     * @param optional string $dbms     the database software the timestamp is for
19
     * @return string date in format of database driver
20
     * @todo add a switch for the f3 database driver and set the timestamp
21
     */
22
    public static function database(int $unixtime = null, string $dbms = null): string
23
    {
24
25
        // use current time if bad time value or unset
26
        $unixtime = (int) $unixtime;
27
28
        if (0 >= $unixtime) {
29
            $unixtime = time();
30
        }
31
32
        // format date/time according to database driver
33
        $dbms = (empty($dbms)) ? \F3::get('db.driver') : $dbms;
34
35
        switch ($dbms) {
36
            default:
37
            case 'mysql':
38
                return date('Y-m-d H:i:s', $unixtime);
39
        }
40
    }
41
42
    /**
43
     * Utility to convert timestamp into a http header date/time.
44
     *
45
     * @param int time php time value
46
     * @param string $zone timezone
47
     */
48
    public static function HTTP(int $unixtime = null, string $zone = ''): string
49
    {
50
        // use current time if bad time value or unset
51
        $unixtime = (int) $unixtime;
52
53
        if (0 >= $unixtime) {
54
            $unixtime = time();
55
        }
56
57
        // if its not a 3 letter timezone set it to GMT
58
        if (3 == \UTF::instance()->strlen($zone)) {
59
            $zone = strtoupper($zone);
60
        } else {
61
            $zone = '';
62
        }
63
64
        return \UTF::instance()->trim(gmdate('D, d M Y H:i:s', $unixtime).' '.$zone);
65
    }
66
}
67