| 1 | <?php |
||
| 5 | class DateUtil |
||
| 6 | { |
||
| 7 | 4 | public static function getDefaultParams(\DateTime $dateTime = null, $noTime = false, $useTimezone = false) |
|
| 8 | { |
||
| 9 | 4 | $params = array(); |
|
| 10 | |||
| 11 | 4 | if ($useTimezone) { |
|
| 12 | $timeZone = $dateTime->getTimezone()->getName(); |
||
|
|
|||
| 13 | $params['TZID'] = $timeZone; |
||
| 14 | } |
||
| 15 | |||
| 16 | 4 | if ($noTime) { |
|
| 17 | $params['VALUE'] = 'DATE'; |
||
| 18 | } |
||
| 19 | |||
| 20 | 4 | return $params; |
|
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Returns a formatted date string. |
||
| 25 | * |
||
| 26 | * @param \DateTime|null $dateTime The DateTime object |
||
| 27 | * @param bool $noTime Indicates if the time will be added |
||
| 28 | * @param bool $useTimezone |
||
| 29 | * @param bool $useUtc |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | 4 | public static function getDateString(\DateTime $dateTime = null, $noTime = false, $useTimezone = false, $useUtc = false) |
|
| 34 | { |
||
| 35 | 4 | if (empty($dateTime)) { |
|
| 36 | 2 | $dateTime = new \DateTime(); |
|
| 37 | } |
||
| 38 | |||
| 39 | 4 | return $dateTime->format(self::getDateFormat($noTime, $useTimezone, $useUtc)); |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the date format that can be passed to DateTime::format(). |
||
| 44 | * |
||
| 45 | * @param bool $noTime Indicates if the time will be added |
||
| 46 | * @param bool $useTimezone |
||
| 47 | * @param bool $useUtc |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 4 | public static function getDateFormat($noTime = false, $useTimezone = false, $useUtc = false) |
|
| 60 | } |
||
| 61 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: