|
| 1 | +<?php namespace Otherguy\Currency\Helpers; |
| 2 | + |
| 3 | +use DateInterval; |
| 4 | +use DateTime; |
| 5 | +use DateTimeInterface; |
| 6 | +use Exception; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class DateHelper |
| 10 | + * |
| 11 | + * @package Otherguy\Currency\Helpers |
| 12 | + */ |
| 13 | +class DateHelper |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * Parse a date from string with a format to a DateTime object |
| 18 | + * |
| 19 | + * @param string $string |
| 20 | + * @param string $format |
| 21 | + * |
| 22 | + * @return DateTime |
| 23 | + */ |
| 24 | + public static function parse(string $string, string $format): DateTime |
| 25 | + { |
| 26 | + return DateTime::createFromFormat($format, $string); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Format a date (or interval) to a string with a given format |
| 31 | + * |
| 32 | + * See formatting options as in PHP date() |
| 33 | + * |
| 34 | + * @param int|string|DateTime|DateInterval|DateTimeInterface $date |
| 35 | + * @param string $format |
| 36 | + * |
| 37 | + * @return string |
| 38 | + * |
| 39 | + * @throws Exception |
| 40 | + * |
| 41 | + * @see date() |
| 42 | + */ |
| 43 | + public static function format($date, string $format): string |
| 44 | + { |
| 45 | + if ($date instanceof DateTime || $date instanceof DateTimeInterface || $date instanceof DateInterval) { |
| 46 | + return $date->format($format); |
| 47 | + } else if ($date === 'now') { |
| 48 | + return date($format); |
| 49 | + } else if (is_string($date)) { |
| 50 | + return (new DateTime($date))->format($format); |
| 51 | + } else { |
| 52 | + $timestamp = (integer)$date; |
| 53 | + return date($format, $timestamp); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Get a date object by given date or time format |
| 60 | + * |
| 61 | + * Examples:: |
| 62 | + * |
| 63 | + * Date.create('2018-12-04') |
| 64 | + * Date.create('first day of next year') |
| 65 | + * |
| 66 | + * @param String $time A date/time string. For valid formats see http://php.net/manual/en/datetime.formats.php |
| 67 | + * |
| 68 | + * @return DateTime |
| 69 | + * |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + public static function create(string $time): DateTime |
| 73 | + { |
| 74 | + return new DateTime($time); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the current date and time |
| 79 | + * |
| 80 | + * Examples:: |
| 81 | + * |
| 82 | + * Date.now().timestamp |
| 83 | + * |
| 84 | + * @return DateTime |
| 85 | + * |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | + public static function now(): DateTime |
| 89 | + { |
| 90 | + return new DateTime('now'); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the current date |
| 95 | + * |
| 96 | + * @return DateTime |
| 97 | + * |
| 98 | + * @throws Exception |
| 99 | + */ |
| 100 | + public static function today(): DateTime |
| 101 | + { |
| 102 | + return new DateTime('today'); |
| 103 | + } |
| 104 | +} |
0 commit comments