|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leaf; |
| 4 | + |
| 5 | +use \DateTime; |
| 6 | + |
| 7 | +/** |
| 8 | + * Leaf Date |
| 9 | + * ---------------------- |
| 10 | + * Quick date/time manipulation with Leaf |
| 11 | + * |
| 12 | + * @author Michael Darko |
| 13 | + * @since 1.1.0 |
| 14 | + */ |
| 15 | +class Date |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Generate a random timestamp |
| 19 | + */ |
| 20 | + public static function randomTimestamp($start = 1149095981, $end = 1749095981) |
| 21 | + { |
| 22 | + $random = mt_rand($start, $end); |
| 23 | + return date("Y-m-d H:i:s", $random); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Generate a random date |
| 28 | + */ |
| 29 | + public static function randomDate($start = 1149095981, $end = 1749095981) |
| 30 | + { |
| 31 | + $timestamp = mt_rand($start, $end); |
| 32 | + $randomDate = new DateTime(); |
| 33 | + $randomDate->setTimestamp($timestamp); |
| 34 | + $randomDate = json_decode(json_encode($randomDate), true); |
| 35 | + return $randomDate['date']; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Set default date timezone |
| 40 | + */ |
| 41 | + public static function setTimezone(String $timezone = "Africa/Accra") |
| 42 | + { |
| 43 | + date_default_timezone_set($timezone); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Set default date timezone |
| 48 | + */ |
| 49 | + public static function getTimezone() |
| 50 | + { |
| 51 | + return date_default_timezone_get(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Parse unix date |
| 56 | + */ |
| 57 | + public static function rawDate($date, $format = 'D, d M Y H:i:s') |
| 58 | + { |
| 59 | + return date($format, $date); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Return current date(timestamp) |
| 64 | + */ |
| 65 | + public static function now($useTFFormat = true) |
| 66 | + { |
| 67 | + return date($useTFFormat ? 'Y-m-d H:i:s' : 'Y-m-d h:i:s a', time()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get the date a number of days ago |
| 72 | + */ |
| 73 | + public static function daysAgo(int $days_ago, $date = null) |
| 74 | + { |
| 75 | + return date('Y-m-d', strtotime("-$days_ago days", strtotime(self::toDate($date ?? self::now())))); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the date a number of months ago |
| 80 | + */ |
| 81 | + public static function monthsAgo(int $months_ago, $date = null) |
| 82 | + { |
| 83 | + return date('Y-m-d', strtotime("-$months_ago months", strtotime(self::toDate($date ?? self::now())))); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the date a number of years ago |
| 88 | + */ |
| 89 | + public static function yearsAgo(int $years_ago, $date = null) |
| 90 | + { |
| 91 | + return date('Y-m-d', strtotime("-$years_ago years", strtotime(self::toDate($date ?? self::now())))); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Convert a timstamp to a date |
| 96 | + */ |
| 97 | + public static function toDate($timestamp, $format = 'Y-m-d') |
| 98 | + { |
| 99 | + $timestamp = new DateTime($timestamp); |
| 100 | + $date = $timestamp; |
| 101 | + return $date->format($format); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get a neatly formatted english date from a timestamp |
| 106 | + */ |
| 107 | + public static function toEnglishDate($timestamp) |
| 108 | + { |
| 109 | + $timestamp = new DateTime($timestamp); |
| 110 | + $day = $timestamp->format('d'); |
| 111 | + $month = $timestamp->format('m'); |
| 112 | + $month = ltrim($month, 0); |
| 113 | + $month = self::intToMonth($month); |
| 114 | + $year = $timestamp->format('Y'); |
| 115 | + $date = $month . ' ' . $day . ', ' . $year; |
| 116 | + return $date; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Format a timestamp to an english readable of a timestamp |
| 121 | + */ |
| 122 | + public static function toEnglishTs($timestamp) |
| 123 | + { |
| 124 | + $timestampp = new DateTime($timestamp); |
| 125 | + $day = $timestampp->format('d'); |
| 126 | + $month = $timestampp->format('m'); |
| 127 | + $month = self::intToMonth(ltrim($month, '0')); |
| 128 | + $year = $timestampp->format('Y'); |
| 129 | + $time = self::toTime($timestamp); |
| 130 | + $english_timeStamp = $day . ' ' . $month . ' ' . $year . ' ' . $time; |
| 131 | + return $english_timeStamp; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the time from a timestamp |
| 136 | + */ |
| 137 | + public static function toTime($ts) |
| 138 | + { |
| 139 | + $ts = new DateTime($ts); |
| 140 | + return $ts->format('G:i:s'); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Format a TimeStamp |
| 145 | + */ |
| 146 | + public static function format($ts, $format = "Y-m-d") |
| 147 | + { |
| 148 | + $ts = new DateTime($ts); |
| 149 | + return $ts->format($format); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the current year |
| 154 | + */ |
| 155 | + public static function year() |
| 156 | + { |
| 157 | + return date('Y', time()); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get current month |
| 162 | + */ |
| 163 | + public static function month() |
| 164 | + { |
| 165 | + return date('m', time()); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get current day |
| 170 | + */ |
| 171 | + public static function day() |
| 172 | + { |
| 173 | + return date('m', time()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Get the month from a number |
| 178 | + */ |
| 179 | + public static function intToMonth(int $number) |
| 180 | + { |
| 181 | + $number = ltrim($number, '0'); |
| 182 | + $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; |
| 183 | + $month = $months[$number - 1]; |
| 184 | + return $month; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Get the day from a number |
| 189 | + */ |
| 190 | + public static function intToDay(int $number) |
| 191 | + { |
| 192 | + $number = ltrim($number, '0'); |
| 193 | + $days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
| 194 | + $day = $days[$number - 1]; |
| 195 | + return $day; |
| 196 | + } |
| 197 | +} |
0 commit comments