PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/freeads/laravel/vendor/nesbot/carbon/readme.md

https://gitlab.com/chalasr/Laravel_FreeAds
Markdown | 919 lines | 683 code | 236 blank | 0 comment | 0 complexity | 966a6417a63d780a4f7387b86b9b819a MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. > **This file is autogenerated. Please see the [Contributing](#about-contributing) section from more information.**
  2. # Carbon
  3. [![Latest Stable Version](https://poser.pugx.org/nesbot/carbon/v/stable.png)](https://packagist.org/packages/nesbot/carbon) [![Total Downloads](https://poser.pugx.org/nesbot/carbon/downloads.png)](https://packagist.org/packages/nesbot/carbon) [![Build Status](https://secure.travis-ci.org/briannesbitt/Carbon.png)](http://travis-ci.org/briannesbitt/Carbon)
  4. A simple API extension for DateTime with PHP 5.3+
  5. ```php
  6. printf("Right now is %s", Carbon::now()->toDateTimeString());
  7. printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString()
  8. $tomorrow = Carbon::now()->addDay();
  9. $lastWeek = Carbon::now()->subWeek();
  10. $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);
  11. $officialDate = Carbon::now()->toRfc2822String();
  12. $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
  13. $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  14. $worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT');
  15. // Don't really want to die so mock now
  16. Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1));
  17. // comparisons are always done in UTC
  18. if (Carbon::now()->gte($worldWillEnd)) {
  19. die();
  20. }
  21. // Phew! Return to normal behaviour
  22. Carbon::setTestNow();
  23. if (Carbon::now()->isWeekend()) {
  24. echo 'Party!';
  25. }
  26. echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
  27. // ... but also does 'from now', 'after' and 'before'
  28. // rolling up to seconds, minutes, hours, days, months, years
  29. $daysSinceEpoch = Carbon::createFromTimeStamp(0)->diffInDays();
  30. ```
  31. ## README Contents
  32. * [Installation](#install)
  33. * [Requirements](#requirements)
  34. * [With composer](#install-composer)
  35. * [Without composer](#install-nocomposer)
  36. * [API](#api)
  37. * [Instantiation](#api-instantiation)
  38. * [Testing Aids](#api-testing)
  39. * [Getters](#api-getters)
  40. * [Setters](#api-setters)
  41. * [Fluent Setters](#api-settersfluent)
  42. * [IsSet](#api-isset)
  43. * [String Formatting and Localization](#api-formatting)
  44. * [Common Formats](#api-commonformats)
  45. * [Comparison](#api-comparison)
  46. * [Addition and Subtraction](#api-addsub)
  47. * [Difference](#api-difference)
  48. * [Difference for Humans](#api-humandiff)
  49. * [Modifiers](#api-modifiers)
  50. * [Constants](#api-constants)
  51. * [About](#about)
  52. * [Contributing](#about-contributing)
  53. * [Author](#about-author)
  54. * [License](#about-license)
  55. * [History](#about-history)
  56. * [Why the name Carbon?](#about-whyname)
  57. <a name="install"/>
  58. ## Installation
  59. <a name="requirements"/>
  60. ### Requirements
  61. - Any flavour of PHP 5.3+ should do
  62. - [optional] PHPUnit to execute the test suite
  63. <a name="install-composer"/>
  64. ### With Composer
  65. The easiest way to install Carbon is via [composer](http://getcomposer.org/). Using the following command:
  66. ```
  67. $ composer require nesbot/carbon
  68. ```
  69. This will automatically find the latest version and include that one, for example `~1.13`.
  70. I you wish you can create the following `composer.json` file and run the `php composer.phar install` command to install it.
  71. ```json
  72. {
  73. "require": {
  74. "nesbot/carbon": "*"
  75. }
  76. }
  77. ```
  78. Note: we recommend using a defined version like `~1.13` instead of `*`,
  79. ```php
  80. <?php
  81. require 'vendor/autoload.php';
  82. use Carbon\Carbon;
  83. printf("Now: %s", Carbon::now());
  84. ```
  85. <a name="install-nocomposer"/>
  86. ### Without Composer
  87. Why are you not using [composer](http://getcomposer.org/)? Download [Carbon.php](https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php) from the repo and save the file into your project path somewhere.
  88. ```php
  89. <?php
  90. require 'path/to/Carbon.php';
  91. use Carbon\Carbon;
  92. printf("Now: %s", Carbon::now());
  93. ```
  94. <a name="api"/>
  95. ## API
  96. The Carbon class is [inherited](http://php.net/manual/en/keyword.extends.php) from the PHP [DateTime](http://www.php.net/manual/en/class.datetime.php) class.
  97. ```php
  98. <?php
  99. class Carbon extends \DateTime
  100. {
  101. // code here
  102. }
  103. ```
  104. Carbon has all of the functions inherited from the base DateTime class. This approach allows you to access the base functionality if you see anything missing in Carbon but is there in DateTime.
  105. > **Note: I live in Ottawa, Ontario, Canada and if the timezone is not specified in the examples then the default of 'America/Toronto' is to be assumed. Typically Ottawa is -0500 but when daylight savings time is on we are -0400.**
  106. Special care has been taken to ensure timezones are handled correctly, and where appropriate are based on the underlying DateTime implementation. For example all comparisons are done in UTC or in the timezone of the datetime being used.
  107. ```php
  108. $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');
  109. $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');
  110. echo $dtVancouver->diffInHours($dtToronto); // 3
  111. ```
  112. Also `is` comparisons are done in the timezone of the provided Carbon instance. For example my current timezone is -13 hours from Tokyo. So `Carbon::now('Asia/Tokyo')->isToday()` would only return false for any time past 1 PM my time. This doesn't make sense since `now()` in tokyo is always today in Tokyo. Thus the comparison to `now()` is done in the same timezone as the current instance.
  113. <a name="api-instantiation"/>
  114. ### Instantiation
  115. There are several different methods available to create a new instance of Carbon. First there is a constructor. It overrides the [parent constructor](http://www.php.net/manual/en/datetime.construct.php) and you are best to read about the first parameter from the PHP manual and understand the date/time string formats it accepts. You'll hopefully find yourself rarely using the constructor but rather relying on the explicit static methods for improved readability.
  116. ```php
  117. $carbon = new Carbon(); // equivalent to Carbon::now()
  118. $carbon = new Carbon('first day of January 2008', 'America/Vancouver');
  119. echo get_class($carbon); // 'Carbon\Carbon'
  120. ```
  121. You'll notice above that the timezone (2nd) parameter was passed as a string rather than a `\DateTimeZone` instance. All DateTimeZone parameters have been augmented so you can pass a DateTimeZone instance or a string and the timezone will be created for you. This is again shown in the next example which also introduces the `now()` function.
  122. ```php
  123. $now = Carbon::now();
  124. $nowInLondonTz = Carbon::now(new DateTimeZone('Europe/London'));
  125. // or just pass the timezone as a string
  126. $nowInLondonTz = Carbon::now('Europe/London');
  127. ```
  128. If you really love your fluid method calls and get frustrated by the extra line or ugly pair of brackets necessary when using the constructor you'll enjoy the `parse` method.
  129. ```php
  130. echo (new Carbon('first day of December 2008'))->addWeeks(2); // 2008-12-15 00:00:00
  131. echo Carbon::parse('first day of December 2008')->addWeeks(2); // 2008-12-15 00:00:00
  132. ```
  133. To accompany `now()`, a few other static instantiation helpers exist to create widely known instances. The only thing to really notice here is that `today()`, `tomorrow()` and `yesterday()`, besides behaving as expected, all accept a timezone parameter and each has their time value set to `00:00:00`.
  134. ```php
  135. $now = Carbon::now();
  136. echo $now; // 2015-02-06 00:04:01
  137. $today = Carbon::today();
  138. echo $today; // 2015-02-06 00:00:00
  139. $tomorrow = Carbon::tomorrow('Europe/London');
  140. echo $tomorrow; // 2015-02-07 00:00:00
  141. $yesterday = Carbon::yesterday();
  142. echo $yesterday; // 2015-02-05 00:00:00
  143. ```
  144. The next group of static helpers are the `createXXX()` helpers. Most of the static `create` functions allow you to provide as many or as few arguments as you want and will provide default values for all others. Generally default values are the current date, time or timezone. Higher values will wrap appropriately but invalid values will throw an `InvalidArgumentException` with an informative message. The message is obtained from an [DateTime::getLastErrors()](http://php.net/manual/en/datetime.getlasterrors.php) call.
  145. ```php
  146. Carbon::createFromDate($year, $month, $day, $tz);
  147. Carbon::createFromTime($hour, $minute, $second, $tz);
  148. Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
  149. ```
  150. `createFromDate()` will default the time to now. `createFromTime()` will default the date to today. `create()` will default any null parameter to the current respective value. As before, the `$tz` defaults to the current timezone and otherwise can be a DateTimeZone instance or simply a string timezone value. The only special case for default values (mimicking the underlying PHP library) occurs when an hour value is specified but no minutes or seconds, they will get defaulted to 0.
  151. ```php
  152. $xmasThisYear = Carbon::createFromDate(null, 12, 25); // Year defaults to current year
  153. $Y2K = Carbon::create(2000, 1, 1, 0, 0, 0);
  154. $alsoY2K = Carbon::create(1999, 12, 31, 24);
  155. $noonLondonTz = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  156. // A two digit minute could not be found
  157. try { Carbon::create(1975, 5, 21, 22, -2, 0); } catch(InvalidArgumentException $x) { echo $x->getMessage(); }
  158. ```
  159. ```php
  160. Carbon::createFromFormat($format, $time, $tz);
  161. ```
  162. `createFromFormat()` is mostly a wrapper for the base php function [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php). The difference being again the `$tz` argument can be a DateTimeZone instance or a string timezone value. Also, if there are errors with the format this function will call the `DateTime::getLastErrors()` method and then throw a `InvalidArgumentException` with the errors as the message. If you look at the source for the `createXX()` functions above, they all make a call to `createFromFormat()`.
  163. ```php
  164. echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00
  165. ```
  166. The final two create functions are for working with [unix timestamps](http://en.wikipedia.org/wiki/Unix_time). The first will create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone. The second, `createFromTimestampUTC()`, is different in that the timezone will remain UTC (GMT). The second acts the same as `Carbon::createFromFormat('@'.$timestamp)` but I have just made it a little more explicit. Negative timestamps are also allowed.
  167. ```php
  168. echo Carbon::createFromTimeStamp(-1)->toDateTimeString(); // 1969-12-31 18:59:59
  169. echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString(); // 1970-01-01 00:59:59
  170. echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString(); // 1969-12-31 23:59:59
  171. ```
  172. You can also create a `copy()` of an existing Carbon instance. As expected the date, time and timezone values are all copied to the new instance.
  173. ```php
  174. $dt = Carbon::now();
  175. echo $dt->diffInYears($dt->copy()->addYear()); // 1
  176. // $dt was unchanged and still holds the value of Carbon:now()
  177. ```
  178. Finally, if you find yourself inheriting a `\DateTime` instance from another library, fear not! You can create a `Carbon` instance via a friendly `instance()` function.
  179. ```php
  180. $dt = new \DateTime('first day of January 2008'); // <== instance from another API
  181. $carbon = Carbon::instance($dt);
  182. echo get_class($carbon); // 'Carbon\Carbon'
  183. echo $carbon->toDateTimeString(); // 2008-01-01 00:00:00
  184. ```
  185. A quick note about microseconds. The PHP DateTime object allows you to set a microsecond value but ignores it for all of its date math. As of 1.12.0 Carbon now supports microseconds during instantiation or copy operations as well as by default with the `format()` method.
  186. ```php
  187. $dt = Carbon::parse('1975-05-21 22:23:00.123456');
  188. echo $dt->micro; // 123456
  189. echo $dt->copy()->micro; // 123456
  190. ```
  191. Ever need to loop through some dates to find the earliest or latest date? Didn't know what to set your initial maximum/minimum values to? There are now two helpers for this to make your decision simple:
  192. ```php
  193. echo Carbon::maxValue(); // '2038-01-18 22:14:07'
  194. echo Carbon::minValue(); // '1901-12-13 15:45:52'
  195. ```
  196. <a name="api-testing"/>
  197. ### Testing Aids
  198. The testing methods allow you to set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions:
  199. - A call to the static now() method, ex. Carbon::now()
  200. - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null)
  201. - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now')
  202. ```php
  203. $knownDate = Carbon::create(2001, 5, 21, 12); // create testing date
  204. Carbon::setTestNow($knownDate); // set the mock (of course this could be a real mock object)
  205. echo Carbon::now(); // 2001-05-21 12:00:00
  206. echo new Carbon(); // 2001-05-21 12:00:00
  207. echo Carbon::parse(); // 2001-05-21 12:00:00
  208. echo new Carbon('now'); // 2001-05-21 12:00:00
  209. echo Carbon::parse('now'); // 2001-05-21 12:00:00
  210. var_dump(Carbon::hasTestNow()); // bool(true)
  211. Carbon::setTestNow(); // clear the mock
  212. var_dump(Carbon::hasTestNow()); // bool(false)
  213. echo Carbon::now(); // 2015-02-06 00:04:01
  214. ```
  215. A more meaning full example:
  216. ```php
  217. class SeasonalProduct
  218. {
  219. protected $price;
  220. public function __construct($price)
  221. {
  222. $this->price = $price;
  223. }
  224. public function getPrice() {
  225. $multiplier = 1;
  226. if (Carbon::now()->month == 12) {
  227. $multiplier = 2;
  228. }
  229. return $this->price * $multiplier;
  230. }
  231. }
  232. $product = new SeasonalProduct(100);
  233. Carbon::setTestNow(Carbon::parse('first day of March 2000'));
  234. echo $product->getPrice(); // 100
  235. Carbon::setTestNow(Carbon::parse('first day of December 2000'));
  236. echo $product->getPrice(); // 200
  237. Carbon::setTestNow(Carbon::parse('first day of May 2000'));
  238. echo $product->getPrice(); // 100
  239. Carbon::setTestNow();
  240. ```
  241. Relative phrases are also mocked according to the given "now" instance.
  242. ```php
  243. $knownDate = Carbon::create(2001, 5, 21, 12); // create testing date
  244. Carbon::setTestNow($knownDate); // set the mock
  245. echo new Carbon('tomorrow'); // 2001-05-22 00:00:00
  246. echo new Carbon('yesterday'); // 2001-05-20 00:00:00
  247. echo new Carbon('next wednesday'); // 2001-05-23 00:00:00
  248. echo new Carbon('last friday'); // 2001-05-18 00:00:00
  249. echo new Carbon('this thursday'); // 2001-05-24 00:00:00
  250. Carbon::setTestNow(); // always clear it !
  251. ```
  252. The list of words that are considered to be relative modifiers are:
  253. - this
  254. - next
  255. - last
  256. - tomorrow
  257. - yesterday
  258. - +
  259. - -
  260. - first
  261. - last
  262. - ago
  263. Be aware that similar to the next(), previous() and modify() methods some of these relative modifiers will set the time to 00:00:00.
  264. <a name="api-getters"/>
  265. ### Getters
  266. The getters are implemented via PHP's `__get()` method. This enables you to access the value as if it was a property rather than a function call.
  267. ```php
  268. $dt = Carbon::parse('2012-9-5 23:26:11.123789');
  269. // These getters specifically return integers, ie intval()
  270. var_dump($dt->year); // int(2012)
  271. var_dump($dt->month); // int(9)
  272. var_dump($dt->day); // int(5)
  273. var_dump($dt->hour); // int(23)
  274. var_dump($dt->minute); // int(26)
  275. var_dump($dt->second); // int(11)
  276. var_dump($dt->micro); // int(123789)
  277. var_dump($dt->dayOfWeek); // int(3)
  278. var_dump($dt->dayOfYear); // int(248)
  279. var_dump($dt->weekOfMonth); // int(1)
  280. var_dump($dt->weekOfYear); // int(36)
  281. var_dump($dt->daysInMonth); // int(30)
  282. var_dump($dt->timestamp); // int(1346901971)
  283. var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(39) calculated vs now in the same tz
  284. var_dump($dt->quarter); // int(3)
  285. // Returns an int of seconds difference from UTC (+/- sign included)
  286. var_dump(Carbon::createFromTimestampUTC(0)->offset); // int(0)
  287. var_dump(Carbon::createFromTimestamp(0)->offset); // int(-18000)
  288. // Returns an int of hours difference from UTC (+/- sign included)
  289. var_dump(Carbon::createFromTimestamp(0)->offsetHours); // int(-5)
  290. // Indicates if day light savings time is on
  291. var_dump(Carbon::createFromDate(2012, 1, 1)->dst); // bool(false)
  292. var_dump(Carbon::createFromDate(2012, 9, 1)->dst); // bool(true)
  293. // Indicates if the instance is in the same timezone as the local timezone
  294. var_dump(Carbon::now()->local); // bool(true)
  295. var_dump(Carbon::now('America/Vancouver')->local); // bool(false)
  296. // Indicates if the instance is in the UTC timezone
  297. var_dump(Carbon::now()->utc); // bool(false)
  298. var_dump(Carbon::now('Europe/London')->utc); // bool(true)
  299. var_dump(Carbon::createFromTimestampUTC(0)->utc); // bool(true)
  300. // Gets the DateTimeZone instance
  301. echo get_class(Carbon::now()->timezone); // DateTimeZone
  302. echo get_class(Carbon::now()->tz); // DateTimeZone
  303. // Gets the DateTimeZone instance name, shortcut for ->timezone->getName()
  304. echo Carbon::now()->timezoneName; // America/Toronto
  305. echo Carbon::now()->tzName; // America/Toronto
  306. ```
  307. <a name="api-setters"/>
  308. ### Setters
  309. The following setters are implemented via PHP's `__set()` method. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC.
  310. ```php
  311. $dt = Carbon::now();
  312. $dt->year = 1975;
  313. $dt->month = 13; // would force year++ and month = 1
  314. $dt->month = 5;
  315. $dt->day = 21;
  316. $dt->hour = 22;
  317. $dt->minute = 32;
  318. $dt->second = 5;
  319. $dt->timestamp = 169957925; // This will not change the timezone
  320. // Set the timezone via DateTimeZone instance or string
  321. $dt->timezone = new DateTimeZone('Europe/London');
  322. $dt->timezone = 'Europe/London';
  323. $dt->tz = 'Europe/London';
  324. ```
  325. <a name="api-settersfluent"/>
  326. ### Fluent Setters
  327. No arguments are optional for the setters, but there are enough variety in the function definitions that you shouldn't need them anyway. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC.
  328. ```php
  329. $dt = Carbon::now();
  330. $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString();
  331. $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString();
  332. $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString();
  333. $dt->timestamp(169957925)->timezone('Europe/London');
  334. $dt->tz('America/Toronto')->setTimezone('America/Vancouver');
  335. ```
  336. <a name="api-isset"/>
  337. ### IsSet
  338. The PHP function `__isset()` is implemented. This was done as some external systems (ex. [Twig](http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties)) validate the existence of a property before using it. This is done using the `isset()` or `empty()` method. You can read more about these on the PHP site: [__isset()](http://www.php.net/manual/en/language.oop5.overloading.php#object.isset), [isset()](http://www.php.net/manual/en/function.isset.php), [empty()](http://www.php.net/manual/en/function.empty.php).
  339. ```php
  340. var_dump(isset(Carbon::now()->iDoNotExist)); // bool(false)
  341. var_dump(isset(Carbon::now()->hour)); // bool(true)
  342. var_dump(empty(Carbon::now()->iDoNotExist)); // bool(true)
  343. var_dump(empty(Carbon::now()->year)); // bool(false)
  344. ```
  345. <a name="api-formatting"/>
  346. ### String Formatting and Localization
  347. All of the available `toXXXString()` methods rely on the base class method [DateTime::format()](http://php.net/manual/en/datetime.format.php). You'll notice the `__toString()` method is defined which allows a Carbon instance to be printed as a pretty date time string when used in a string context.
  348. ```php
  349. $dt = Carbon::create(1975, 12, 25, 14, 15, 16);
  350. var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString()
  351. echo $dt->toDateString(); // 1975-12-25
  352. echo $dt->toFormattedDateString(); // Dec 25, 1975
  353. echo $dt->toTimeString(); // 14:15:16
  354. echo $dt->toDateTimeString(); // 1975-12-25 14:15:16
  355. echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
  356. // ... of course format() is still available
  357. echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM
  358. ```
  359. You can also set the default __toString() format (which defaults to `Y-m-d H:i:s`) thats used when [type juggling](http://php.net/manual/en/language.types.type-juggling.php) occurs.
  360. ```php
  361. Carbon::setToStringFormat('jS \o\f F, Y g:i:s a');
  362. echo $dt; // 25th of December, 1975 2:15:16 pm
  363. Carbon::resetToStringFormat();
  364. echo $dt; // 1975-12-25 14:15:16
  365. ```
  366. Unfortunately the base class DateTime does not have any localization support. To begin localization support a `formatLocalized($format)` method has been added. The implementation makes a call to [strftime](http://www.php.net/strftime) using the current instance timestamp. If you first set the current locale with [setlocale()](http://www.php.net/setlocale) then the string returned will be formatted in the correct locale.
  367. ```php
  368. setlocale(LC_TIME, 'German');
  369. echo $dt->formatLocalized('%A %d %B %Y'); // Donnerstag 25 Dezember 1975
  370. setlocale(LC_TIME, '');
  371. echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975
  372. ```
  373. <a name="api-commonformats"/>
  374. ## Common Formats
  375. The following are wrappers for the common formats provided in the [DateTime class](http://www.php.net/manual/en/class.datetime.php).
  376. ```php
  377. $dt = Carbon::now();
  378. // $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
  379. echo $dt->toAtomString(); // 1975-12-25T14:15:16-05:00
  380. echo $dt->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
  381. echo $dt->toIso8601String(); // 1975-12-25T14:15:16-0500
  382. echo $dt->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
  383. echo $dt->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
  384. echo $dt->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
  385. echo $dt->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
  386. echo $dt->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
  387. echo $dt->toRfc3339String(); // 1975-12-25T14:15:16-05:00
  388. echo $dt->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
  389. echo $dt->toW3cString(); // 1975-12-25T14:15:16-05:00
  390. ```
  391. <a name="api-comparison"/>
  392. ### Comparison
  393. Simple comparison is offered up via the following functions. Remember that the comparison is done in the UTC timezone so things aren't always as they seem.
  394. ```php
  395. echo Carbon::now()->tzName; // America/Toronto
  396. $first = Carbon::create(2012, 9, 5, 23, 26, 11);
  397. $second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');
  398. echo $first->toDateTimeString(); // 2012-09-05 23:26:11
  399. echo $first->tzName; // America/Toronto
  400. echo $second->toDateTimeString(); // 2012-09-05 20:26:11
  401. echo $second->tzName; // America/Vancouver
  402. var_dump($first->eq($second)); // bool(true)
  403. var_dump($first->ne($second)); // bool(false)
  404. var_dump($first->gt($second)); // bool(false)
  405. var_dump($first->gte($second)); // bool(true)
  406. var_dump($first->lt($second)); // bool(false)
  407. var_dump($first->lte($second)); // bool(true)
  408. $first->setDateTime(2012, 1, 1, 0, 0, 0);
  409. $second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver'
  410. var_dump($first->eq($second)); // bool(false)
  411. var_dump($first->ne($second)); // bool(true)
  412. var_dump($first->gt($second)); // bool(false)
  413. var_dump($first->gte($second)); // bool(false)
  414. var_dump($first->lt($second)); // bool(true)
  415. var_dump($first->lte($second)); // bool(true)
  416. ```
  417. To determine if the current instance is between two other instances you can use the aptly named `between()` method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries.
  418. ```php
  419. $first = Carbon::create(2012, 9, 5, 1);
  420. $second = Carbon::create(2012, 9, 5, 5);
  421. var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
  422. var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
  423. var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false)
  424. ```
  425. Woah! Did you forget min() and max() ? Nope. That is covered as well by the suitably named `min()` and `max()` methods. As usual the default parameter is now if null is specified.
  426. ```php
  427. $dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
  428. $dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
  429. echo $dt1->min($dt2); // 2012-01-01 00:00:00
  430. $dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
  431. $dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
  432. echo $dt1->max($dt2); // 2014-01-30 00:00:00
  433. // now is the default param
  434. $dt1 = Carbon::create(2000, 1, 1, 0, 0, 0);
  435. echo $dt1->max(); // 2015-02-06 00:04:01
  436. ```
  437. To handle the most used cases there are some simple helper functions that hopefully are obvious from their names. For the methods that compare to `now()` (ex. isToday()) in some manner the `now()` is created in the same timezone as the instance.
  438. ```php
  439. $dt = Carbon::now();
  440. $dt->isWeekday();
  441. $dt->isWeekend();
  442. $dt->isYesterday();
  443. $dt->isToday();
  444. $dt->isTomorrow();
  445. $dt->isFuture();
  446. $dt->isPast();
  447. $dt->isLeapYear();
  448. $dt->isSameDay(Carbon::now());
  449. $born = Carbon::createFromDate(1987, 4, 23);
  450. $noCake = Carbon::createFromDate(2014, 9, 26);
  451. $yesCake = Carbon::createFromDate(2014, 4, 23);
  452. var_dump($born->isBirthday($noCake)); // bool(false)
  453. var_dump($born->isBirthday($yesCake)); // bool(true)
  454. ```
  455. <a name="api-addsub"/>
  456. ### Addition and Subtraction
  457. The default DateTime provides a couple of different methods for easily adding and subtracting time. There is `modify()`, `add()` and `sub()`. `modify()` takes a *magical* date/time format string, 'last day of next month', that it parses and applies the modification while `add()` and `sub()` use a `DateInterval` class thats not so obvious, `new \DateInterval('P6YT5M')`. Hopefully using these fluent functions will be more clear and easier to read after not seeing your code for a few weeks. But of course I don't make you choose since the base class functions are still available.
  458. ```php
  459. $dt = Carbon::create(2012, 1, 31, 0);
  460. echo $dt->toDateTimeString(); // 2012-01-31 00:00:00
  461. echo $dt->addYears(5); // 2017-01-31 00:00:00
  462. echo $dt->addYear(); // 2018-01-31 00:00:00
  463. echo $dt->subYear(); // 2017-01-31 00:00:00
  464. echo $dt->subYears(5); // 2012-01-31 00:00:00
  465. echo $dt->addMonths(60); // 2017-01-31 00:00:00
  466. echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps
  467. echo $dt->subMonth(); // 2017-02-03 00:00:00
  468. echo $dt->subMonths(60); // 2012-02-03 00:00:00
  469. echo $dt->addDays(29); // 2012-03-03 00:00:00
  470. echo $dt->addDay(); // 2012-03-04 00:00:00
  471. echo $dt->subDay(); // 2012-03-03 00:00:00
  472. echo $dt->subDays(29); // 2012-02-03 00:00:00
  473. echo $dt->addWeekdays(4); // 2012-02-09 00:00:00
  474. echo $dt->addWeekday(); // 2012-02-10 00:00:00
  475. echo $dt->subWeekday(); // 2012-02-09 00:00:00
  476. echo $dt->subWeekdays(4); // 2012-02-03 00:00:00
  477. echo $dt->addWeeks(3); // 2012-02-24 00:00:00
  478. echo $dt->addWeek(); // 2012-03-02 00:00:00
  479. echo $dt->subWeek(); // 2012-02-24 00:00:00
  480. echo $dt->subWeeks(3); // 2012-02-03 00:00:00
  481. echo $dt->addHours(24); // 2012-02-04 00:00:00
  482. echo $dt->addHour(); // 2012-02-04 01:00:00
  483. echo $dt->subHour(); // 2012-02-04 00:00:00
  484. echo $dt->subHours(24); // 2012-02-03 00:00:00
  485. echo $dt->addMinutes(61); // 2012-02-03 01:01:00
  486. echo $dt->addMinute(); // 2012-02-03 01:02:00
  487. echo $dt->subMinute(); // 2012-02-03 01:01:00
  488. echo $dt->subMinutes(61); // 2012-02-03 00:00:00
  489. echo $dt->addSeconds(61); // 2012-02-03 00:01:01
  490. echo $dt->addSecond(); // 2012-02-03 00:01:02
  491. echo $dt->subSecond(); // 2012-02-03 00:01:01
  492. echo $dt->subSeconds(61); // 2012-02-03 00:00:00
  493. ```
  494. For fun you can also pass negative values to `addXXX()`, in fact that's how `subXXX()` is implemented.
  495. <a name="api-difference"/>
  496. ### Difference
  497. These functions always return the **total difference** expressed in the specified time requested. This differs from the base class `diff()` function where an interval of 61 seconds would be returned as 1 minute and 1 second via a `DateInterval` instance. The `diffInMinutes()` function would simply return 1. All values are truncated and not rounded. Each function below has a default first parameter which is the Carbon instance to compare to, or null if you want to use `now()`. The 2nd parameter again is optional and indicates if you want the return value to be the absolute value or a relative value that might have a `-` (negative) sign if the passed in date is less than the current instance. This will default to true, return the absolute value. The comparisons are done in UTC.
  498. ```php
  499. // Carbon::diffInYears(Carbon $dt = null, $abs = true)
  500. echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0
  501. $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  502. $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
  503. echo $dtOttawa->diffInHours($dtVancouver); // 3
  504. echo $dtOttawa->diffInHours($dtVancouver, false); // 3
  505. echo $dtVancouver->diffInHours($dtOttawa, false); // -3
  506. $dt = Carbon::create(2012, 1, 31, 0);
  507. echo $dt->diffInDays($dt->copy()->addMonth()); // 31
  508. echo $dt->diffInDays($dt->copy()->subMonth(), false); // -31
  509. $dt = Carbon::create(2012, 4, 30, 0);
  510. echo $dt->diffInDays($dt->copy()->addMonth()); // 30
  511. echo $dt->diffInDays($dt->copy()->addWeek()); // 7
  512. $dt = Carbon::create(2012, 1, 1, 0);
  513. echo $dt->diffInMinutes($dt->copy()->addSeconds(59)); // 0
  514. echo $dt->diffInMinutes($dt->copy()->addSeconds(60)); // 1
  515. echo $dt->diffInMinutes($dt->copy()->addSeconds(119)); // 1
  516. echo $dt->diffInMinutes($dt->copy()->addSeconds(120)); // 2
  517. echo $dt->addSeconds(120)->secondsSinceMidnight(); // 120
  518. ```
  519. There is also a special `diffInDaysFiltered()` method to help you filter the difference by days. For example to count the weekend days between two instances:
  520. ```php
  521. $dt = Carbon::create(2014, 1, 1);
  522. $dt2 = Carbon::create(2014, 12, 31);
  523. $daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) {
  524. return $date->isWeekend();
  525. }, $dt2);
  526. echo $daysForExtraCoding; // 104
  527. // others that are defined
  528. // diffInYears(), diffInMonths(), diffInWeeks()
  529. // diffInDays(), diffInWeekdays(), diffInWeekendDays()
  530. // diffInHours(), diffInMinutes(), diffInSeconds()
  531. // secondsSinceMidnight(), secondsUntilEndOfDay()
  532. ```
  533. <a name="api-humandiff"/>
  534. ### Difference for Humans
  535. It is easier for humans to read `1 month ago` compared to 30 days ago. This is a common function seen in most date libraries so I thought I would add it here as well. It uses approximations for a month being 4 weeks. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to `now()` if not specified.
  536. This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities:
  537. * When comparing a value in the past to default now:
  538. * 1 hour ago
  539. * 5 months ago
  540. * When comparing a value in the future to default now:
  541. * 1 hour from now
  542. * 5 months from now
  543. * When comparing a value in the past to another value:
  544. * 1 hour before
  545. * 5 months before
  546. * When comparing a value in the future to another value:
  547. * 1 hour after
  548. * 5 months after
  549. You may also pass `true` as a 2nd parameter to remove the modifiers *ago*, *from now*, etc : `diffForHumans(Carbon $other, true)`
  550. ```php
  551. // The most typical usage is for comments
  552. // The instance is the date the comment was created and its being compared to default now()
  553. echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago
  554. echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after
  555. $dt = Carbon::createFromDate(2011, 8, 1);
  556. echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before
  557. echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
  558. echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 seconds from now
  559. echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago
  560. echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 weeks
  561. ```
  562. <a name="api-modifiers"/>
  563. ### Modifiers
  564. These group of methods perform helpful modifications to the current instance. Most of them are self explanatory from their names... or at least should be. You'll also notice that the startOfXXX(), next() and previous() methods set the time to 00:00:00 and the endOfXXX() methods set the time to 23:59:59.
  565. The only one slightly different is the `average()` function. It moves your instance to the middle date between itself and the provided Carbon argument.
  566. ```php
  567. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  568. echo $dt->startOfDay(); // 2012-01-31 00:00:00
  569. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  570. echo $dt->endOfDay(); // 2012-01-31 23:59:59
  571. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  572. echo $dt->startOfMonth(); // 2012-01-01 00:00:00
  573. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  574. echo $dt->endOfMonth(); // 2012-01-31 23:59:59
  575. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  576. echo $dt->startOfYear(); // 2012-01-01 00:00:00
  577. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  578. echo $dt->endOfYear(); // 2012-12-31 23:59:59
  579. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  580. echo $dt->startOfDecade(); // 2010-01-01 00:00:00
  581. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  582. echo $dt->endOfDecade(); // 2019-12-31 23:59:59
  583. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  584. echo $dt->startOfCentury(); // 2000-01-01 00:00:00
  585. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  586. echo $dt->endOfCentury(); // 2099-12-31 23:59:59
  587. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  588. echo $dt->startOfWeek(); // 2012-01-30 00:00:00
  589. var_dump($dt->dayOfWeek == Carbon::MONDAY); // bool(true) : ISO8601 week starts on Monday
  590. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  591. echo $dt->endOfWeek(); // 2012-02-05 23:59:59
  592. var_dump($dt->dayOfWeek == Carbon::SUNDAY); // bool(true) : ISO8601 week ends on Sunday
  593. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  594. echo $dt->next(Carbon::WEDNESDAY); // 2012-02-01 00:00:00
  595. var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true)
  596. $dt = Carbon::create(2012, 1, 1, 12, 0, 0);
  597. echo $dt->next(); // 2012-01-08 00:00:00
  598. $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
  599. echo $dt->previous(Carbon::WEDNESDAY); // 2012-01-25 00:00:00
  600. var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true)
  601. $dt = Carbon::create(2012, 1, 1, 12, 0, 0);
  602. echo $dt->previous(); // 2011-12-25 00:00:00
  603. $start = Carbon::create(2014, 1, 1, 0, 0, 0);
  604. $end = Carbon::create(2014, 1, 30, 0, 0, 0);
  605. echo $start->average($end); // 2014-01-15 12:00:00
  606. // others that are defined that are similar
  607. // firstOfMonth(), lastOfMonth(), nthOfMonth()
  608. // firstOfQuarter(), lastOfQuarter(), nthOfQuarter()
  609. // firstOfYear(), lastOfYear(), nthOfYear()
  610. ```
  611. <a name="api-constants"/>
  612. ### Constants
  613. The following constants are defined in the Carbon class.
  614. // These getters specifically return integers, ie intval()
  615. var_dump(Carbon::SUNDAY); // int(0)
  616. var_dump(Carbon::MONDAY); // int(1)
  617. var_dump(Carbon::TUESDAY); // int(2)
  618. var_dump(Carbon::WEDNESDAY); // int(3)
  619. var_dump(Carbon::THURSDAY); // int(4)
  620. var_dump(Carbon::FRIDAY); // int(5)
  621. var_dump(Carbon::SATURDAY); // int(6)
  622. var_dump(Carbon::YEARS_PER_CENTURY); // int(100)
  623. var_dump(Carbon::YEARS_PER_DECADE); // int(10)
  624. var_dump(Carbon::MONTHS_PER_YEAR); // int(12)
  625. var_dump(Carbon::WEEKS_PER_YEAR); // int(52)
  626. var_dump(Carbon::DAYS_PER_WEEK); // int(7)
  627. var_dump(Carbon::HOURS_PER_DAY); // int(24)
  628. var_dump(Carbon::MINUTES_PER_HOUR); // int(60)
  629. var_dump(Carbon::SECONDS_PER_MINUTE); // int(60)
  630. ```php
  631. $dt = Carbon::createFromDate(2012, 10, 6);
  632. if ($dt->dayOfWeek === Carbon::SATURDAY) {
  633. echo 'Place bets on Ottawa Senators Winning!';
  634. }
  635. ```
  636. <a name="about"/>
  637. ## About
  638. <a name="about-contributing"/>
  639. ### Contributing
  640. I hate reading a readme.md file that has code errors and/or sample output that is incorrect. I tried something new with this project and wrote a quick readme parser that can **lint** sample source code or **execute** and inject the actual result into a generated readme.
  641. > **Don't make changes to the `readme.md` directly!!**
  642. Change the `readme.src.md` and then use the `readme.php` to generate the new `readme.md` file. It can be run at the command line using `php readme.php` from the project root. Maybe someday I'll extract this out to another project or at least run it with a post receive hook, but for now its just a local tool, deal with it.
  643. The commands are quickly explained below. To see some examples you can view the raw `readme.src.md` file in this repo.
  644. `{{::lint()}}`
  645. The `lint` command is meant for confirming the code is valid and will `eval()` the code passed into the function. Assuming there were no errors, the executed source code will then be injected back into the text replacing out the `{{::lint()}}`. When you look at the raw `readme.src.md` you will see that the code can span several lines. Remember the code is executed in the context of the running script so any variables will be available for the rest of the file.
  646. {{::lint($var = 'brian nesbitt';)}} => $var = 'brian nesbitt';
  647. > As mentioned the `$var` can later be echo'd and you would get 'brian nesbitt' as all of the source is executed in the same scope.
  648. `{{varName::exec()}}` and `{{varName_eval}}`
  649. The `exec` command begins by performing an `eval()` on the code passed into the function. The executed source code will then be injected back into the text replacing out the `{{varName::exec()}}`. This will also create a variable named `varName_eval` that you can then place anywhere in the file and it will get replaced with the output of the `eval()`. You can use any type of output (`echo`, `printf`, `var_dump` etc) statement to return the result value as an output buffer is setup to capture the output.
  650. {{exVarName::exec(echo $var;)}} => echo $var;
  651. {{exVarName_eval}} => brian nesbitt // $var is still set from above
  652. `/*pad()*/`
  653. The `pad()` is a special source modifier. This will pad the code block to the indicated number of characters using spaces. Its particularly handy for aligning `//` comments when showing results.
  654. {{exVarName1::exec(echo 12345;/*pad(20)*/)}} // {{exVarName1_eval}}
  655. {{exVarName2::exec(echo 6;/*pad(20)*/)}} // {{exVarName2_eval}}
  656. ... would generate to:
  657. echo 12345; // 12345
  658. echo 6; // 6
  659. Apart from the readme the typical steps can be used to contribute your own improvements.
  660. * Fork
  661. * Clone
  662. * PHPUnit
  663. * Branch
  664. * PHPUnit
  665. * Code
  666. * PHPUnit
  667. * Commit
  668. * Push
  669. * Pull request
  670. * Relax and play Castle Crashers
  671. <a name="about-author"/>
  672. ### Author
  673. Brian Nesbitt - <brian@nesbot.com> - <http://twitter.com/NesbittBrian>
  674. <a name="about-license"/>
  675. ### License
  676. Carbon is licensed under the MIT License - see the `LICENSE` file for details
  677. <a name="about-history"/>
  678. ### History
  679. You can view the history of the Carbon project in the [history file](https://github.com/briannesbitt/Carbon/blob/master/history.md).
  680. <a name="about-whyname"/>
  681. ### Why the name Carbon?
  682. Read about [Carbon Dating](http://en.wikipedia.org/wiki/Radiocarbon_dating)
  683. ![](https://cruel-carlota.pagodabox.com/55ce479cc1edc5e0cc5b4b6f9a7a9200)