No Description

DiffTest.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\Carbon;
  11. class DiffTest extends TestFixture
  12. {
  13. public function testDiffInYearsPositive()
  14. {
  15. $dt = Carbon::createFromDate(2000, 1, 1);
  16. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()));
  17. }
  18. public function testDiffInYearsNegativeWithSign()
  19. {
  20. $dt = Carbon::createFromDate(2000, 1, 1);
  21. $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false));
  22. }
  23. public function testDiffInYearsNegativeNoSign()
  24. {
  25. $dt = Carbon::createFromDate(2000, 1, 1);
  26. $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear()));
  27. }
  28. public function testDiffInYearsVsDefaultNow()
  29. {
  30. $this->assertSame(1, Carbon::now()->subYear()->diffInYears());
  31. }
  32. public function testDiffInYearsEnsureIsTruncated()
  33. {
  34. $dt = Carbon::createFromDate(2000, 1, 1);
  35. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7)));
  36. }
  37. public function testDiffInMonthsPositive()
  38. {
  39. $dt = Carbon::createFromDate(2000, 1, 1);
  40. $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth()));
  41. }
  42. public function testDiffInMonthsNegativeWithSign()
  43. {
  44. $dt = Carbon::createFromDate(2000, 1, 1);
  45. $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false));
  46. }
  47. public function testDiffInMonthsNegativeNoSign()
  48. {
  49. $dt = Carbon::createFromDate(2000, 1, 1);
  50. $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth()));
  51. }
  52. public function testDiffInMonthsVsDefaultNow()
  53. {
  54. $this->assertSame(12, Carbon::now()->subYear()->diffInMonths());
  55. }
  56. public function testDiffInMonthsEnsureIsTruncated()
  57. {
  58. $dt = Carbon::createFromDate(2000, 1, 1);
  59. $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16)));
  60. }
  61. public function testDiffInDaysPositive()
  62. {
  63. $dt = Carbon::createFromDate(2000, 1, 1);
  64. $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear()));
  65. }
  66. public function testDiffInDaysNegativeWithSign()
  67. {
  68. $dt = Carbon::createFromDate(2000, 1, 1);
  69. $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false));
  70. }
  71. public function testDiffInDaysNegativeNoSign()
  72. {
  73. $dt = Carbon::createFromDate(2000, 1, 1);
  74. $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear()));
  75. }
  76. public function testDiffInDaysVsDefaultNow()
  77. {
  78. $this->assertSame(7, Carbon::now()->subWeek()->diffInDays());
  79. }
  80. public function testDiffInDaysEnsureIsTruncated()
  81. {
  82. $dt = Carbon::createFromDate(2000, 1, 1);
  83. $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13)));
  84. }
  85. public function testDiffInDaysFilteredPositiveWithMutated()
  86. {
  87. $dt = Carbon::createFromDate(2000, 1, 1);
  88. $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
  89. return $date->dayOfWeek === 1;
  90. }, $dt->copy()->endOfMonth()));
  91. }
  92. public function testDiffInDaysFilteredPositiveWithSecondObject()
  93. {
  94. $dt1 = Carbon::createFromDate(2000, 1, 1);
  95. $dt2 = Carbon::createFromDate(2000, 1, 31);
  96. $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  97. return $date->dayOfWeek === Carbon::SUNDAY;
  98. }, $dt2));
  99. }
  100. public function testDiffInDaysFilteredNegativeNoSignWithMutated()
  101. {
  102. $dt = Carbon::createFromDate(2000, 1, 31);
  103. $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
  104. return $date->dayOfWeek === Carbon::SUNDAY;
  105. }, $dt->copy()->startOfMonth()));
  106. }
  107. public function testDiffInDaysFilteredNegativeNoSignWithSecondObject()
  108. {
  109. $dt1 = Carbon::createFromDate(2000, 1, 31);
  110. $dt2 = Carbon::createFromDate(2000, 1, 1);
  111. $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  112. return $date->dayOfWeek === Carbon::SUNDAY;
  113. }, $dt2));
  114. }
  115. public function testDiffInDaysFilteredNegativeWithSignWithMutated()
  116. {
  117. $dt = Carbon::createFromDate(2000, 1, 31);
  118. $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) {
  119. return $date->dayOfWeek === 1;
  120. }, $dt->copy()->startOfMonth(), false));
  121. }
  122. public function testDiffInDaysFilteredNegativeWithSignWithSecondObject()
  123. {
  124. $dt1 = Carbon::createFromDate(2000, 1, 31);
  125. $dt2 = Carbon::createFromDate(2000, 1, 1);
  126. $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  127. return $date->dayOfWeek === Carbon::SUNDAY;
  128. }, $dt2, false));
  129. }
  130. public function testBug188DiffWithSameDates()
  131. {
  132. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  133. $end = $start->copy();
  134. $this->assertSame(0, $start->diffInDays($end));
  135. $this->assertSame(0, $start->diffInWeekdays($end));
  136. }
  137. public function testBug188DiffWithDatesOnlyHoursApart()
  138. {
  139. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  140. $end = $start->copy();
  141. $this->assertSame(0, $start->diffInDays($end));
  142. $this->assertSame(0, $start->diffInWeekdays($end));
  143. }
  144. public function testBug188DiffWithSameDates1DayApart()
  145. {
  146. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  147. $end = $start->copy()->addDay();
  148. $this->assertSame(1, $start->diffInDays($end));
  149. $this->assertSame(1, $start->diffInWeekdays($end));
  150. }
  151. public function testBug188DiffWithDatesOnTheWeekend()
  152. {
  153. $start = Carbon::create(2014, 1, 1, 0, 0, 0);
  154. $start->next(Carbon::SATURDAY);
  155. $end = $start->copy()->addDay();
  156. $this->assertSame(1, $start->diffInDays($end));
  157. $this->assertSame(0, $start->diffInWeekdays($end));
  158. }
  159. public function testDiffInWeekdaysPositive()
  160. {
  161. $dt = Carbon::createFromDate(2000, 1, 1);
  162. $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->endOfMonth()));
  163. }
  164. public function testDiffInWeekdaysNegativeNoSign()
  165. {
  166. $dt = Carbon::createFromDate(2000, 1, 31);
  167. $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->startOfMonth()));
  168. }
  169. public function testDiffInWeekdaysNegativeWithSign()
  170. {
  171. $dt = Carbon::createFromDate(2000, 1, 31);
  172. $this->assertSame(-21, $dt->diffInWeekdays($dt->copy()->startOfMonth(), false));
  173. }
  174. public function testDiffInWeekendDaysPositive()
  175. {
  176. $dt = Carbon::createFromDate(2000, 1, 1);
  177. $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->endOfMonth()));
  178. }
  179. public function testDiffInWeekendDaysNegativeNoSign()
  180. {
  181. $dt = Carbon::createFromDate(2000, 1, 31);
  182. $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth()));
  183. }
  184. public function testDiffInWeekendDaysNegativeWithSign()
  185. {
  186. $dt = Carbon::createFromDate(2000, 1, 31);
  187. $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), false));
  188. }
  189. public function testDiffInWeeksPositive()
  190. {
  191. $dt = Carbon::createFromDate(2000, 1, 1);
  192. $this->assertSame(52, $dt->diffInWeeks($dt->copy()->addYear()));
  193. }
  194. public function testDiffInWeeksNegativeWithSign()
  195. {
  196. $dt = Carbon::createFromDate(2000, 1, 1);
  197. $this->assertSame(-52, $dt->diffInWeeks($dt->copy()->subYear(), false));
  198. }
  199. public function testDiffInWeeksNegativeNoSign()
  200. {
  201. $dt = Carbon::createFromDate(2000, 1, 1);
  202. $this->assertSame(52, $dt->diffInWeeks($dt->copy()->subYear()));
  203. }
  204. public function testDiffInWeeksVsDefaultNow()
  205. {
  206. $this->assertSame(1, Carbon::now()->subWeek()->diffInWeeks());
  207. }
  208. public function testDiffInWeeksEnsureIsTruncated()
  209. {
  210. $dt = Carbon::createFromDate(2000, 1, 1);
  211. $this->assertSame(0, $dt->diffInWeeks($dt->copy()->addWeek()->subDay()));
  212. }
  213. public function testDiffInHoursPositive()
  214. {
  215. $dt = Carbon::createFromDate(2000, 1, 1);
  216. $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2)));
  217. }
  218. public function testDiffInHoursNegativeWithSign()
  219. {
  220. $dt = Carbon::createFromDate(2000, 1, 1);
  221. $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false));
  222. }
  223. public function testDiffInHoursNegativeNoSign()
  224. {
  225. $dt = Carbon::createFromDate(2000, 1, 1);
  226. $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2)));
  227. }
  228. public function testDiffInHoursVsDefaultNow()
  229. {
  230. Carbon::setTestNow(Carbon::create(2012, 1, 15));
  231. $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours());
  232. Carbon::setTestNow();
  233. }
  234. public function testDiffInHoursEnsureIsTruncated()
  235. {
  236. $dt = Carbon::createFromDate(2000, 1, 1);
  237. $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31)));
  238. }
  239. public function testDiffInMinutesPositive()
  240. {
  241. $dt = Carbon::createFromDate(2000, 1, 1);
  242. $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2)));
  243. }
  244. public function testDiffInMinutesPositiveAlot()
  245. {
  246. $dt = Carbon::createFromDate(2000, 1, 1);
  247. $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2)));
  248. }
  249. public function testDiffInMinutesNegativeWithSign()
  250. {
  251. $dt = Carbon::createFromDate(2000, 1, 1);
  252. $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false));
  253. }
  254. public function testDiffInMinutesNegativeNoSign()
  255. {
  256. $dt = Carbon::createFromDate(2000, 1, 1);
  257. $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2)));
  258. }
  259. public function testDiffInMinutesVsDefaultNow()
  260. {
  261. $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes());
  262. }
  263. public function testDiffInMinutesEnsureIsTruncated()
  264. {
  265. $dt = Carbon::createFromDate(2000, 1, 1);
  266. $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31)));
  267. }
  268. public function testDiffInSecondsPositive()
  269. {
  270. $dt = Carbon::createFromDate(2000, 1, 1);
  271. $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2)));
  272. }
  273. public function testDiffInSecondsPositiveAlot()
  274. {
  275. $dt = Carbon::createFromDate(2000, 1, 1);
  276. $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2)));
  277. }
  278. public function testDiffInSecondsNegativeWithSign()
  279. {
  280. $dt = Carbon::createFromDate(2000, 1, 1);
  281. $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false));
  282. }
  283. public function testDiffInSecondsNegativeNoSign()
  284. {
  285. $dt = Carbon::createFromDate(2000, 1, 1);
  286. $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2)));
  287. }
  288. public function testDiffInSecondsVsDefaultNow()
  289. {
  290. $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds());
  291. }
  292. public function testDiffInSecondsEnsureIsTruncated()
  293. {
  294. $dt = Carbon::createFromDate(2000, 1, 1);
  295. $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9)));
  296. }
  297. public function testDiffInSecondsWithTimezones()
  298. {
  299. $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  300. $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
  301. $this->assertSame(3 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver));
  302. }
  303. public function testDiffInSecondsWithTimezonesAndVsDefault()
  304. {
  305. $dt = Carbon::now('America/Vancouver');
  306. $this->assertSame(0, $dt->diffInSeconds());
  307. }
  308. public function testDiffForHumansNowAndSecond()
  309. {
  310. $d = Carbon::now();
  311. $this->assertSame('1 second ago', $d->diffForHumans());
  312. }
  313. public function testDiffForHumansNowAndSecondWithTimezone()
  314. {
  315. $d = Carbon::now('America/Vancouver');
  316. $this->assertSame('1 second ago', $d->diffForHumans());
  317. }
  318. public function testDiffForHumansNowAndSeconds()
  319. {
  320. $d = Carbon::now()->subSeconds(2);
  321. $this->assertSame('2 seconds ago', $d->diffForHumans());
  322. }
  323. public function testDiffForHumansNowAndNearlyMinute()
  324. {
  325. $d = Carbon::now()->subSeconds(59);
  326. $this->assertSame('59 seconds ago', $d->diffForHumans());
  327. }
  328. public function testDiffForHumansNowAndMinute()
  329. {
  330. $d = Carbon::now()->subMinute();
  331. $this->assertSame('1 minute ago', $d->diffForHumans());
  332. }
  333. public function testDiffForHumansNowAndMinutes()
  334. {
  335. $d = Carbon::now()->subMinutes(2);
  336. $this->assertSame('2 minutes ago', $d->diffForHumans());
  337. }
  338. public function testDiffForHumansNowAndNearlyHour()
  339. {
  340. $d = Carbon::now()->subMinutes(59);
  341. $this->assertSame('59 minutes ago', $d->diffForHumans());
  342. }
  343. public function testDiffForHumansNowAndHour()
  344. {
  345. $d = Carbon::now()->subHour();
  346. $this->assertSame('1 hour ago', $d->diffForHumans());
  347. }
  348. public function testDiffForHumansNowAndHours()
  349. {
  350. Carbon::setTestNow(Carbon::create(2012, 1, 15));
  351. $d = Carbon::now()->subHours(2);
  352. $this->assertSame('2 hours ago', $d->diffForHumans());
  353. Carbon::setTestNow();
  354. }
  355. public function testDiffForHumansNowAndNearlyDay()
  356. {
  357. Carbon::setTestNow(Carbon::create(2012, 1, 15));
  358. $d = Carbon::now()->subHours(23);
  359. $this->assertSame('23 hours ago', $d->diffForHumans());
  360. Carbon::setTestNow();
  361. }
  362. public function testDiffForHumansNowAndDay()
  363. {
  364. $d = Carbon::now()->subDay();
  365. $this->assertSame('1 day ago', $d->diffForHumans());
  366. }
  367. public function testDiffForHumansNowAndDays()
  368. {
  369. $d = Carbon::now()->subDays(2);
  370. $this->assertSame('2 days ago', $d->diffForHumans());
  371. }
  372. public function testDiffForHumansNowAndNearlyWeek()
  373. {
  374. $d = Carbon::now()->subDays(6);
  375. $this->assertSame('6 days ago', $d->diffForHumans());
  376. }
  377. public function testDiffForHumansNowAndWeek()
  378. {
  379. $d = Carbon::now()->subWeek();
  380. $this->assertSame('1 week ago', $d->diffForHumans());
  381. }
  382. public function testDiffForHumansNowAndWeeks()
  383. {
  384. $d = Carbon::now()->subWeeks(2);
  385. $this->assertSame('2 weeks ago', $d->diffForHumans());
  386. }
  387. public function testDiffForHumansNowAndNearlyMonth()
  388. {
  389. $d = Carbon::now()->subWeeks(3);
  390. $this->assertSame('3 weeks ago', $d->diffForHumans());
  391. }
  392. public function testDiffForHumansNowAndMonth()
  393. {
  394. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  395. $d = Carbon::now()->subWeeks(4);
  396. $this->assertSame('4 weeks ago', $d->diffForHumans());
  397. $d = Carbon::now()->subMonth();
  398. $this->assertSame('1 month ago', $d->diffForHumans());
  399. Carbon::setTestNow();
  400. }
  401. public function testDiffForHumansNowAndMonths()
  402. {
  403. $d = Carbon::now()->subMonths(2);
  404. $this->assertSame('2 months ago', $d->diffForHumans());
  405. }
  406. public function testDiffForHumansNowAndNearlyYear()
  407. {
  408. $d = Carbon::now()->subMonths(11);
  409. $this->assertSame('11 months ago', $d->diffForHumans());
  410. }
  411. public function testDiffForHumansNowAndYear()
  412. {
  413. $d = Carbon::now()->subYear();
  414. $this->assertSame('1 year ago', $d->diffForHumans());
  415. }
  416. public function testDiffForHumansNowAndYears()
  417. {
  418. $d = Carbon::now()->subYears(2);
  419. $this->assertSame('2 years ago', $d->diffForHumans());
  420. }
  421. public function testDiffForHumansNowAndFutureSecond()
  422. {
  423. $d = Carbon::now()->addSecond();
  424. $this->assertSame('1 second from now', $d->diffForHumans());
  425. }
  426. public function testDiffForHumansNowAndFutureSeconds()
  427. {
  428. $d = Carbon::now()->addSeconds(2);
  429. $this->assertSame('2 seconds from now', $d->diffForHumans());
  430. }
  431. public function testDiffForHumansNowAndNearlyFutureMinute()
  432. {
  433. $d = Carbon::now()->addSeconds(59);
  434. $this->assertSame('59 seconds from now', $d->diffForHumans());
  435. }
  436. public function testDiffForHumansNowAndFutureMinute()
  437. {
  438. $d = Carbon::now()->addMinute();
  439. $this->assertSame('1 minute from now', $d->diffForHumans());
  440. }
  441. public function testDiffForHumansNowAndFutureMinutes()
  442. {
  443. $d = Carbon::now()->addMinutes(2);
  444. $this->assertSame('2 minutes from now', $d->diffForHumans());
  445. }
  446. public function testDiffForHumansNowAndNearlyFutureHour()
  447. {
  448. $d = Carbon::now()->addMinutes(59);
  449. $this->assertSame('59 minutes from now', $d->diffForHumans());
  450. }
  451. public function testDiffForHumansNowAndFutureHour()
  452. {
  453. $d = Carbon::now()->addHour();
  454. $this->assertSame('1 hour from now', $d->diffForHumans());
  455. }
  456. public function testDiffForHumansNowAndFutureHours()
  457. {
  458. $d = Carbon::now()->addHours(2);
  459. $this->assertSame('2 hours from now', $d->diffForHumans());
  460. }
  461. public function testDiffForHumansNowAndNearlyFutureDay()
  462. {
  463. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  464. $d = Carbon::now()->addHours(23);
  465. $this->assertSame('23 hours from now', $d->diffForHumans());
  466. Carbon::setTestNow();
  467. }
  468. public function testDiffForHumansNowAndFutureDay()
  469. {
  470. $d = Carbon::now()->addDay();
  471. $this->assertSame('1 day from now', $d->diffForHumans());
  472. }
  473. public function testDiffForHumansNowAndFutureDays()
  474. {
  475. $d = Carbon::now()->addDays(2);
  476. $this->assertSame('2 days from now', $d->diffForHumans());
  477. }
  478. public function testDiffForHumansNowAndNearlyFutureWeek()
  479. {
  480. $d = Carbon::now()->addDays(6);
  481. $this->assertSame('6 days from now', $d->diffForHumans());
  482. }
  483. public function testDiffForHumansNowAndFutureWeek()
  484. {
  485. $d = Carbon::now()->addWeek();
  486. $this->assertSame('1 week from now', $d->diffForHumans());
  487. }
  488. public function testDiffForHumansNowAndFutureWeeks()
  489. {
  490. $d = Carbon::now()->addWeeks(2);
  491. $this->assertSame('2 weeks from now', $d->diffForHumans());
  492. }
  493. public function testDiffForHumansNowAndNearlyFutureMonth()
  494. {
  495. $d = Carbon::now()->addWeeks(3);
  496. $this->assertSame('3 weeks from now', $d->diffForHumans());
  497. }
  498. public function testDiffForHumansNowAndFutureMonth()
  499. {
  500. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  501. $d = Carbon::now()->addWeeks(4);
  502. $this->assertSame('4 weeks from now', $d->diffForHumans());
  503. $d = Carbon::now()->addMonth();
  504. $this->assertSame('1 month from now', $d->diffForHumans());
  505. Carbon::setTestNow();
  506. }
  507. public function testDiffForHumansNowAndFutureMonths()
  508. {
  509. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  510. $d = Carbon::now()->addMonths(2);
  511. $this->assertSame('2 months from now', $d->diffForHumans());
  512. Carbon::setTestNow();
  513. }
  514. public function testDiffForHumansNowAndNearlyFutureYear()
  515. {
  516. $d = Carbon::now()->addMonths(11);
  517. $this->assertSame('11 months from now', $d->diffForHumans());
  518. }
  519. public function testDiffForHumansNowAndFutureYear()
  520. {
  521. $d = Carbon::now()->addYear();
  522. $this->assertSame('1 year from now', $d->diffForHumans());
  523. }
  524. public function testDiffForHumansNowAndFutureYears()
  525. {
  526. $d = Carbon::now()->addYears(2);
  527. $this->assertSame('2 years from now', $d->diffForHumans());
  528. }
  529. public function testDiffForHumansOtherAndSecond()
  530. {
  531. $d = Carbon::now()->addSecond();
  532. $this->assertSame('1 second before', Carbon::now()->diffForHumans($d));
  533. }
  534. public function testDiffForHumansOtherAndSeconds()
  535. {
  536. $d = Carbon::now()->addSeconds(2);
  537. $this->assertSame('2 seconds before', Carbon::now()->diffForHumans($d));
  538. }
  539. public function testDiffForHumansOtherAndNearlyMinute()
  540. {
  541. $d = Carbon::now()->addSeconds(59);
  542. $this->assertSame('59 seconds before', Carbon::now()->diffForHumans($d));
  543. }
  544. public function testDiffForHumansOtherAndMinute()
  545. {
  546. $d = Carbon::now()->addMinute();
  547. $this->assertSame('1 minute before', Carbon::now()->diffForHumans($d));
  548. }
  549. public function testDiffForHumansOtherAndMinutes()
  550. {
  551. $d = Carbon::now()->addMinutes(2);
  552. $this->assertSame('2 minutes before', Carbon::now()->diffForHumans($d));
  553. }
  554. public function testDiffForHumansOtherAndNearlyHour()
  555. {
  556. $d = Carbon::now()->addMinutes(59);
  557. $this->assertSame('59 minutes before', Carbon::now()->diffForHumans($d));
  558. }
  559. public function testDiffForHumansOtherAndHour()
  560. {
  561. $d = Carbon::now()->addHour();
  562. $this->assertSame('1 hour before', Carbon::now()->diffForHumans($d));
  563. }
  564. public function testDiffForHumansOtherAndHours()
  565. {
  566. $d = Carbon::now()->addHours(2);
  567. $this->assertSame('2 hours before', Carbon::now()->diffForHumans($d));
  568. }
  569. public function testDiffForHumansOtherAndNearlyDay()
  570. {
  571. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  572. $d = Carbon::now()->addHours(23);
  573. $this->assertSame('23 hours before', Carbon::now()->diffForHumans($d));
  574. Carbon::setTestNow();
  575. }
  576. public function testDiffForHumansOtherAndDay()
  577. {
  578. $d = Carbon::now()->addDay();
  579. $this->assertSame('1 day before', Carbon::now()->diffForHumans($d));
  580. }
  581. public function testDiffForHumansOtherAndDays()
  582. {
  583. $d = Carbon::now()->addDays(2);
  584. $this->assertSame('2 days before', Carbon::now()->diffForHumans($d));
  585. }
  586. public function testDiffForHumansOtherAndNearlyWeek()
  587. {
  588. $d = Carbon::now()->addDays(6);
  589. $this->assertSame('6 days before', Carbon::now()->diffForHumans($d));
  590. }
  591. public function testDiffForHumansOtherAndWeek()
  592. {
  593. $d = Carbon::now()->addWeek();
  594. $this->assertSame('1 week before', Carbon::now()->diffForHumans($d));
  595. }
  596. public function testDiffForHumansOtherAndWeeks()
  597. {
  598. $d = Carbon::now()->addWeeks(2);
  599. $this->assertSame('2 weeks before', Carbon::now()->diffForHumans($d));
  600. }
  601. public function testDiffForHumansOtherAndNearlyMonth()
  602. {
  603. $d = Carbon::now()->addWeeks(3);
  604. $this->assertSame('3 weeks before', Carbon::now()->diffForHumans($d));
  605. }
  606. public function testDiffForHumansOtherAndMonth()
  607. {
  608. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  609. $d = Carbon::now()->addWeeks(4);
  610. $this->assertSame('4 weeks before', Carbon::now()->diffForHumans($d));
  611. $d = Carbon::now()->addMonth();
  612. $this->assertSame('1 month before', Carbon::now()->diffForHumans($d));
  613. Carbon::setTestNow();
  614. }
  615. public function testDiffForHumansOtherAndMonths()
  616. {
  617. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  618. $d = Carbon::now()->addMonths(2);
  619. $this->assertSame('2 months before', Carbon::now()->diffForHumans($d));
  620. Carbon::setTestNow();
  621. }
  622. public function testDiffForHumansOtherAndNearlyYear()
  623. {
  624. $d = Carbon::now()->addMonths(11);
  625. $this->assertSame('11 months before', Carbon::now()->diffForHumans($d));
  626. }
  627. public function testDiffForHumansOtherAndYear()
  628. {
  629. $d = Carbon::now()->addYear();
  630. $this->assertSame('1 year before', Carbon::now()->diffForHumans($d));
  631. }
  632. public function testDiffForHumansOtherAndYears()
  633. {
  634. $d = Carbon::now()->addYears(2);
  635. $this->assertSame('2 years before', Carbon::now()->diffForHumans($d));
  636. }
  637. public function testDiffForHumansOtherAndFutureSecond()
  638. {
  639. $d = Carbon::now()->subSecond();
  640. $this->assertSame('1 second after', Carbon::now()->diffForHumans($d));
  641. }
  642. public function testDiffForHumansOtherAndFutureSeconds()
  643. {
  644. $d = Carbon::now()->subSeconds(2);
  645. $this->assertSame('2 seconds after', Carbon::now()->diffForHumans($d));
  646. }
  647. public function testDiffForHumansOtherAndNearlyFutureMinute()
  648. {
  649. $d = Carbon::now()->subSeconds(59);
  650. $this->assertSame('59 seconds after', Carbon::now()->diffForHumans($d));
  651. }
  652. public function testDiffForHumansOtherAndFutureMinute()
  653. {
  654. $d = Carbon::now()->subMinute();
  655. $this->assertSame('1 minute after', Carbon::now()->diffForHumans($d));
  656. }
  657. public function testDiffForHumansOtherAndFutureMinutes()
  658. {
  659. $d = Carbon::now()->subMinutes(2);
  660. $this->assertSame('2 minutes after', Carbon::now()->diffForHumans($d));
  661. }
  662. public function testDiffForHumansOtherAndNearlyFutureHour()
  663. {
  664. $d = Carbon::now()->subMinutes(59);
  665. $this->assertSame('59 minutes after', Carbon::now()->diffForHumans($d));
  666. }
  667. public function testDiffForHumansOtherAndFutureHour()
  668. {
  669. $d = Carbon::now()->subHour();
  670. $this->assertSame('1 hour after', Carbon::now()->diffForHumans($d));
  671. }
  672. public function testDiffForHumansOtherAndFutureHours()
  673. {
  674. $d = Carbon::now()->subHours(2);
  675. $this->assertSame('2 hours after', Carbon::now()->diffForHumans($d));
  676. }
  677. public function testDiffForHumansOtherAndNearlyFutureDay()
  678. {
  679. Carbon::setTestNow(Carbon::create(2012, 1, 15));
  680. $d = Carbon::now()->subHours(23);
  681. $this->assertSame('23 hours after', Carbon::now()->diffForHumans($d));
  682. Carbon::setTestNow();
  683. }
  684. public function testDiffForHumansOtherAndFutureDay()
  685. {
  686. $d = Carbon::now()->subDay();
  687. $this->assertSame('1 day after', Carbon::now()->diffForHumans($d));
  688. }
  689. public function testDiffForHumansOtherAndFutureDays()
  690. {
  691. $d = Carbon::now()->subDays(2);
  692. $this->assertSame('2 days after', Carbon::now()->diffForHumans($d));
  693. }
  694. public function testDiffForHumansOtherAndNearlyFutureWeek()
  695. {
  696. $d = Carbon::now()->subDays(6);
  697. $this->assertSame('6 days after', Carbon::now()->diffForHumans($d));
  698. }
  699. public function testDiffForHumansOtherAndFutureWeek()
  700. {
  701. $d = Carbon::now()->subWeek();
  702. $this->assertSame('1 week after', Carbon::now()->diffForHumans($d));
  703. }
  704. public function testDiffForHumansOtherAndFutureWeeks()
  705. {
  706. $d = Carbon::now()->subWeeks(2);
  707. $this->assertSame('2 weeks after', Carbon::now()->diffForHumans($d));
  708. }
  709. public function testDiffForHumansOtherAndNearlyFutureMonth()
  710. {
  711. $d = Carbon::now()->subWeeks(3);
  712. $this->assertSame('3 weeks after', Carbon::now()->diffForHumans($d));
  713. }
  714. public function testDiffForHumansOtherAndFutureMonth()
  715. {
  716. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  717. $d = Carbon::now()->subWeeks(4);
  718. $this->assertSame('4 weeks after', Carbon::now()->diffForHumans($d));
  719. $d = Carbon::now()->subMonth();
  720. $this->assertSame('1 month after', Carbon::now()->diffForHumans($d));
  721. Carbon::setTestNow();
  722. }
  723. public function testDiffForHumansOtherAndFutureMonths()
  724. {
  725. $d = Carbon::now()->subMonths(2);
  726. $this->assertSame('2 months after', Carbon::now()->diffForHumans($d));
  727. }
  728. public function testDiffForHumansOtherAndNearlyFutureYear()
  729. {
  730. $d = Carbon::now()->subMonths(11);
  731. $this->assertSame('11 months after', Carbon::now()->diffForHumans($d));
  732. }
  733. public function testDiffForHumansOtherAndFutureYear()
  734. {
  735. $d = Carbon::now()->subYear();
  736. $this->assertSame('1 year after', Carbon::now()->diffForHumans($d));
  737. }
  738. public function testDiffForHumansOtherAndFutureYears()
  739. {
  740. $d = Carbon::now()->subYears(2);
  741. $this->assertSame('2 years after', Carbon::now()->diffForHumans($d));
  742. }
  743. public function testDiffForHumansAbsoluteSeconds()
  744. {
  745. $d = Carbon::now()->subSeconds(59);
  746. $this->assertSame('59 seconds', Carbon::now()->diffForHumans($d, true));
  747. $d = Carbon::now()->addSeconds(59);
  748. $this->assertSame('59 seconds', Carbon::now()->diffForHumans($d, true));
  749. }
  750. public function testDiffForHumansAbsoluteMinutes()
  751. {
  752. $d = Carbon::now()->subMinutes(30);
  753. $this->assertSame('30 minutes', Carbon::now()->diffForHumans($d, true));
  754. $d = Carbon::now()->addMinutes(30);
  755. $this->assertSame('30 minutes', Carbon::now()->diffForHumans($d, true));
  756. }
  757. public function testDiffForHumansAbsoluteHours()
  758. {
  759. $d = Carbon::now()->subHours(3);
  760. $this->assertSame('3 hours', Carbon::now()->diffForHumans($d, true));
  761. $d = Carbon::now()->addHours(3);
  762. $this->assertSame('3 hours', Carbon::now()->diffForHumans($d, true));
  763. }
  764. public function testDiffForHumansAbsoluteDays()
  765. {
  766. $d = Carbon::now()->subDays(2);
  767. $this->assertSame('2 days', Carbon::now()->diffForHumans($d, true));
  768. $d = Carbon::now()->addDays(2);
  769. $this->assertSame('2 days', Carbon::now()->diffForHumans($d, true));
  770. }
  771. public function testDiffForHumansAbsoluteWeeks()
  772. {
  773. $d = Carbon::now()->subWeeks(2);
  774. $this->assertSame('2 weeks', Carbon::now()->diffForHumans($d, true));
  775. $d = Carbon::now()->addWeeks(2);
  776. $this->assertSame('2 weeks', Carbon::now()->diffForHumans($d, true));
  777. }
  778. public function testDiffForHumansAbsoluteMonths()
  779. {
  780. Carbon::setTestNow(Carbon::create(2012, 1, 1));
  781. $d = Carbon::now()->subMonths(2);
  782. $this->assertSame('2 months', Carbon::now()->diffForHumans($d, true));
  783. $d = Carbon::now()->addMonths(2);
  784. $this->assertSame('2 months', Carbon::now()->diffForHumans($d, true));
  785. Carbon::setTestNow();
  786. }
  787. public function testDiffForHumansAbsoluteYears()
  788. {
  789. $d = Carbon::now()->subYears(1);
  790. $this->assertSame('1 year', Carbon::now()->diffForHumans($d, true));
  791. $d = Carbon::now()->addYears(1);
  792. $this->assertSame('1 year', Carbon::now()->diffForHumans($d, true));
  793. }
  794. public function testDiffForHumansWithShorterMonthShouldStillBeAMonth()
  795. {
  796. $feb15 = Carbon::parse('2015-02-15');
  797. $mar15 = Carbon::parse('2015-03-15');
  798. $this->assertSame('1 month after', $mar15->diffForHumans($feb15));
  799. }
  800. }