PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/readme.src.md

https://github.com/michaelcaplan/Carbon
Markdown | 663 lines | 487 code | 176 blank | 0 comment | 0 complexity | b3f80d5da0fae94b0b32c5cdd1851b23 MD5 | raw file
  1. > **This file is autogenerated. Please see the [Contributing](#about-contributing) section from more information.**
  2. # Carbon
  3. [![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. {{::lint(
  7. printf("Right now is %s", Carbon::now()->toDateTimeString());
  8. printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString()
  9. $tomorrow = Carbon::now()->addDay();
  10. $lastWeek = Carbon::now()->subWeek();
  11. $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);
  12. $officialDate = Carbon::now()->toRFC2822String();
  13. $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
  14. $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  15. $worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT');
  16. // comparisons are always done in UTC
  17. if (Carbon::now()->gte($worldWillEnd)) {
  18. die();
  19. }
  20. if (Carbon::now()->isWeekend()) {
  21. echo 'Party!';
  22. }
  23. )}}
  24. {{intro::exec(echo Carbon::now()->subMinutes(2)->diffForHumans();)}} // '{{intro_eval}}'
  25. // ... but also does 'from now', 'after' and 'before'
  26. // rolling up to seconds, minutes, hours, days, months, years
  27. {{::lint(
  28. $daysSinceEpoch = Carbon::createFromTimeStamp(0)->diffInDays();
  29. )}}
  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. * [Getters](#api-getters)
  39. * [Setters](#api-setters)
  40. * [Fluent Setters](#api-settersfluent)
  41. * [IsSet](#api-isset)
  42. * [Formatting and Strings](#api-formatting)
  43. * [Common Formats](#api-commonformats)
  44. * [Comparison](#api-comparison)
  45. * [Addition and Subtraction](#api-addsub)
  46. * [Difference](#api-difference)
  47. * [Difference for Humans](#api-humandiff)
  48. * [Constants](#api-constants)
  49. * [About](#about)
  50. * [Contributing](#about-contributing)
  51. * [Author](#about-author)
  52. * [License](#about-license)
  53. * [History](#about-history)
  54. * [Why the name Carbon?](#about-whyname)
  55. <a name="install"/>
  56. ## Installation
  57. <a name="requirements"/>
  58. ### Requirements
  59. - Any flavour of PHP 5.3+ should do
  60. - [optional] PHPUnit to execute the test suite
  61. <a name="install-composer"/>
  62. ### With Composer
  63. The easiest way to install Carbon is via [composer](http://getcomposer.org/). Create the following `composer.json` file and run the `php composer.phar install` command to install it.
  64. ```json
  65. {
  66. "require": {
  67. "nesbot/Carbon": "*"
  68. }
  69. }
  70. ```
  71. ```php
  72. <?php
  73. require 'vendor/autoload.php';
  74. use Carbon\Carbon;
  75. {{::lint(printf("Now: %s", Carbon::now());)}}
  76. ```
  77. <a name="install-nocomposer"/>
  78. ### Without Composer
  79. Why are you not using [composer](http://getcomposer.org/)? Download [Carbon.php](https://github.com/briannesbitt/Carbon/blob/master/Carbon/Carbon.php) from the repo and save the file into your project path somewhere.
  80. ```php
  81. <?php
  82. require 'path/to/Carbon.php';
  83. use Carbon\Carbon;
  84. {{::lint(printf("Now: %s", Carbon::now());)}}
  85. ```
  86. <a name="api"/>
  87. ## API
  88. 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.
  89. ```php
  90. <?php
  91. class Carbon extends \DateTime
  92. {
  93. // code here
  94. }
  95. ```
  96. 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.
  97. > **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.**
  98. 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.
  99. ```php
  100. {{::lint($dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');)}}
  101. {{::lint($dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');)}}
  102. {{tz::exec(echo $dtVancouver->diffInHours($dtToronto);)}} // {{tz_eval}}
  103. ```
  104. 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.
  105. <a name="api-instantiation"/>
  106. ### Instantiation
  107. 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.
  108. ```php
  109. {{::lint($carbon = new Carbon();/*pad(40)*/)}} // equivalent to Carbon::now()
  110. {{::lint($carbon = new Carbon('first day of January 2008', 'America/Vancouver');)}}
  111. {{ctorType::exec(echo get_class($carbon);/*pad(40)*/)}} // '{{ctorType_eval}}'
  112. ```
  113. 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.
  114. ```php
  115. {{::lint(
  116. $now = Carbon::now();
  117. $nowInLondonTz = Carbon::now(new DateTimeZone('Europe/London'));
  118. // or just pass the timezone as a string
  119. $nowInLondonTz = Carbon::now('Europe/London');
  120. )}}
  121. ```
  122. 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`.
  123. ```php
  124. {{::lint($now = Carbon::now();)}}
  125. {{now::exec(echo $now;/*pad(40)*/)}} // {{now_eval}}
  126. {{::lint($today = Carbon::today();)}}
  127. {{today::exec(echo $today;/*pad(40)*/)}} // {{today_eval}}
  128. {{::lint($tomorrow = Carbon::tomorrow('Europe/London');)}}
  129. {{tomorrow::exec(echo $tomorrow;/*pad(40)*/)}} // {{tomorrow_eval}}
  130. {{::lint($yesterday = Carbon::yesterday();)}}
  131. {{yesterday::exec(echo $yesterday;/*pad(40)*/)}} // {{yesterday_eval}}
  132. ```
  133. 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.
  134. ```php
  135. Carbon::createFromDate($year, $month, $day, $tz);
  136. Carbon::createFromTime($hour, $minute, $second, $tz);
  137. Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
  138. ```
  139. `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.
  140. ```php
  141. {{::lint(
  142. $xmasThisYear = Carbon::createFromDate(null, 12, 25); // Year defaults to current year
  143. $Y2K = Carbon::create(2000, 1, 1, 0, 0, 0);
  144. $alsoY2K = Carbon::create(1999, 12, 31, 24);
  145. $noonLondonTz = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  146. )}}
  147. // {{createFromDateException_eval}}
  148. {{createFromDateException::exec(try { Carbon::create(1975, 5, 21, 22, -2, 0); } catch(InvalidArgumentException $x) { echo $x->getMessage(); })}}
  149. ```
  150. ```php
  151. Carbon::createFromFormat($format, $time, $tz);
  152. ```
  153. `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()`.
  154. ```php
  155. {{createFromFormat1::exec(echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString();)}} // {{createFromFormat1_eval}}
  156. ```
  157. 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.
  158. ```php
  159. {{createFromTimeStamp1::exec(echo Carbon::createFromTimeStamp(-1)->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStamp1_eval}}
  160. {{createFromTimeStamp2::exec(echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStamp2_eval}}
  161. {{createFromTimeStampUTC::exec(echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStampUTC_eval}}
  162. ```
  163. 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.
  164. ```php
  165. {{::lint($dt = Carbon::now();)}}
  166. {{copy2::exec(echo $dt->diffInYears($dt->copy()->addYear());)}} // {{copy2_eval}}
  167. // $dt was unchanged and still holds the value of Carbon:now()
  168. ```
  169. 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.
  170. ```php
  171. {{::lint($dt = new \DateTime('first day of January 2008');)}} // <== instance from another API
  172. {{::lint($carbon = Carbon::instance($dt);)}}
  173. {{ctorType1::exec(echo get_class($carbon);/*pad(54)*/)}} // '{{ctorType1_eval}}'
  174. {{ctorType2::exec(echo $carbon->toDateTimeString();/*pad(54)*/)}} // '{{ctorType2_eval}}'
  175. ```
  176. <a name="api-getters"/>
  177. ### Getters
  178. 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.
  179. ```php
  180. {{::lint($dt = Carbon::create(2012, 9, 5, 23, 26, 11);)}}
  181. // These getters specifically return integers, ie intval()
  182. {{getyear::exec(var_dump($dt->year);/*pad(54)*/)}} // {{getyear_eval}}
  183. {{getmonth::exec(var_dump($dt->month);/*pad(54)*/)}} // {{getmonth_eval}}
  184. {{getday::exec(var_dump($dt->day);/*pad(54)*/)}} // {{getday_eval}}
  185. {{gethour::exec(var_dump($dt->hour);/*pad(54)*/)}} // {{gethour_eval}}
  186. {{getminute::exec(var_dump($dt->minute);/*pad(54)*/)}} // {{getminute_eval}}
  187. {{getsecond::exec(var_dump($dt->second);/*pad(54)*/)}} // {{getsecond_eval}}
  188. {{getdow::exec(var_dump($dt->dayOfWeek);/*pad(54)*/)}} // {{getdow_eval}}
  189. {{getdoy::exec(var_dump($dt->dayOfYear);/*pad(54)*/)}} // {{getdoy_eval}}
  190. {{getwoy::exec(var_dump($dt->weekOfYear);/*pad(54)*/)}} // {{getwoy_eval}}
  191. {{getdnm::exec(var_dump($dt->daysInMonth);/*pad(54)*/)}} // {{getdnm_eval}}
  192. {{getts::exec(var_dump($dt->timestamp);/*pad(54)*/)}} // {{getts_eval}}
  193. {{getage::exec(var_dump(Carbon::createFromDate(1975, 5, 21)->age);/*pad(54)*/)}} // {{getage_eval}} calculated vs now in the same tz
  194. {{getq::exec(var_dump($dt->quarter);/*pad(54)*/)}} // {{getq_eval}}
  195. // Returns an int of seconds difference from UTC (+/- sign included)
  196. {{get1::exec(var_dump(Carbon::createFromTimestampUTC(0)->offset);/*pad(54)*/)}} // {{get1_eval}}
  197. {{get2::exec(var_dump(Carbon::createFromTimestamp(0)->offset);/*pad(54)*/)}} // {{get2_eval}}
  198. // Returns an int of hours difference from UTC (+/- sign included)
  199. {{get3::exec(var_dump(Carbon::createFromTimestamp(0)->offsetHours);/*pad(54)*/)}} // {{get3_eval}}
  200. // Indicates if day light savings time is on
  201. {{get4::exec(var_dump(Carbon::createFromDate(2012, 1, 1)->dst);/*pad(54)*/)}} // {{get4_eval}}
  202. // Gets the DateTimeZone instance
  203. {{get5::exec(echo get_class(Carbon::now()->timezone);/*pad(54)*/)}} // {{get5_eval}}
  204. {{get6::exec(echo get_class(Carbon::now()->tz);/*pad(54)*/)}} // {{get6_eval}}
  205. // Gets the DateTimeZone instance name, shortcut for ->timezone->getName()
  206. {{get7::exec(echo Carbon::now()->timezoneName;/*pad(54)*/)}} // {{get7_eval}}
  207. {{get8::exec(echo Carbon::now()->tzName;/*pad(54)*/)}} // {{get8_eval}}
  208. ```
  209. <a name="api-setters"/>
  210. ### Setters
  211. 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.
  212. ```php
  213. {{::lint(
  214. $dt = Carbon::now();
  215. $dt->year = 1975;
  216. $dt->month = 13; // would force year++ and month = 1
  217. $dt->month = 5;
  218. $dt->day = 21;
  219. $dt->hour = 22;
  220. $dt->minute = 32;
  221. $dt->second = 5;
  222. $dt->timestamp = 169957925; // This will not change the timezone
  223. // Set the timezone via DateTimeZone instance or string
  224. $dt->timezone = new DateTimeZone('Europe/London');
  225. $dt->timezone = 'Europe/London';
  226. $dt->tz = 'Europe/London';
  227. )}}
  228. ```
  229. <a name="api-settersfluent"/>
  230. ### Fluent Setters
  231. 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.
  232. ```php
  233. {{::lint(
  234. $dt = Carbon::now();
  235. $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString();
  236. $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString();
  237. $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString();
  238. $dt->timestamp(169957925)->timezone('Europe/London');
  239. $dt->tz('America/Toronto')->setTimezone('America/Vancouver');
  240. )}}
  241. ```
  242. <a name="api-isset"/>
  243. ### IsSet
  244. 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).
  245. ```php
  246. {{isset1::exec(var_dump(isset(Carbon::now()->iDoNotExist));/*pad(50)*/)}} // {{isset1_eval}}
  247. {{isset2::exec(var_dump(isset(Carbon::now()->hour));/*pad(50)*/)}} // {{isset2_eval}}
  248. {{isset3::exec(var_dump(empty(Carbon::now()->iDoNotExist));/*pad(50)*/)}} // {{isset3_eval}}
  249. {{isset4::exec(var_dump(empty(Carbon::now()->year));/*pad(50)*/)}} // {{isset4_eval}}
  250. ```
  251. <a name="api-formatting"/>
  252. ### Formatting and Strings
  253. 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.
  254. ```php
  255. {{::lint($dt = Carbon::create(1975, 12, 25, 14, 15, 16);)}}
  256. {{format1::exec(var_dump($dt->toDateTimeString() == $dt);/*pad(50)*/)}} // {{format1_eval}} => uses __toString()
  257. {{format2::exec(echo $dt->toDateString();/*pad(50)*/)}} // {{format2_eval}}
  258. {{format3::exec(echo $dt->toFormattedDateString();/*pad(50)*/)}} // {{format3_eval}}
  259. {{format4::exec(echo $dt->toTimeString();/*pad(50)*/)}} // {{format4_eval}}
  260. {{format5::exec(echo $dt->toDateTimeString();/*pad(50)*/)}} // {{format5_eval}}
  261. {{format6::exec(echo $dt->toDayDateTimeString();/*pad(50)*/)}} // {{format6_eval}}
  262. // ... of course format() is still available
  263. {{format7::exec(echo $dt->format('l jS \\of F Y h:i:s A');/*pad(50)*/)}} // {{format7_eval}}
  264. ```
  265. <a name="api-commonformats"/>
  266. ## Common Formats
  267. The following are wrappers for the common formats provided in the [DateTime class](http://www.php.net/manual/en/class.datetime.php).
  268. ```php
  269. $dt = Carbon::now();
  270. echo $dt->toATOMString(); // same as $dt->format(DateTime::ATOM);
  271. echo $dt->toCOOKIEString();
  272. echo $dt->toISO8601String();
  273. echo $dt->toRFC822String();
  274. echo $dt->toRFC850String();
  275. echo $dt->toRFC1036String();
  276. echo $dt->toRFC1123String();
  277. echo $dt->toRFC2822String();
  278. echo $dt->toRFC3339String();
  279. echo $dt->toRSSString();
  280. echo $dt->toW3CString();
  281. ```
  282. <a name="api-comparison"/>
  283. ### Comparison
  284. 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.
  285. ```php
  286. {{::lint($first = Carbon::create(2012, 9, 5, 23, 26, 11);)}}
  287. {{::lint($second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');)}}
  288. {{compare1::exec(echo $first->toDateTimeString();/*pad(50)*/)}} // {{compare1_eval}}
  289. {{compare2::exec(echo $second->toDateTimeString();/*pad(50)*/)}} // {{compare2_eval}}
  290. {{compare3::exec(var_dump($first->eq($second));/*pad(50)*/)}} // {{compare3_eval}}
  291. {{compare4::exec(var_dump($first->ne($second));/*pad(50)*/)}} // {{compare4_eval}}
  292. {{compare5::exec(var_dump($first->gt($second));/*pad(50)*/)}} // {{compare5_eval}}
  293. {{compare6::exec(var_dump($first->gte($second));/*pad(50)*/)}} // {{compare6_eval}}
  294. {{compare7::exec(var_dump($first->lt($second));/*pad(50)*/)}} // {{compare7_eval}}
  295. {{compare8::exec(var_dump($first->lte($second));/*pad(50)*/)}} // {{compare8_eval}}
  296. {{::lint($first->setDateTime(2012, 1, 1, 0, 0, 0);)}}
  297. {{::lint($second->setDateTime(2012, 1, 1, 0, 0, 0);/*pad(50)*/)}} // Remember tz is 'America/Vancouver'
  298. {{compare9::exec(var_dump($first->eq($second));/*pad(50)*/)}} // {{compare9_eval}}
  299. {{compare10::exec(var_dump($first->ne($second));/*pad(50)*/)}} // {{compare10_eval}}
  300. {{compare11::exec(var_dump($first->gt($second));/*pad(50)*/)}} // {{compare11_eval}}
  301. {{compare12::exec(var_dump($first->gte($second));/*pad(50)*/)}} // {{compare12_eval}}
  302. {{compare13::exec(var_dump($first->lt($second));/*pad(50)*/)}} // {{compare13_eval}}
  303. {{compare14::exec(var_dump($first->lte($second));/*pad(50)*/)}} // {{compare14_eval}}
  304. ```
  305. 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.
  306. ```php
  307. {{::lint(
  308. $dt = Carbon::now();
  309. $dt->isWeekday();
  310. $dt->isWeekend();
  311. $dt->isYesterday();
  312. $dt->isToday();
  313. $dt->isTomorrow();
  314. $dt->isFuture();
  315. $dt->isPast();
  316. $dt->isLeapYear();
  317. )}}
  318. ```
  319. <a name="api-addsub"/>
  320. ### Addition and Subtraction
  321. 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.
  322. ```php
  323. {{::lint($dt = Carbon::create(2012, 1, 31, 0);)}}
  324. {{addsub1::exec(echo $dt->toDateTimeString();/*pad(40)*/)}} // {{addsub1_eval}}
  325. {{addsub2::exec(echo $dt->addYears(5);/*pad(40)*/)}} // {{addsub2_eval}}
  326. {{addsub3::exec(echo $dt->addYear();/*pad(40)*/)}} // {{addsub3_eval}}
  327. {{addsub4::exec(echo $dt->subYear();/*pad(40)*/)}} // {{addsub4_eval}}
  328. {{addsub5::exec(echo $dt->subYears(5);/*pad(40)*/)}} // {{addsub5_eval}}
  329. {{addsub6::exec(echo $dt->addMonths(60);/*pad(40)*/)}} // {{addsub6_eval}}
  330. {{addsub7::exec(echo $dt->addMonth();/*pad(40)*/)}} // {{addsub7_eval}} equivalent of $dt->month($dt->month + 1); so it wraps
  331. {{addsub8::exec(echo $dt->subMonth();/*pad(40)*/)}} // {{addsub8_eval}}
  332. {{addsub9::exec(echo $dt->subMonths(60);/*pad(40)*/)}} // {{addsub9_eval}}
  333. {{addsub10::exec(echo $dt->addDays(29);/*pad(40)*/)}} // {{addsub10_eval}}
  334. {{addsub11::exec(echo $dt->addDay();/*pad(40)*/)}} // {{addsub11_eval}}
  335. {{addsub12::exec(echo $dt->subDay();/*pad(40)*/)}} // {{addsub12_eval}}
  336. {{addsub13::exec(echo $dt->subDays(29);/*pad(40)*/)}} // {{addsub13_eval}}
  337. {{addsub14::exec(echo $dt->addWeekdays(4);/*pad(40)*/)}} // {{addsub14_eval}}
  338. {{addsub15::exec(echo $dt->addWeekday();/*pad(40)*/)}} // {{addsub15_eval}}
  339. {{addsub16::exec(echo $dt->subWeekday();/*pad(40)*/)}} // {{addsub16_eval}}
  340. {{addsub17::exec(echo $dt->subWeekdays(4);/*pad(40)*/)}} // {{addsub17_eval}}
  341. {{addsub18::exec(echo $dt->addWeeks(3);/*pad(40)*/)}} // {{addsub18_eval}}
  342. {{addsub19::exec(echo $dt->addWeek();/*pad(40)*/)}} // {{addsub19_eval}}
  343. {{addsub20::exec(echo $dt->subWeek();/*pad(40)*/)}} // {{addsub20_eval}}
  344. {{addsub21::exec(echo $dt->subWeeks(3);/*pad(40)*/)}} // {{addsub21_eval}}
  345. {{addsub22::exec(echo $dt->addHours(24);/*pad(40)*/)}} // {{addsub22_eval}}
  346. {{addsub23::exec(echo $dt->addHour();/*pad(40)*/)}} // {{addsub23_eval}}
  347. {{addsub24::exec(echo $dt->subHour();/*pad(40)*/)}} // {{addsub24_eval}}
  348. {{addsub25::exec(echo $dt->subHours(24);/*pad(40)*/)}} // {{addsub25_eval}}
  349. {{addsub26::exec(echo $dt->addMinutes(61);/*pad(40)*/)}} // {{addsub26_eval}}
  350. {{addsub27::exec(echo $dt->addMinute();/*pad(40)*/)}} // {{addsub27_eval}}
  351. {{addsub28::exec(echo $dt->subMinute();/*pad(40)*/)}} // {{addsub28_eval}}
  352. {{addsub29::exec(echo $dt->subMinutes(61);/*pad(40)*/)}} // {{addsub29_eval}}
  353. {{addsub30::exec(echo $dt->addSeconds(61);/*pad(40)*/)}} // {{addsub30_eval}}
  354. {{addsub31::exec(echo $dt->addSecond();/*pad(40)*/)}} // {{addsub31_eval}}
  355. {{addsub32::exec(echo $dt->subSecond();/*pad(40)*/)}} // {{addsub32_eval}}
  356. {{addsub33::exec(echo $dt->subSeconds(61);/*pad(40)*/)}} // {{addsub33_eval}}
  357. {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);/*pad(40)*/)}}
  358. {{addsub35::exec(echo $dt->startOfDay();/*pad(40)*/)}} // {{addsub35_eval}}
  359. {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
  360. {{addsub37::exec(echo $dt->endOfDay();/*pad(40)*/)}} // {{addsub37_eval}}
  361. {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
  362. {{addsub39::exec(echo $dt->startOfMonth();/*pad(40)*/)}} // {{addsub39_eval}}
  363. {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
  364. {{addsub41::exec(echo $dt->endOfMonth();/*pad(40)*/)}} // {{addsub41_eval}}
  365. ```
  366. For fun you can also pass negative values to `addXXX()`, in fact that's how `subXXX()` is implemented.
  367. <a name="api-difference"/>
  368. ### Difference
  369. 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.
  370. ```php
  371. // Carbon::diffInYears(Carbon $dt = null, $abs = true)
  372. {{diff1::exec(echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London'));)}} // {{diff1_eval}}
  373. {{::lint($dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');)}}
  374. {{::lint($dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');)}}
  375. {{diff4::exec(echo $dtOttawa->diffInHours($dtVancouver);/*pad(70)*/)}} // {{diff4_eval}}
  376. {{diff5::exec(echo $dtOttawa->diffInHours($dtVancouver, false);/*pad(70)*/)}} // {{diff5_eval}}
  377. {{diff6::exec(echo $dtVancouver->diffInHours($dtOttawa, false);/*pad(70)*/)}} // {{diff6_eval}}
  378. {{::lint($dt = Carbon::create(2012, 1, 31, 0);)}}
  379. {{diff8::exec(echo $dt->diffInDays($dt->copy()->addMonth());/*pad(70)*/)}} // {{diff8_eval}}
  380. {{diff9::exec(echo $dt->diffInDays($dt->copy()->subMonth(), false);/*pad(70)*/)}} // {{diff9_eval}}
  381. {{::lint($dt = Carbon::create(2012, 4, 30, 0);)}}
  382. {{diff11::exec(echo $dt->diffInDays($dt->copy()->addMonth());/*pad(70)*/)}} // {{diff11_eval}}
  383. {{diff12::exec(echo $dt->diffInDays($dt->copy()->addWeek());/*pad(70)*/)}} // {{diff12_eval}}
  384. {{::lint($dt = Carbon::create(2012, 1, 1, 0);)}}
  385. {{diff14::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(59));/*pad(70)*/)}} // {{diff14_eval}}
  386. {{diff15::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(60));/*pad(70)*/)}} // {{diff15_eval}}
  387. {{diff16::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(119));/*pad(70)*/)}} // {{diff16_eval}}
  388. {{diff17::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(120));/*pad(70)*/)}} // {{diff17_eval}}
  389. // others that are defined
  390. // diffInYears(), diffInMonths(), diffInDays()
  391. // diffInHours(), diffInMinutes(), diffInSeconds()
  392. ```
  393. <a name="api-humandiff"/>
  394. ### Difference for Humans
  395. 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 month being 30 days which then equates a year to 360 days. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to `now()` if not specified.
  396. This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities:
  397. * When comparing a value in the past to default now:
  398. * 1 hour ago
  399. * 5 months ago
  400. * When comparing a value in the future to default now:
  401. * 1 hour from now
  402. * 5 months from now
  403. * When comparing a value in the past to another value:
  404. * 1 hour before
  405. * 5 months before
  406. * When comparing a value in the future to another value:
  407. * 1 hour after
  408. * 5 months after
  409. ```php
  410. // The most typical usage is for comments
  411. // The instance is the date the comment was created and its being compared to default now()
  412. {{humandiff1::exec(echo Carbon::now()->subDays(5)->diffForHumans();/*pad(62)*/)}} // {{humandiff1_eval}}
  413. {{humandiff2::exec(echo Carbon::now()->diffForHumans(Carbon::now()->subYear());/*pad(62)*/)}} // {{humandiff2_eval}}
  414. {{::lint($dt = Carbon::createFromDate(2011, 2, 1);)}}
  415. {{humandiff4::exec(echo $dt->diffForHumans($dt->copy()->addMonth());/*pad(62)*/)}} // {{humandiff4_eval}}
  416. {{humandiff5::exec(echo $dt->diffForHumans($dt->copy()->subMonth());/*pad(62)*/)}} // {{humandiff5_eval}}
  417. {{humandiff6::exec(echo Carbon::now()->addSeconds(5)->diffForHumans();/*pad(62)*/)}} // {{humandiff6_eval}}
  418. ```
  419. <a name="api-constants"/>
  420. ### Constants
  421. The following constants are defined in the Carbon class.
  422. * SUNDAY = 0
  423. * MONDAY = 1
  424. * TUESDAY = 2
  425. * WEDNESDAY = 3
  426. * THURSDAY = 4
  427. * FRIDAY = 5
  428. * SATURDAY = 6
  429. * MONTHS_PER_YEAR = 12
  430. * HOURS_PER_DAY = 24
  431. * MINUTES_PER_HOUR = 60
  432. * SECONDS_PER_MINUTE = 60
  433. ```php
  434. {{::lint(
  435. $dt = Carbon::createFromDate(2012, 10, 6);
  436. if ($dt->dayOfWeek === Carbon::SATURDAY) {
  437. echo 'Place bets on Ottawa Senators Winning!';
  438. }
  439. )}}
  440. ```
  441. <a name="about"/>
  442. ## About
  443. <a name="about-contributing"/>
  444. ### Contributing
  445. 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.
  446. > **Don't make changes to the `readme.md` directly!!**
  447. 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.
  448. The commands are quickly explained below. To see some examples you can view the raw `readme.src.md` file in this repo.
  449. `\{\{::lint()}}`
  450. 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.
  451. \{\{::lint($var = 'brian nesbitt';)}} => {{::lint($var = 'brian nesbitt';)}}
  452. > 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.
  453. `\{\{varName::exec()}}` and `{{varName_eval}}`
  454. 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.
  455. \{\{exVarName::exec(echo $var;)}} => {{exVarName::exec(echo $var;)}}
  456. \{\{exVarName_eval}} => {{exVarName_eval}} // $var is still set from above
  457. `/*pad()*/`
  458. 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.
  459. \{\{exVarName1::exec(echo 12345;/*pad(20)*/)}} // \{\{exVarName1_eval}}
  460. \{\{exVarName2::exec(echo 6;/*pad(20)*/)}} // \{\{exVarName2_eval}}
  461. ... would generate to:
  462. {{exVarName1::exec(echo 12345;/*pad(20)*/)}} // {{exVarName1_eval}}
  463. {{exVarName2::exec(echo 6;/*pad(20)*/)}} // {{exVarName2_eval}}
  464. Apart from the readme the typical steps can be used to contribute your own improvements.
  465. * Fork
  466. * Clone
  467. * PHPUnit
  468. * Branch
  469. * PHPUnit
  470. * Code
  471. * PHPUnit
  472. * Commit
  473. * Push
  474. * Pull request
  475. * Relax and play Castle Crashers
  476. <a name="about-author"/>
  477. ### Author
  478. Brian Nesbitt - <brian@nesbot.com> - <http://twitter.com/NesbittBrian>
  479. <a name="about-license"/>
  480. ### License
  481. Carbon is licensed under the MIT License - see the `LICENSE` file for details
  482. <a name="about-history"/>
  483. ### History
  484. You can view the history of the Carbon project in the [history file](https://github.com/briannesbitt/Carbon/blob/master/history.md).
  485. <a name="about-whyname"/>
  486. ### Why the name Carbon?
  487. Read about [Carbon Dating](http://en.wikipedia.org/wiki/Radiocarbon_dating)