├── .codacy.yml ├── .github └── FUNDING.yml ├── .php_cs ├── LICENSE ├── README.md ├── composer.json ├── phpmd.xml └── src ├── Date.php └── DateServiceProvider.php /.codacy.yml: -------------------------------------------------------------------------------- 1 | --- 2 | engines: 3 | phpmd: 4 | enabled: true 5 | config: 6 | rulesets: "phpmd.xml" 7 | rubocop: 8 | enabled: true 9 | shellcheck: 10 | enabled: true 11 | stylelint: 12 | enabled: true 13 | duplication: 14 | enabled: true 15 | metrics: 16 | enabled: true 17 | coverage: 18 | enabled: true 19 | structure: 20 | enabled: true 21 | sonar-php: 22 | enabled: true 23 | fixme: 24 | enabled: true 25 | phpcodesniffer: 26 | enabled: true 27 | exclude_paths: 28 | - 'tests/**' 29 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [jenssegers] 2 | tidelift: "packagist/jenssegers/date" 3 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | in(['src', 'tests']); 8 | 9 | return Config::create() 10 | ->setRules([ 11 | '@Symfony' => true, 12 | 'ordered_imports' => true, 13 | 'phpdoc_align' => false, 14 | 'phpdoc_to_comment' => false, 15 | 'phpdoc_inline_tag' => false, 16 | 'yoda_style' => false, 17 | 'blank_line_before_statement' => false, 18 | 'phpdoc_separation' => false, 19 | 'pre_increment' => false, 20 | 'increment_style' => false, 21 | 'phpdoc_types' => false, 22 | ]) 23 | ->setFinder($finder); 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jens Segers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Date 2 | ==== 3 | 4 | [![Latest Stable Version](http://img.shields.io/github/release/jenssegers/date.svg)](https://packagist.org/packages/jenssegers/date) [![Total Downloads](http://img.shields.io/packagist/dm/jenssegers/date.svg)](https://packagist.org/packages/jenssegers/date) [![Build Status](http://img.shields.io/travis/jenssegers/date.svg)](https://travis-ci.org/jenssegers/date) [![Coverage Status](https://coveralls.io/repos/github/jenssegers/date/badge.svg?branch=master)](https://coveralls.io/github/jenssegers/date?branch=master) [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg)](https://www.paypal.me/jenssegers) 5 | 6 | This date library extends [Carbon](https://github.com/briannesbitt/Carbon) with multi-language support. Methods such as `format`, `diffForHumans`, `parse`, `createFromFormat` and the new `timespan`, will now be translated based on your locale. 7 | 8 | > All translations made by contributors have been moved to the Carbon 2 package. This package now uses the Carbon translations to provide you with better multi-language support. Translation issues should be reported on the [Carbon repository](https://github.com/briannesbitt/Carbon). Please also check out the original documentation [here](https://carbon.nesbot.com/docs). 9 | 10 | Installation 11 | ------------ 12 | 13 | Install using composer: 14 | 15 | ```bash 16 | composer require jenssegers/date 17 | ``` 18 | 19 | Laravel 20 | ------- 21 | 22 | There is a service provider included for integration with the Laravel framework. This provider will get the application locale setting and use this for translations. This service will be automatically registered if you use Laravel 5.5+ using the auto-discovery. Else to register the service provider, add the following to the providers array in `config/app.php`: 23 | 24 | ```php 25 | Jenssegers\Date\DateServiceProvider::class, 26 | ``` 27 | 28 | You can also add it as a Facade in `config/app.php`: 29 | 30 | ```php 31 | 'Date' => Jenssegers\Date\Date::class, 32 | ``` 33 | 34 | Languages 35 | --------- 36 | 37 | This package contains language files for the following languages (https://carbon.nesbot.com/docs/#supported-locales): 38 | 39 | - Afar (aa) 40 | - Afrikaans (af) 41 | - Aghem (agq) 42 | - Aguaruna (agr) 43 | - Akan (ak) 44 | - Amharic (am) 45 | - Aragonese (an) 46 | - Angika (anp) 47 | - Arabic (ar) 48 | - Assamese (as) 49 | - Asu (asa) 50 | - Asturian (ast) 51 | - Southern Aymara (ayc) 52 | - Azerbaijani (az) 53 | - Basaa (bas) 54 | - Belarusian (be) 55 | - Bemba (bem) 56 | - ber (ber) 57 | - Bena (bez) 58 | - Bulgarian (bg) 59 | - Bhili (bhb) 60 | - Bhojpuri (bho) 61 | - Bislama (bi) 62 | - Bambara (bm) 63 | - Bengali (bn) 64 | - Tibetan (bo) 65 | - Breton (br) 66 | - Bodo (brx) 67 | - Bosnian (bs) 68 | - Bilin (byn) 69 | - Catalan (ca) 70 | - Chakma (ccp) 71 | - Chechen (ce) 72 | - Chiga (cgg) 73 | - Cherokee (chr) 74 | - Chinese (cmn) 75 | - Crimean Turkish (crh) 76 | - Czech (cs) 77 | - Kashubian (csb) 78 | - Church Slavic (cu) 79 | - Chuvash (cv) 80 | - Welsh (cy) 81 | - Danish (da) 82 | - Taita (dav) 83 | - German (de) 84 | - Zarma (dje) 85 | - Dogri (macrolanguage) (doi) 86 | - Lower Sorbian (dsb) 87 | - Duala (dua) 88 | - Divehi (dv) 89 | - Jola-Fonyi (dyo) 90 | - Dzongkha (dz) 91 | - Embu (ebu) 92 | - Ewe (ee) 93 | - Greek (modern) (el) 94 | - English (en) 95 | - Esperanto (eo) 96 | - Spanish (es) 97 | - Estonian (et) 98 | - Basque (eu) 99 | - Ewondo (ewo) 100 | - Persian (fa) 101 | - Fulah (ff) 102 | - Finnish (fi) 103 | - Filipino (fil) 104 | - Faroese (fo) 105 | - French (fr) 106 | - Friulian (fur) 107 | - Western Frisian (fy) 108 | - Irish (ga) 109 | - Gaelic (gd) 110 | - Geez (gez) 111 | - Galician (gl) 112 | - Konkani (gom) 113 | - Swiss German (gsw) 114 | - Gujarati (gu) 115 | - Gusii (guz) 116 | - Manx (gv) 117 | - Hausa (ha) 118 | - Hakka Chinese (hak) 119 | - Hawaiian (haw) 120 | - Hebrew (modern) (he) 121 | - Hindi (hi) 122 | - Fiji Hindi (hif) 123 | - Chhattisgarhi (hne) 124 | - Croatian (hr) 125 | - Upper Sorbian (hsb) 126 | - Haitian (ht) 127 | - Hungarian (hu) 128 | - Armenian (hy) 129 | - i18n (i18n) 130 | - Interlingua (ia) 131 | - Indonesian (id) 132 | - Igbo (ig) 133 | - Sichuan Yi (ii) 134 | - Inupiaq (ik) 135 | - in (in) 136 | - Icelandic (is) 137 | - Italian (it) 138 | - Inuktitut (iu) 139 | - iw (iw) 140 | - Japanese (ja) 141 | - Ngomba (jgo) 142 | - Machame (jmc) 143 | - Javanese (jv) 144 | - Georgian (ka) 145 | - Kabyle (kab) 146 | - Kamba (kam) 147 | - Makonde (kde) 148 | - Kabuverdianu (kea) 149 | - Koyra Chiini (khq) 150 | - Kikuyu (ki) 151 | - Kazakh (kk) 152 | - Kako (kkj) 153 | - Kalaallisut (kl) 154 | - Kalenjin (kln) 155 | - Central Khmer (km) 156 | - Kannada (kn) 157 | - Korean (ko) 158 | - Konkani (kok) 159 | - Kashmiri (ks) 160 | - Shambala (ksb) 161 | - Bafia (ksf) 162 | - Colognian (ksh) 163 | - Kurdish (ku) 164 | - Cornish (kw) 165 | - Kirghiz (ky) 166 | - Langi (lag) 167 | - Luxembourgish (lb) 168 | - Ganda (lg) 169 | - Limburgan (li) 170 | - Ligurian (lij) 171 | - Lakota (lkt) 172 | - Lingala (ln) 173 | - Lao (lo) 174 | - Northern Luri (lrc) 175 | - Lithuanian (lt) 176 | - Luba-Katanga (lu) 177 | - Luo (luo) 178 | - Luyia (luy) 179 | - Latvian (lv) 180 | - Literary Chinese (lzh) 181 | - Magahi (mag) 182 | - Maithili (mai) 183 | - Masai (mas) 184 | - Meru (mer) 185 | - Morisyen (mfe) 186 | - Malagasy (mg) 187 | - Makhuwa-Meetto (mgh) 188 | - Metaʼ (mgo) 189 | - Eastern Mari (mhr) 190 | - Maori (mi) 191 | - Mískito (miq) 192 | - Karbi (mjw) 193 | - Macedonian (mk) 194 | - Malayalam (ml) 195 | - Mongolian (mn) 196 | - Manipuri (mni) 197 | - mo (mo) 198 | - Marathi (mr) 199 | - Malay (ms) 200 | - Maltese (mt) 201 | - Mundang (mua) 202 | - Burmese (my) 203 | - Mazanderani (mzn) 204 | - Min Nan Chinese (nan) 205 | - Nama (naq) 206 | - Norwegian Bokmål (nb) 207 | - North Ndebele (nd) 208 | - Low German (nds) 209 | - Nepali (ne) 210 | - Central Nahuatl (nhn) 211 | - Niuean (niu) 212 | - Dutch (nl) 213 | - Kwasio (nmg) 214 | - Norwegian Nynorsk (nn) 215 | - Ngiemboon (nnh) 216 | - Norwegian (no) 217 | - South Ndebele (nr) 218 | - Northern Sotho (nso) 219 | - Nuer (nus) 220 | - Nyankole (nyn) 221 | - Occitan (oc) 222 | - Oromo (om) 223 | - Oriya (or) 224 | - Ossetian (os) 225 | - Panjabi (pa) 226 | - Papiamento (pap) 227 | - Polish (pl) 228 | - Prussian (prg) 229 | - Pashto (ps) 230 | - Portuguese (pt) 231 | - Quechua (qu) 232 | - Cusco Quechua (quz) 233 | - Rajasthani (raj) 234 | - Romansh (rm) 235 | - Rundi (rn) 236 | - Romanian (ro) 237 | - Rombo (rof) 238 | - Russian (ru) 239 | - Kinyarwanda (rw) 240 | - Rwa (rwk) 241 | - Sanskrit (sa) 242 | - Sakha (sah) 243 | - Samburu (saq) 244 | - Santali (sat) 245 | - Sangu (sbp) 246 | - Sardinian (sc) 247 | - Sindhi (sd) 248 | - Northern Sami (se) 249 | - Sena (seh) 250 | - Koyraboro Senni (ses) 251 | - Sango (sg) 252 | - Samogitian (sgs) 253 | - sh (sh) 254 | - Tachelhit (shi) 255 | - Shan (shn) 256 | - Shuswap (shs) 257 | - Sinhala (si) 258 | - Sidamo (sid) 259 | - Slovak (sk) 260 | - Slovene (sl) 261 | - Samoan (sm) 262 | - Inari Sami (smn) 263 | - Shona (sn) 264 | - Somali (so) 265 | - Albanian (sq) 266 | - Serbian (sr) 267 | - Swati (ss) 268 | - Southern Sotho (st) 269 | - Swedish (sv) 270 | - Swahili (sw) 271 | - Silesian (szl) 272 | - Tamil (ta) 273 | - Tulu (tcy) 274 | - Telugu (te) 275 | - Teso (teo) 276 | - Tetum (tet) 277 | - Tajik (tg) 278 | - Thai (th) 279 | - Chitwania Tharu (the) 280 | - Tigrinya (ti) 281 | - Tigre (tig) 282 | - Turkmen (tk) 283 | - Tagalog (tl) 284 | - Klingon (tlh) 285 | - Tswana (tn) 286 | - Tongan (Tonga Islands) (to) 287 | - Tok Pisin (tpi) 288 | - Turkish (tr) 289 | - Tsonga (ts) 290 | - Tatar (tt) 291 | - Tasawaq (twq) 292 | - Talossan (tzl) 293 | - Tamazight (tzm) 294 | - Uighur (ug) 295 | - Ukrainian (uk) 296 | - Unami (unm) 297 | - Urdu (ur) 298 | - Uzbek (uz) 299 | - Vai (vai) 300 | - Venda (ve) 301 | - Vietnamese (vi) 302 | - Volapük (vo) 303 | - Vunjo (vun) 304 | - Walloon (wa) 305 | - Walser (wae) 306 | - Wolaytta (wal) 307 | - Wolof (wo) 308 | - Xhosa (xh) 309 | - Soga (xog) 310 | - Yangben (yav) 311 | - Yiddish (yi) 312 | - Yoruba (yo) 313 | - Cantonese (yue) 314 | - Yau (Morobe Province) (yuw) 315 | - Standard Moroccan Tamazight (zgh) 316 | - Chinese (zh) 317 | - Zulu (zu) 318 | 319 | Usage 320 | ----- 321 | 322 | The Date class extends the Carbon methods such as `format` and `diffForHumans`, and translates them based on your locale: 323 | 324 | ```php 325 | use Jenssegers\Date\Date; 326 | 327 | Date::setLocale('nl'); 328 | 329 | echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16 330 | 331 | echo Date::parse('-1 day')->diffForHumans(); // 1 dag geleden 332 | ``` 333 | 334 | The Date class also added some aliases and additional methods such as: `ago` which is an alias for `diffForHumans`, and the `timespan` method: 335 | 336 | ```php 337 | echo $date->timespan(); // 3 months, 1 week, 1 day, 3 hours, 20 minutes 338 | ``` 339 | 340 | Methods such as `parse` and `createFromFormat` also support "reverse translations". When calling these methods with translated input, it will try to translate it to English before passing it to DateTime: 341 | 342 | ```php 343 | $date = Date::createFromFormat('l d F Y', 'zaterdag 21 maart 2015'); 344 | ``` 345 | 346 | Carbon 347 | ------ 348 | 349 | Carbon is the library the Date class is based on. All of the original Carbon operations are still available, check out for more information. Here are some of the available methods: 350 | 351 | ### Creating dates 352 | 353 | You can create Date objects just like the DateTime object (): 354 | 355 | ```php 356 | $date = new Date(); 357 | $date = new Date('2000-01-31'); 358 | $date = new Date('2000-01-31 12:00:00'); 359 | 360 | // With time zone 361 | $date = new Date('2000-01-31', new DateTimeZone('Europe/Brussels')); 362 | ``` 363 | 364 | You can skip the creation of a DateTimeZone object: 365 | 366 | ```php 367 | $date = new Date('2000-01-31', 'Europe/Brussels'); 368 | ``` 369 | 370 | Create Date objects from a relative format (): 371 | 372 | ```php 373 | $date = new Date('now'); 374 | $date = new Date('today'); 375 | $date = new Date('+1 hour'); 376 | $date = new Date('next monday'); 377 | ``` 378 | 379 | This is also available using these static methods: 380 | 381 | ```php 382 | $date = Date::parse('now'); 383 | $date = Date::now(); 384 | ``` 385 | 386 | Creating a Date from a timestamp: 387 | 388 | ```php 389 | $date = new Date(1367186296); 390 | ``` 391 | 392 | Or from an existing date or time: 393 | 394 | ```php 395 | $date = Date::createFromDate(2000, 1, 31); 396 | $date = Date::createFromTime(12, 0, 0); 397 | $date = Date::create(2000, 1, 31, 12, 0, 0); 398 | ``` 399 | 400 | ### Formatting Dates 401 | 402 | You can format a Date object like the DateTime object (): 403 | 404 | ```php 405 | echo Date::now()->format('Y-m-d'); // 2000-01-31 406 | ``` 407 | 408 | The Date object can be cast to a string: 409 | 410 | ```php 411 | echo Date::now(); // 2000-01-31 12:00:00 412 | ``` 413 | 414 | Get a human readable output (alias for diffForHumans): 415 | 416 | ```php 417 | echo $date->ago(); // 5 days ago 418 | ``` 419 | 420 | Calculate a timespan: 421 | 422 | ```php 423 | $date = new Date('+1000 days'); 424 | echo Date::now()->timespan($date); 425 | // 2 years, 8 months, 3 weeks, 5 days 426 | 427 | // or even 428 | echo Date::now()->timespan('+1000 days'); 429 | ``` 430 | 431 | Get years since date: 432 | 433 | ```php 434 | $date = new Date('-10 years'); 435 | echo $date->age; // 10 436 | 437 | $date = new Date('+10 years'); 438 | echo $date->age; // -10 439 | ``` 440 | 441 | ### Manipulating Dates 442 | 443 | You can manipulate by using the *add* and *sub* methods, with relative intervals (): 444 | 445 | ```php 446 | $yesterday = Date::now()->sub('1 day'); 447 | $tomorrow = Date::now()->add('1 day'); 448 | 449 | // ISO 8601 450 | $date = Date::now()->add('P2Y4DT6H8M'); 451 | ``` 452 | 453 | You can access and modify all date attributes as an object: 454 | 455 | ```php 456 | $date->year = 2013: 457 | $date->month = 1; 458 | $date->day = 31; 459 | 460 | $date->hour = 12; 461 | $date->minute = 0; 462 | $date->second = 0; 463 | ``` 464 | 465 | ## Contributing 466 | 467 | Language contributions should made to . 468 | 469 | ## License 470 | 471 | Laravel Date is licensed under [The MIT License (MIT)](LICENSE). 472 | 473 | ## Security contact information 474 | 475 | To report a security vulnerability, follow [these steps](https://tidelift.com/security). 476 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jenssegers/date", 3 | "description": "A date library to help you work with dates in different languages", 4 | "keywords": ["laravel", "date", "time", "datetime", "i18n", "translation", "carbon"], 5 | "homepage": "https://github.com/jenssegers/date", 6 | "license" : "MIT", 7 | "authors": [ 8 | { 9 | "name": "Jens Segers", 10 | "homepage": "https://jenssegers.com" 11 | } 12 | ], 13 | "require": { 14 | "nesbot/carbon": "^2.72.0" 15 | }, 16 | "require-dev": { 17 | "phpunit/phpunit": "^10.4.0", 18 | "php-coveralls/php-coveralls": "^2.7" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Jenssegers\\Date\\": "src/" 23 | } 24 | }, 25 | "autoload-dev": { 26 | "psr-4": { 27 | "Tests\\Jenssegers\\": "tests/" 28 | } 29 | }, 30 | "extra": { 31 | "branch-alias": { 32 | "dev-master": "3.1-dev" 33 | }, 34 | "laravel": { 35 | "providers": [ 36 | "Jenssegers\\Date\\DateServiceProvider" 37 | ], 38 | "aliases": { 39 | "Date": "Jenssegers\\Date\\Date" 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /phpmd.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | Mess detection rules for Jenssegers\Date 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Date.php: -------------------------------------------------------------------------------- 1 | app['events']->listen($localeChangedEvent, function () { 26 | $this->setLocale(); 27 | }); 28 | 29 | $this->setLocale(); 30 | } 31 | 32 | /** 33 | * Set the locale. 34 | */ 35 | protected function setLocale() 36 | { 37 | $locale = $this->app['translator']->getLocale(); 38 | 39 | Date::setLocale($locale); 40 | } 41 | 42 | /** 43 | * Register the service provider. 44 | */ 45 | public function register() 46 | { 47 | // Nothing. 48 | } 49 | 50 | /** 51 | * Get the services provided by the provider. 52 | * 53 | * @return array 54 | */ 55 | public function provides() 56 | { 57 | return ['Date']; 58 | } 59 | } 60 | --------------------------------------------------------------------------------