|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* @title CDateTime Class |
|
4
|
|
|
* @desc Some useful methods of dates, formatting managements of time relative to the language. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
|
7
|
|
|
* @copyright (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved. |
|
8
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
|
9
|
|
|
* @package PH7 / Framework / Date |
|
10
|
|
|
* @version 1.2 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PH7\Framework\Date; |
|
14
|
|
|
|
|
15
|
|
|
defined('PH7') or exit('Restricted access'); |
|
16
|
|
|
|
|
17
|
|
|
use PH7\Framework\Config\Config; |
|
18
|
|
|
use DateTime; |
|
19
|
|
|
use DateTimeZone; |
|
20
|
|
|
|
|
21
|
|
|
class CDateTime |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Config */ |
|
24
|
|
|
private $_oConfig; |
|
25
|
|
|
|
|
26
|
|
|
/** @var DateTime */ |
|
27
|
|
|
private $_oDateTime; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->_oConfig = Config::getInstance(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get, initialization method. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string|integer $mTime If specified, you must enter a date/timestamp, otherwise it's the current time. |
|
38
|
|
|
* |
|
39
|
|
|
* @return CDateTime |
|
40
|
|
|
*/ |
|
41
|
|
|
public function get($mTime = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$sSetTime = (!empty($mTime)) ? date('Y-m-d H:i:s', (!is_numeric($mTime) ? strtotime($mTime) : $mTime) ) : 'now'; |
|
44
|
|
|
$this->_oDateTime = new DateTime($sSetTime, new DateTimeZone($this->_oConfig->values['language.application']['timezone'])); |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get Date Time. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $sFormat |
|
53
|
|
|
* |
|
54
|
|
|
* @return string Date time format |
|
55
|
|
|
*/ |
|
56
|
|
|
public function dateTime($sFormat = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$sFormat = (empty($sFormat)) ? $this->_oConfig->values['language.application']['date_time_format'] : $sFormat; |
|
59
|
|
|
|
|
60
|
|
|
return $this->_oDateTime->format($sFormat); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get Date. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $sFormat |
|
67
|
|
|
* |
|
68
|
|
|
* @return string Date format |
|
69
|
|
|
*/ |
|
70
|
|
|
public function date($sFormat = null) |
|
71
|
|
|
{ |
|
72
|
|
|
$sFormat = (empty($sFormat)) ? $this->_oConfig->values['language.application']['date_format'] : $sFormat; |
|
73
|
|
|
|
|
74
|
|
|
return $this->_oDateTime->format($sFormat); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get Time. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $sFormat |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Time format |
|
83
|
|
|
*/ |
|
84
|
|
|
public function time($sFormat = null) |
|
85
|
|
|
{ |
|
86
|
|
|
$sFormat = (empty($sFormat)) ? $this->_oConfig->values['language.application']['time_format'] : $sFormat; |
|
87
|
|
|
|
|
88
|
|
|
return $this->_oDateTime->format($sFormat); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.