You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
6.4 KiB

3 years ago
  1. # Carbon
  2. [![Latest Stable Version](https://img.shields.io/packagist/v/nesbot/carbon.svg?style=flat-square)](https://packagist.org/packages/nesbot/carbon)
  3. [![Total Downloads](https://img.shields.io/packagist/dt/nesbot/carbon.svg?style=flat-square)](https://packagist.org/packages/nesbot/carbon)
  4. [![GitHub Actions](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbriannesbitt%2FCarbon%2Fbadge&style=flat-square&label=Build&logo=none)](https://actions-badge.atrox.dev/briannesbitt/Carbon/goto)
  5. [![StyleCI](https://github.styleci.io/repos/5724990/shield?style=flat-square)](https://github.styleci.io/repos/5724990)
  6. [![codecov.io](https://img.shields.io/codecov/c/github/briannesbitt/Carbon.svg?style=flat-square)](https://codecov.io/github/briannesbitt/Carbon?branch=master)
  7. [![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat-square)](https://github.com/phpstan/phpstan)
  8. [![Tidelift](https://tidelift.com/badges/github/briannesbitt/Carbon)](https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=readme)
  9. An international PHP extension for DateTime. [https://carbon.nesbot.com](https://carbon.nesbot.com)
  10. ```php
  11. <?php
  12. use Carbon\Carbon;
  13. printf("Right now is %s", Carbon::now()->toDateTimeString());
  14. printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString()
  15. $tomorrow = Carbon::now()->addDay();
  16. $lastWeek = Carbon::now()->subWeek();
  17. $nextSummerOlympics = Carbon::createFromDate(2016)->addYears(4);
  18. $officialDate = Carbon::now()->toRfc2822String();
  19. $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
  20. $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  21. $internetWillBlowUpOn = Carbon::create(2038, 01, 19, 3, 14, 7, 'GMT');
  22. // Don't really want this to happen so mock now
  23. Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1));
  24. // comparisons are always done in UTC
  25. if (Carbon::now()->gte($internetWillBlowUpOn)) {
  26. die();
  27. }
  28. // Phew! Return to normal behaviour
  29. Carbon::setTestNow();
  30. if (Carbon::now()->isWeekend()) {
  31. echo 'Party!';
  32. }
  33. // Over 200 languages (and over 500 regional variants) supported:
  34. echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
  35. echo Carbon::now()->subMinutes(2)->locale('zh_CN')->diffForHumans(); // '2分钟前'
  36. echo Carbon::parse('2019-07-23 14:51')->isoFormat('LLLL'); // 'Tuesday, July 23, 2019 2:51 PM'
  37. echo Carbon::parse('2019-07-23 14:51')->locale('fr_FR')->isoFormat('LLLL'); // 'mardi 23 juillet 2019 14:51'
  38. // ... but also does 'from now', 'after' and 'before'
  39. // rolling up to seconds, minutes, hours, days, months, years
  40. $daysSinceEpoch = Carbon::createFromTimestamp(0)->diffInDays();
  41. ```
  42. [Get supported nesbot/carbon with the Tidelift Subscription](https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=readme)
  43. ## Installation
  44. ### With Composer
  45. ```
  46. $ composer require nesbot/carbon
  47. ```
  48. ```json
  49. {
  50. "require": {
  51. "nesbot/carbon": "^2.16"
  52. }
  53. }
  54. ```
  55. ```php
  56. <?php
  57. require 'vendor/autoload.php';
  58. use Carbon\Carbon;
  59. printf("Now: %s", Carbon::now());
  60. ```
  61. ### Without Composer
  62. Why are you not using [composer](https://getcomposer.org/)? Download the Carbon [latest release](https://github.com/briannesbitt/Carbon/releases) and put the contents of the ZIP archive into a directory in your project. Then require the file `autoload.php` to get all classes and dependencies loaded on need.
  63. ```php
  64. <?php
  65. require 'path-to-Carbon-directory/autoload.php';
  66. use Carbon\Carbon;
  67. printf("Now: %s", Carbon::now());
  68. ```
  69. ## Docs
  70. [https://carbon.nesbot.com/docs](https://carbon.nesbot.com/docs)
  71. ## Security contact information
  72. To report a security vulnerability, please use the
  73. [Tidelift security contact](https://tidelift.com/security).
  74. Tidelift will coordinate the fix and disclosure.
  75. ## Credits
  76. ### Contributors
  77. This project exists thanks to all the people who contribute.
  78. <a href="https://github.com/briannesbitt/Carbon/graphs/contributors" target="_blank"><img src="https://opencollective.com/Carbon/contributors.svg?width=890&button=false" /></a>
  79. ### Translators
  80. [Thanks to people helping us to translate Carbon in so many languages](https://carbon.nesbot.com/contribute/translators/)
  81. ### Sponsors
  82. Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
  83. <a href="https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=readme" target="_blank"><img src="https://carbon.nesbot.com/tidelift-brand.png" width="256" height="64"></a>
  84. <a href="https://onlinecasinohex.ca/?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/0/avatar.svg" width="192" height="64"></a>
  85. <a href="https://opencollective.com/Carbon/sponsor/0/website" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/0/avatar.svg"></a>
  86. <a href="https://opencollective.com/Carbon/sponsor/1/website" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/1/avatar.svg"></a>
  87. <a href="https://opencollective.com/Carbon/sponsor/2/website" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/2/avatar.svg"></a>
  88. <a href="https://opencollective.com/Carbon/sponsor/3/website" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/3/avatar.svg"></a>
  89. <a href="https://opencollective.com/Carbon/sponsor/4/website" target="_blank"><img src="https://opencollective.com/Carbon/sponsor/4/avatar.svg"></a>
  90. [[Become a sponsor](https://opencollective.com/Carbon#sponsor)]
  91. ### Backers
  92. Thank you to all our backers! 🙏
  93. <a href="https://opencollective.com/Carbon#backers" target="_blank"><img src="https://opencollective.com/Carbon/backers.svg?width=890"></a>
  94. [[Become a backer](https://opencollective.com/Carbon#backer)]
  95. ## Carbon for enterprise
  96. Available as part of the Tidelift Subscription.
  97. The maintainers of ``Carbon`` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)