DiffTest.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Carbon\CarbonInterface;
  14. use Carbon\CarbonInterval;
  15. use Carbon\Exceptions\InvalidFormatException;
  16. use Closure;
  17. use DateTime;
  18. use InvalidArgumentException;
  19. use Tests\AbstractTestCase;
  20. class DiffTest extends AbstractTestCase
  21. {
  22. public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null)
  23. {
  24. parent::wrapWithTestNow($func, $dt ?: Carbon::createMidnightDate(2012, 1, 1));
  25. }
  26. public function testDiffAsCarbonInterval()
  27. {
  28. $dt = Carbon::createFromDate(2000, 1, 1);
  29. $this->assertCarbonInterval($dt->diffAsCarbonInterval($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0);
  30. }
  31. public function testDiffInYearsPositive()
  32. {
  33. $dt = Carbon::createFromDate(2000, 1, 1);
  34. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()));
  35. }
  36. public function testDiffInYearsNegativeWithSign()
  37. {
  38. $dt = Carbon::createFromDate(2000, 1, 1);
  39. $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false));
  40. }
  41. public function testDiffInYearsNegativeNoSign()
  42. {
  43. $dt = Carbon::createFromDate(2000, 1, 1);
  44. $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear()));
  45. }
  46. public function testDiffInYearsVsDefaultNow()
  47. {
  48. $this->wrapWithTestNow(function () {
  49. $this->assertSame(1, Carbon::now()->subYear()->diffInYears());
  50. });
  51. }
  52. public function testDiffInYearsEnsureIsTruncated()
  53. {
  54. $dt = Carbon::createFromDate(2000, 1, 1);
  55. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7)));
  56. }
  57. public function testDiffInMonthsPositive()
  58. {
  59. $dt = Carbon::createFromDate(2000, 1, 1);
  60. $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth()));
  61. }
  62. public function testDiffInMonthsNegativeWithSign()
  63. {
  64. $dt = Carbon::createFromDate(2000, 1, 1);
  65. $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false));
  66. }
  67. public function testDiffInMonthsNegativeNoSign()
  68. {
  69. $dt = Carbon::createFromDate(2000, 1, 1);
  70. $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth()));
  71. }
  72. public function testDiffInMonthsVsDefaultNow()
  73. {
  74. $this->wrapWithTestNow(function () {
  75. $this->assertSame(12, Carbon::now()->subYear()->diffInMonths());
  76. });
  77. }
  78. public function testDiffInMonthsEnsureIsTruncated()
  79. {
  80. $dt = Carbon::createFromDate(2000, 1, 1);
  81. $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16)));
  82. }
  83. public function testDiffInDaysPositive()
  84. {
  85. $dt = Carbon::createFromDate(2000, 1, 1);
  86. $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear()));
  87. }
  88. public function testDiffInDaysNegativeWithSign()
  89. {
  90. $dt = Carbon::createFromDate(2000, 1, 1);
  91. $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false));
  92. }
  93. public function testDiffInDaysNegativeNoSign()
  94. {
  95. $dt = Carbon::createFromDate(2000, 1, 1);
  96. $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear()));
  97. }
  98. public function testDiffInDaysVsDefaultNow()
  99. {
  100. $this->wrapWithTestNow(function () {
  101. $this->assertSame(7, Carbon::now()->subWeek()->diffInDays());
  102. });
  103. }
  104. public function testDiffInDaysEnsureIsTruncated()
  105. {
  106. $dt = Carbon::createFromDate(2000, 1, 1);
  107. $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13)));
  108. }
  109. public function testDiffInDaysFilteredPositiveWithMutated()
  110. {
  111. $dt = Carbon::createFromDate(2000, 1, 1);
  112. $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
  113. return $date->dayOfWeek === 1;
  114. }, $dt->copy()->endOfMonth()));
  115. }
  116. public function testDiffInDaysFilteredPositiveWithSecondObject()
  117. {
  118. $dt1 = Carbon::createFromDate(2000, 1, 1);
  119. $dt2 = Carbon::createFromDate(2000, 1, 31);
  120. $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  121. return $date->dayOfWeek === Carbon::SUNDAY;
  122. }, $dt2));
  123. }
  124. public function testDiffInDaysFilteredNegativeNoSignWithMutated()
  125. {
  126. $dt = Carbon::createFromDate(2000, 1, 31);
  127. $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
  128. return $date->dayOfWeek === Carbon::SUNDAY;
  129. }, $dt->copy()->startOfMonth()));
  130. }
  131. public function testDiffInDaysFilteredNegativeNoSignWithSecondObject()
  132. {
  133. $dt1 = Carbon::createFromDate(2000, 1, 31);
  134. $dt2 = Carbon::createFromDate(2000, 1, 1);
  135. $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  136. return $date->dayOfWeek === Carbon::SUNDAY;
  137. }, $dt2));
  138. }
  139. public function testDiffInDaysFilteredNegativeWithSignWithMutated()
  140. {
  141. $dt = Carbon::createFromDate(2000, 1, 31);
  142. $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) {
  143. return $date->dayOfWeek === 1;
  144. }, $dt->copy()->startOfMonth(), false));
  145. }
  146. public function testDiffInDaysFilteredNegativeWithSignWithSecondObject()
  147. {
  148. $dt1 = Carbon::createFromDate(2000, 1, 31);
  149. $dt2 = Carbon::createFromDate(2000, 1, 1);
  150. $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) {
  151. return $date->dayOfWeek === Carbon::SUNDAY;
  152. }, $dt2, false));
  153. }
  154. public function testDiffInHoursFiltered()
  155. {
  156. $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay();
  157. $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
  158. $this->assertSame(31, $dt1->diffInHoursFiltered(function (Carbon $date) {
  159. return $date->hour === 9;
  160. }, $dt2));
  161. }
  162. public function testDiffInHoursFilteredNegative()
  163. {
  164. $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay();
  165. $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
  166. $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) {
  167. return $date->hour === 9;
  168. }, $dt2, false));
  169. }
  170. public function testDiffInHoursFilteredWorkHoursPerWeek()
  171. {
  172. $dt1 = Carbon::createFromDate(2000, 1, 5)->endOfDay();
  173. $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
  174. $this->assertSame(40, $dt1->diffInHoursFiltered(function (Carbon $date) {
  175. return $date->hour > 8 && $date->hour < 17;
  176. }, $dt2));
  177. }
  178. public function testDiffFilteredUsingMinutesPositiveWithMutated()
  179. {
  180. $dt = Carbon::createFromDate(2000, 1, 1)->startOfDay();
  181. $this->assertSame(60, $dt->diffFiltered(CarbonInterval::minute(), function (Carbon $date) {
  182. return $date->hour === 12;
  183. }, Carbon::createFromDate(2000, 1, 1)->endOfDay()));
  184. }
  185. public function testDiffFilteredPositiveWithSecondObject()
  186. {
  187. $dt1 = Carbon::create(2000, 1, 1);
  188. $dt2 = $dt1->copy()->addSeconds(80);
  189. $this->assertSame(40, $dt1->diffFiltered(CarbonInterval::second(), function (Carbon $date) {
  190. return $date->second % 2 === 0;
  191. }, $dt2));
  192. }
  193. public function testDiffFilteredNegativeNoSignWithMutated()
  194. {
  195. $dt = Carbon::createFromDate(2000, 1, 31);
  196. $this->assertSame(2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) {
  197. return $date->dayOfWeek === Carbon::SUNDAY;
  198. }, $dt->copy()->startOfMonth()));
  199. }
  200. public function testDiffFilteredNegativeNoSignWithSecondObject()
  201. {
  202. $dt1 = Carbon::createFromDate(2006, 1, 31);
  203. $dt2 = Carbon::createFromDate(2000, 1, 1);
  204. $this->assertSame(7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) {
  205. return $date->month === 1;
  206. }, $dt2));
  207. }
  208. public function testDiffFilteredNegativeWithSignWithMutated()
  209. {
  210. $dt = Carbon::createFromDate(2000, 1, 31);
  211. $this->assertSame(-4, $dt->diffFiltered(CarbonInterval::week(), function (Carbon $date) {
  212. return $date->month === 12;
  213. }, $dt->copy()->subMonths(3), false));
  214. }
  215. public function testDiffFilteredNegativeWithSignWithSecondObject()
  216. {
  217. $dt1 = Carbon::createFromDate(2001, 1, 31);
  218. $dt2 = Carbon::createFromDate(1999, 1, 1);
  219. $this->assertSame(-12, $dt1->diffFiltered(CarbonInterval::month(), function (Carbon $date) {
  220. return $date->year === 2000;
  221. }, $dt2, false));
  222. }
  223. public function testBug188DiffWithSameDates()
  224. {
  225. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  226. $end = $start->copy();
  227. $this->assertSame(0, $start->diffInDays($end));
  228. $this->assertSame(0, $start->diffInWeekdays($end));
  229. }
  230. public function testBug188DiffWithDatesOnlyHoursApart()
  231. {
  232. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  233. $end = $start->copy();
  234. $this->assertSame(0, $start->diffInDays($end));
  235. $this->assertSame(0, $start->diffInWeekdays($end));
  236. }
  237. public function testBug188DiffWithSameDates1DayApart()
  238. {
  239. $start = Carbon::create(2014, 10, 8, 15, 20, 0);
  240. $end = $start->copy()->addDay();
  241. $this->assertSame(1, $start->diffInDays($end));
  242. $this->assertSame(1, $start->diffInWeekdays($end));
  243. }
  244. public function testBug188DiffWithDatesOnTheWeekend()
  245. {
  246. $start = Carbon::create(2014, 1, 1, 0, 0, 0);
  247. $start = $start->next(Carbon::SATURDAY);
  248. $end = $start->copy()->addDay();
  249. $this->assertSame(1, $start->diffInDays($end));
  250. $this->assertSame(0, $start->diffInWeekdays($end));
  251. }
  252. public function testDiffInWeekdaysPositive()
  253. {
  254. $dt = Carbon::createFromDate(2000, 1, 1);
  255. $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->addMonth()));
  256. }
  257. public function testDiffInWeekdaysNegativeNoSign()
  258. {
  259. $dt = Carbon::createFromDate(2000, 1, 31);
  260. $this->assertSame(20, $dt->diffInWeekdays($dt->copy()->startOfMonth()));
  261. }
  262. public function testDiffInWeekdaysNegativeWithSign()
  263. {
  264. $dt = Carbon::createFromDate(2000, 1, 31);
  265. $this->assertSame(-20, $dt->diffInWeekdays($dt->copy()->startOfMonth(), false));
  266. }
  267. public function testDiffInWeekendDaysPositive()
  268. {
  269. $dt = Carbon::createFromDate(2000, 1, 1);
  270. $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->endOfMonth()));
  271. }
  272. public function testDiffInWeekendDaysNegativeNoSign()
  273. {
  274. $dt = Carbon::createFromDate(2000, 1, 31);
  275. $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth()));
  276. }
  277. public function testDiffInWeekendDaysNegativeWithSign()
  278. {
  279. $dt = Carbon::createFromDate(2000, 1, 31);
  280. $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), false));
  281. }
  282. public function testDiffInWeeksPositive()
  283. {
  284. $dt = Carbon::createFromDate(2000, 1, 1);
  285. $this->assertSame(52, $dt->diffInWeeks($dt->copy()->addYear()));
  286. }
  287. public function testDiffInWeeksNegativeWithSign()
  288. {
  289. $dt = Carbon::createFromDate(2000, 1, 1);
  290. $this->assertSame(-52, $dt->diffInWeeks($dt->copy()->subYear(), false));
  291. }
  292. public function testDiffInWeeksNegativeNoSign()
  293. {
  294. $dt = Carbon::createFromDate(2000, 1, 1);
  295. $this->assertSame(52, $dt->diffInWeeks($dt->copy()->subYear()));
  296. }
  297. public function testDiffInWeeksVsDefaultNow()
  298. {
  299. $this->wrapWithTestNow(function () {
  300. $this->assertSame(1, Carbon::now()->subWeek()->diffInWeeks());
  301. });
  302. }
  303. public function testDiffInWeeksEnsureIsTruncated()
  304. {
  305. $dt = Carbon::createFromDate(2000, 1, 1);
  306. $this->assertSame(0, $dt->diffInWeeks($dt->copy()->addWeek()->subDay()));
  307. }
  308. public function testDiffInHoursPositive()
  309. {
  310. $dt = Carbon::createFromDate(2000, 1, 1);
  311. $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2)));
  312. }
  313. public function testDiffInHoursNegativeWithSign()
  314. {
  315. $dt = Carbon::createFromDate(2000, 1, 1);
  316. $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false));
  317. }
  318. public function testDiffInHoursNegativeNoSign()
  319. {
  320. $dt = Carbon::createFromDate(2000, 1, 1);
  321. $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2)));
  322. }
  323. public function testDiffInHoursVsDefaultNow()
  324. {
  325. $this->wrapWithTestNow(function () {
  326. $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours());
  327. }, Carbon::create(2012, 1, 15));
  328. }
  329. public function testDiffInHoursEnsureIsTruncated()
  330. {
  331. $dt = Carbon::createFromDate(2000, 1, 1);
  332. $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31)));
  333. }
  334. public function testDiffInHoursWithTimezones()
  335. {
  336. date_default_timezone_set('Africa/Algiers');
  337. Carbon::setTestNow();
  338. $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto');
  339. $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver');
  340. $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver');
  341. $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');
  342. sleep(2);
  343. $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');
  344. $this->assertSame(0, $dtVancouver->diffInHours($dtToronto) % 24);
  345. $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto');
  346. $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver');
  347. $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver');
  348. }
  349. public function testDiffInMinutesPositive()
  350. {
  351. $dt = Carbon::createFromDate(2000, 1, 1);
  352. $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2)));
  353. }
  354. public function testDiffInMinutesPositiveALot()
  355. {
  356. $dt = Carbon::createFromDate(2000, 1, 1);
  357. $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2)));
  358. }
  359. public function testDiffInMinutesNegativeWithSign()
  360. {
  361. $dt = Carbon::createFromDate(2000, 1, 1);
  362. $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false));
  363. }
  364. public function testDiffInMinutesNegativeNoSign()
  365. {
  366. $dt = Carbon::createFromDate(2000, 1, 1);
  367. $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2)));
  368. }
  369. public function testDiffInMinutesVsDefaultNow()
  370. {
  371. $this->wrapWithTestNow(function () {
  372. $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes());
  373. });
  374. }
  375. public function testDiffInMinutesEnsureIsTruncated()
  376. {
  377. $dt = Carbon::createFromDate(2000, 1, 1);
  378. $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31)));
  379. }
  380. public function testDiffInSecondsPositive()
  381. {
  382. $dt = Carbon::createFromDate(2000, 1, 1);
  383. $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2)));
  384. }
  385. public function testDiffInSecondsPositiveALot()
  386. {
  387. $dt = Carbon::createFromDate(2000, 1, 1);
  388. $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2)));
  389. }
  390. public function testDiffInSecondsNegativeWithSign()
  391. {
  392. $dt = Carbon::createFromDate(2000, 1, 1);
  393. $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false));
  394. }
  395. public function testDiffInSecondsNegativeNoSign()
  396. {
  397. $dt = Carbon::createFromDate(2000, 1, 1);
  398. $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2)));
  399. }
  400. public function testDiffInSecondsVsDefaultNow()
  401. {
  402. $this->wrapWithTestNow(function () {
  403. $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds());
  404. });
  405. }
  406. public function testDiffInSecondsEnsureIsTruncated()
  407. {
  408. $dt = Carbon::createFromDate(2000, 1, 1);
  409. $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9)));
  410. }
  411. public function testDiffInSecondsWithTimezones()
  412. {
  413. $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  414. $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
  415. $this->assertSame(0, $dtOttawa->diffInSeconds($dtVancouver) % (24 * 3600));
  416. $dtOttawa = Carbon::createMidnightDate(2000, 1, 1, 'America/Toronto');
  417. $dtVancouver = Carbon::createMidnightDate(2000, 1, 1, 'America/Vancouver');
  418. $this->assertSame(3 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver));
  419. }
  420. public function testDiffInSecondsWithTimezonesAndVsDefault()
  421. {
  422. $vanNow = Carbon::now('America/Vancouver');
  423. $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz);
  424. $this->wrapWithTestNow(function () use ($vanNow) {
  425. $this->assertSame(0, $vanNow->diffInSeconds());
  426. }, $hereNow);
  427. }
  428. public function testDiffForHumansNowAndSecond()
  429. {
  430. $this->wrapWithTestNow(function () {
  431. $this->assertSame('1 second ago', Carbon::now()->diffForHumans());
  432. });
  433. }
  434. public function testDiffForHumansNowAndSecondWithTimezone()
  435. {
  436. $vanNow = Carbon::now('America/Vancouver');
  437. $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz);
  438. $this->wrapWithTestNow(function () use ($vanNow) {
  439. $this->assertSame('1 second ago', $vanNow->diffForHumans());
  440. }, $hereNow);
  441. }
  442. public function testDiffForHumansNowAndSeconds()
  443. {
  444. $this->wrapWithTestNow(function () {
  445. $this->assertSame('2 seconds ago', Carbon::now()->subSeconds(2)->diffForHumans());
  446. });
  447. }
  448. public function testDiffForHumansNowAndNearlyMinute()
  449. {
  450. $this->wrapWithTestNow(function () {
  451. $this->assertSame('59 seconds ago', Carbon::now()->subSeconds(59)->diffForHumans());
  452. });
  453. }
  454. public function testDiffForHumansNowAndMinute()
  455. {
  456. $this->wrapWithTestNow(function () {
  457. $this->assertSame('1 minute ago', Carbon::now()->subMinute()->diffForHumans());
  458. });
  459. }
  460. public function testDiffForHumansNowAndMinutes()
  461. {
  462. $this->wrapWithTestNow(function () {
  463. $this->assertSame('2 minutes ago', Carbon::now()->subMinutes(2)->diffForHumans());
  464. });
  465. }
  466. public function testDiffForHumansNowAndNearlyHour()
  467. {
  468. $this->wrapWithTestNow(function () {
  469. $this->assertSame('59 minutes ago', Carbon::now()->subMinutes(59)->diffForHumans());
  470. });
  471. }
  472. public function testDiffForHumansNowAndHour()
  473. {
  474. $this->wrapWithTestNow(function () {
  475. $this->assertSame('1 hour ago', Carbon::now()->subHour()->diffForHumans());
  476. });
  477. }
  478. public function testDiffForHumansNowAndHours()
  479. {
  480. $this->wrapWithTestNow(function () {
  481. $this->assertSame('2 hours ago', Carbon::now()->subHours(2)->diffForHumans());
  482. });
  483. }
  484. public function testDiffForHumansNowAndNearlyDay()
  485. {
  486. $this->wrapWithTestNow(function () {
  487. $this->assertSame('23 hours ago', Carbon::now()->subHours(23)->diffForHumans());
  488. });
  489. }
  490. public function testDiffForHumansNowAndDay()
  491. {
  492. $this->wrapWithTestNow(function () {
  493. $this->assertSame('1 day ago', Carbon::now()->subDay()->diffForHumans());
  494. });
  495. }
  496. public function testDiffForHumansNowAndDays()
  497. {
  498. $this->wrapWithTestNow(function () {
  499. $this->assertSame('2 days ago', Carbon::now()->subDays(2)->diffForHumans());
  500. });
  501. }
  502. public function testDiffForHumansNowAndNearlyWeek()
  503. {
  504. $this->wrapWithTestNow(function () {
  505. $this->assertSame('6 days ago', Carbon::now()->subDays(6)->diffForHumans());
  506. });
  507. }
  508. public function testDiffForHumansNowAndWeek()
  509. {
  510. $this->wrapWithTestNow(function () {
  511. $this->assertSame('1 week ago', Carbon::now()->subWeek()->diffForHumans());
  512. });
  513. }
  514. public function testDiffForHumansNowAndWeeks()
  515. {
  516. $this->wrapWithTestNow(function () {
  517. $this->assertSame('2 weeks ago', Carbon::now()->subWeeks(2)->diffForHumans());
  518. });
  519. }
  520. public function testDiffForHumansNowAndNearlyMonth()
  521. {
  522. $this->wrapWithTestNow(function () {
  523. $this->assertSame('3 weeks ago', Carbon::now()->subWeeks(3)->diffForHumans());
  524. });
  525. }
  526. public function testDiffForHumansNowAndMonth()
  527. {
  528. Carbon::setTestNow('2018-12-01');
  529. $this->assertSame('4 weeks ago', Carbon::now()->subWeeks(4)->diffForHumans());
  530. $this->assertSame('1 month ago', Carbon::now()->subMonth()->diffForHumans());
  531. }
  532. public function testDiffForHumansNowAndMonths()
  533. {
  534. $this->wrapWithTestNow(function () {
  535. $this->assertSame('2 months ago', Carbon::now()->subMonthsNoOverflow(2)->diffForHumans());
  536. });
  537. }
  538. public function testDiffForHumansNowAndNearlyYear()
  539. {
  540. $this->wrapWithTestNow(function () {
  541. $this->assertSame('11 months ago', Carbon::now()->subMonthsNoOverflow(11)->diffForHumans());
  542. });
  543. }
  544. public function testDiffForHumansNowAndYear()
  545. {
  546. $this->wrapWithTestNow(function () {
  547. $this->assertSame('1 year ago', Carbon::now()->subYear()->diffForHumans());
  548. });
  549. }
  550. public function testDiffForHumansNowAndYears()
  551. {
  552. $this->wrapWithTestNow(function () {
  553. $this->assertSame('2 years ago', Carbon::now()->subYears(2)->diffForHumans());
  554. });
  555. }
  556. public function testDiffForHumansNowAndFutureSecond()
  557. {
  558. $this->wrapWithTestNow(function () {
  559. $this->assertSame('1 second from now', Carbon::now()->addSecond()->diffForHumans());
  560. });
  561. }
  562. public function testDiffForHumansNowAndFutureSeconds()
  563. {
  564. $this->wrapWithTestNow(function () {
  565. $this->assertSame('2 seconds from now', Carbon::now()->addSeconds(2)->diffForHumans());
  566. });
  567. }
  568. public function testDiffForHumansNowAndNearlyFutureMinute()
  569. {
  570. $this->wrapWithTestNow(function () {
  571. $this->assertSame('59 seconds from now', Carbon::now()->addSeconds(59)->diffForHumans());
  572. });
  573. }
  574. public function testDiffForHumansNowAndFutureMinute()
  575. {
  576. $this->wrapWithTestNow(function () {
  577. $this->assertSame('1 minute from now', Carbon::now()->addMinute()->diffForHumans());
  578. });
  579. }
  580. public function testDiffForHumansNowAndFutureMinutes()
  581. {
  582. $this->wrapWithTestNow(function () {
  583. $this->assertSame('2 minutes from now', Carbon::now()->addMinutes(2)->diffForHumans());
  584. });
  585. }
  586. public function testDiffForHumansNowAndNearlyFutureHour()
  587. {
  588. $this->wrapWithTestNow(function () {
  589. $this->assertSame('59 minutes from now', Carbon::now()->addMinutes(59)->diffForHumans());
  590. });
  591. }
  592. public function testDiffForHumansNowAndFutureHour()
  593. {
  594. $this->wrapWithTestNow(function () {
  595. $this->assertSame('1 hour from now', Carbon::now()->addHour()->diffForHumans());
  596. });
  597. }
  598. public function testDiffForHumansNowAndFutureHours()
  599. {
  600. $this->wrapWithTestNow(function () {
  601. $this->assertSame('2 hours from now', Carbon::now()->addHours(2)->diffForHumans());
  602. });
  603. }
  604. public function testDiffForHumansNowAndNearlyFutureDay()
  605. {
  606. $this->wrapWithTestNow(function () {
  607. $this->assertSame('23 hours from now', Carbon::now()->addHours(23)->diffForHumans());
  608. });
  609. }
  610. public function testDiffForHumansNowAndFutureDay()
  611. {
  612. $this->wrapWithTestNow(function () {
  613. $this->assertSame('1 day from now', Carbon::now()->addDay()->diffForHumans());
  614. });
  615. }
  616. public function testDiffForHumansNowAndFutureDays()
  617. {
  618. $this->wrapWithTestNow(function () {
  619. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->diffForHumans());
  620. });
  621. }
  622. public function testDiffForHumansNowAndNearlyFutureWeek()
  623. {
  624. $this->wrapWithTestNow(function () {
  625. $this->assertSame('6 days from now', Carbon::now()->addDays(6)->diffForHumans());
  626. });
  627. }
  628. public function testDiffForHumansNowAndFutureWeek()
  629. {
  630. $this->wrapWithTestNow(function () {
  631. $this->assertSame('1 week from now', Carbon::now()->addWeek()->diffForHumans());
  632. });
  633. }
  634. public function testDiffForHumansNowAndFutureWeeks()
  635. {
  636. $this->wrapWithTestNow(function () {
  637. $this->assertSame('2 weeks from now', Carbon::now()->addWeeks(2)->diffForHumans());
  638. });
  639. }
  640. public function testDiffForHumansNowAndNearlyFutureMonth()
  641. {
  642. $this->wrapWithTestNow(function () {
  643. $this->assertSame('3 weeks from now', Carbon::now()->addWeeks(3)->diffForHumans());
  644. });
  645. }
  646. public function testDiffForHumansNowAndFutureMonth()
  647. {
  648. $this->wrapWithTestNow(function () {
  649. $this->assertSame('4 weeks from now', Carbon::now()->addWeeks(4)->diffForHumans());
  650. $this->assertSame('1 month from now', Carbon::now()->addMonth()->diffForHumans());
  651. });
  652. }
  653. public function testDiffForHumansNowAndFutureMonths()
  654. {
  655. $this->wrapWithTestNow(function () {
  656. $this->assertSame('2 months from now', Carbon::now()->addMonths(2)->diffForHumans());
  657. });
  658. }
  659. public function testDiffForHumansNowAndNearlyFutureYear()
  660. {
  661. $this->wrapWithTestNow(function () {
  662. $this->assertSame('11 months from now', Carbon::now()->addMonths(11)->diffForHumans());
  663. });
  664. }
  665. public function testDiffForHumansNowAndFutureYear()
  666. {
  667. $this->wrapWithTestNow(function () {
  668. $this->assertSame('1 year from now', Carbon::now()->addYear()->diffForHumans());
  669. });
  670. }
  671. public function testDiffForHumansNowAndFutureYears()
  672. {
  673. $this->wrapWithTestNow(function () {
  674. $this->assertSame('2 years from now', Carbon::now()->addYears(2)->diffForHumans());
  675. });
  676. }
  677. public function testDiffForHumansOtherAndSecond()
  678. {
  679. $this->wrapWithTestNow(function () {
  680. $this->assertSame('1 second before', Carbon::now()->diffForHumans(Carbon::now()->addSecond()));
  681. });
  682. }
  683. public function testDiffForHumansOtherAndSeconds()
  684. {
  685. $this->wrapWithTestNow(function () {
  686. $this->assertSame('2 seconds before', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(2)));
  687. });
  688. }
  689. public function testDiffForHumansOtherAndNearlyMinute()
  690. {
  691. $this->wrapWithTestNow(function () {
  692. $this->assertSame('59 seconds before', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(59)));
  693. });
  694. }
  695. public function testDiffForHumansOtherAndMinute()
  696. {
  697. $this->wrapWithTestNow(function () {
  698. $this->assertSame('1 minute before', Carbon::now()->diffForHumans(Carbon::now()->addMinute()));
  699. });
  700. }
  701. public function testDiffForHumansOtherAndMinutes()
  702. {
  703. $this->wrapWithTestNow(function () {
  704. $this->assertSame('2 minutes before', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(2)));
  705. });
  706. }
  707. public function testDiffForHumansOtherAndNearlyHour()
  708. {
  709. $this->wrapWithTestNow(function () {
  710. $this->assertSame('59 minutes before', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(59)));
  711. });
  712. }
  713. public function testDiffForHumansOtherAndHour()
  714. {
  715. $this->wrapWithTestNow(function () {
  716. $this->assertSame('1 hour before', Carbon::now()->diffForHumans(Carbon::now()->addHour()));
  717. });
  718. }
  719. public function testDiffForHumansOtherAndHours()
  720. {
  721. $this->wrapWithTestNow(function () {
  722. $this->assertSame('2 hours before', Carbon::now()->diffForHumans(Carbon::now()->addHours(2)));
  723. });
  724. }
  725. public function testDiffForHumansOtherAndNearlyDay()
  726. {
  727. $this->wrapWithTestNow(function () {
  728. $this->assertSame('23 hours before', Carbon::now()->diffForHumans(Carbon::now()->addHours(23)));
  729. });
  730. }
  731. public function testDiffForHumansOtherAndDay()
  732. {
  733. $this->wrapWithTestNow(function () {
  734. $this->assertSame('1 day before', Carbon::now()->diffForHumans(Carbon::now()->addDay()));
  735. });
  736. }
  737. public function testDiffForHumansOtherAndDays()
  738. {
  739. $this->wrapWithTestNow(function () {
  740. $this->assertSame('2 days before', Carbon::now()->diffForHumans(Carbon::now()->addDays(2)));
  741. });
  742. }
  743. public function testDiffForHumansOtherAndNearlyWeek()
  744. {
  745. $this->wrapWithTestNow(function () {
  746. $this->assertSame('6 days before', Carbon::now()->diffForHumans(Carbon::now()->addDays(6)));
  747. });
  748. }
  749. public function testDiffForHumansOverWeekWithDefaultPartsCount()
  750. {
  751. $this->wrapWithTestNow(function () {
  752. $this->assertSame('1 week ago', Carbon::now()->subDays(8)->diffForHumans());
  753. });
  754. }
  755. public function testDiffForHumansOverWeekWithPartsCount1()
  756. {
  757. $this->wrapWithTestNow(function () {
  758. $this->assertSame(
  759. '1 week ago',
  760. Carbon::now()->subDays(8)->diffForHumans(null, false, false, 1)
  761. );
  762. });
  763. }
  764. public function testDiffForHumansOverWeekWithPartsCount2()
  765. {
  766. $this->wrapWithTestNow(function () {
  767. $this->assertSame(
  768. '1 week 1 day ago',
  769. Carbon::now()->subDays(8)->diffForHumans(null, false, false, 2)
  770. );
  771. });
  772. }
  773. public function testDiffForHumansOverWeekWithMicrosecondsBuggyGap()
  774. {
  775. $this->wrapWithTestNow(function () {
  776. $this->assertSame(
  777. '23 hours 59 minutes 59 seconds after',
  778. Carbon::parse('2018-12-03 12:34:45.123456')
  779. ->diffForHumans('2018-12-02 12:34:45.123476', ['parts' => 3])
  780. );
  781. });
  782. }
  783. public function testDiffForHumansOtherAndWeek()
  784. {
  785. $this->wrapWithTestNow(function () {
  786. $this->assertSame('1 week before', Carbon::now()->diffForHumans(Carbon::now()->addWeek()));
  787. });
  788. }
  789. public function testDiffForHumansOtherAndWeeks()
  790. {
  791. $this->wrapWithTestNow(function () {
  792. $this->assertSame('2 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(2)));
  793. });
  794. }
  795. public function testDiffForHumansOtherAndNearlyMonth()
  796. {
  797. $this->wrapWithTestNow(function () {
  798. $this->assertSame('3 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(3)));
  799. });
  800. }
  801. public function testDiffForHumansOtherAndMonth()
  802. {
  803. $this->wrapWithTestNow(function () {
  804. $this->assertSame('4 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(4)));
  805. $this->assertSame('1 month before', Carbon::now()->diffForHumans(Carbon::now()->addMonth()));
  806. });
  807. }
  808. public function testDiffForHumansOtherAndMonths()
  809. {
  810. $this->wrapWithTestNow(function () {
  811. $this->assertSame('2 months before', Carbon::now()->diffForHumans(Carbon::now()->addMonths(2)));
  812. });
  813. }
  814. public function testDiffForHumansOtherAndNearlyYear()
  815. {
  816. $this->wrapWithTestNow(function () {
  817. $this->assertSame('11 months before', Carbon::now()->diffForHumans(Carbon::now()->addMonths(11)));
  818. });
  819. }
  820. public function testDiffForHumansOtherAndYear()
  821. {
  822. $this->wrapWithTestNow(function () {
  823. $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYear()));
  824. });
  825. }
  826. public function testDiffForHumansOtherAndYears()
  827. {
  828. $this->wrapWithTestNow(function () {
  829. $this->assertSame('2 years before', Carbon::now()->diffForHumans(Carbon::now()->addYears(2)));
  830. });
  831. }
  832. public function testDiffForHumansOtherAndFutureSecond()
  833. {
  834. $this->wrapWithTestNow(function () {
  835. $this->assertSame('1 second after', Carbon::now()->diffForHumans(Carbon::now()->subSecond()));
  836. });
  837. }
  838. public function testDiffForHumansOtherAndFutureSeconds()
  839. {
  840. $this->wrapWithTestNow(function () {
  841. $this->assertSame('2 seconds after', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(2)));
  842. });
  843. }
  844. public function testDiffForHumansOtherAndNearlyFutureMinute()
  845. {
  846. $this->wrapWithTestNow(function () {
  847. $this->assertSame('59 seconds after', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(59)));
  848. });
  849. }
  850. public function testDiffForHumansOtherAndFutureMinute()
  851. {
  852. $this->wrapWithTestNow(function () {
  853. $this->assertSame('1 minute after', Carbon::now()->diffForHumans(Carbon::now()->subMinute()));
  854. });
  855. }
  856. public function testDiffForHumansOtherAndFutureMinutes()
  857. {
  858. $this->wrapWithTestNow(function () {
  859. $this->assertSame('2 minutes after', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(2)));
  860. });
  861. }
  862. public function testDiffForHumansOtherAndNearlyFutureHour()
  863. {
  864. $this->wrapWithTestNow(function () {
  865. $this->assertSame('59 minutes after', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(59)));
  866. });
  867. }
  868. public function testDiffForHumansOtherAndFutureHour()
  869. {
  870. $this->wrapWithTestNow(function () {
  871. $this->assertSame('1 hour after', Carbon::now()->diffForHumans(Carbon::now()->subHour()));
  872. });
  873. }
  874. public function testDiffForHumansOtherAndFutureHours()
  875. {
  876. $this->wrapWithTestNow(function () {
  877. $this->assertSame('2 hours after', Carbon::now()->diffForHumans(Carbon::now()->subHours(2)));
  878. });
  879. }
  880. public function testDiffForHumansOtherAndNearlyFutureDay()
  881. {
  882. $this->wrapWithTestNow(function () {
  883. $this->assertSame('23 hours after', Carbon::now()->diffForHumans(Carbon::now()->subHours(23)));
  884. });
  885. }
  886. public function testDiffForHumansOtherAndFutureDay()
  887. {
  888. $this->wrapWithTestNow(function () {
  889. $this->assertSame('1 day after', Carbon::now()->diffForHumans(Carbon::now()->subDay()));
  890. });
  891. }
  892. public function testDiffForHumansOtherAndFutureDays()
  893. {
  894. $this->wrapWithTestNow(function () {
  895. $this->assertSame('2 days after', Carbon::now()->diffForHumans(Carbon::now()->subDays(2)));
  896. });
  897. }
  898. public function testDiffForHumansOtherAndNearlyFutureWeek()
  899. {
  900. $this->wrapWithTestNow(function () {
  901. $this->assertSame('6 days after', Carbon::now()->diffForHumans(Carbon::now()->subDays(6)));
  902. });
  903. }
  904. public function testDiffForHumansOtherAndFutureWeek()
  905. {
  906. $this->wrapWithTestNow(function () {
  907. $this->assertSame('1 week after', Carbon::now()->diffForHumans(Carbon::now()->subWeek()));
  908. });
  909. }
  910. public function testDiffForHumansOtherAndFutureWeeks()
  911. {
  912. $this->wrapWithTestNow(function () {
  913. $this->assertSame('2 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(2)));
  914. });
  915. }
  916. public function testDiffForHumansOtherAndNearlyFutureMonth()
  917. {
  918. $this->wrapWithTestNow(function () {
  919. $this->assertSame('3 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(3)));
  920. });
  921. }
  922. public function testDiffForHumansOtherAndFutureMonth()
  923. {
  924. Carbon::setTestNow('2018-12-01');
  925. $this->assertSame('4 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(4)));
  926. $this->assertSame('1 month after', Carbon::now()->diffForHumans(Carbon::now()->subMonth()));
  927. }
  928. public function testDiffForHumansOtherAndFutureMonths()
  929. {
  930. $this->wrapWithTestNow(function () {
  931. $this->assertSame('2 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(2)));
  932. });
  933. }
  934. public function testDiffForHumansOtherAndNearlyFutureYear()
  935. {
  936. $this->wrapWithTestNow(function () {
  937. $this->assertSame('11 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(11)));
  938. });
  939. }
  940. public function testDiffForHumansOtherAndFutureYear()
  941. {
  942. $this->wrapWithTestNow(function () {
  943. $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYear()));
  944. });
  945. }
  946. public function testDiffForHumansOtherAndFutureYears()
  947. {
  948. $this->wrapWithTestNow(function () {
  949. $this->assertSame('2 years after', Carbon::now()->diffForHumans(Carbon::now()->subYears(2)));
  950. });
  951. }
  952. public function testDiffForHumansAbsoluteSeconds()
  953. {
  954. $this->wrapWithTestNow(function () {
  955. $this->assertSame('59 seconds', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(59), true));
  956. $this->assertSame('59 seconds', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(59), true));
  957. });
  958. }
  959. public function testDiffForHumansAbsoluteMinutes()
  960. {
  961. $this->wrapWithTestNow(function () {
  962. $this->assertSame('30 minutes', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(30), true));
  963. $this->assertSame('30 minutes', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(30), true));
  964. });
  965. }
  966. public function testDiffForHumansAbsoluteHours()
  967. {
  968. $this->wrapWithTestNow(function () {
  969. $this->assertSame('3 hours', Carbon::now()->diffForHumans(Carbon::now()->subHours(3), true));
  970. $this->assertSame('3 hours', Carbon::now()->diffForHumans(Carbon::now()->addHours(3), true));
  971. });
  972. }
  973. public function testDiffForHumansAbsoluteDays()
  974. {
  975. $this->wrapWithTestNow(function () {
  976. $this->assertSame('2 days', Carbon::now()->diffForHumans(Carbon::now()->subDays(2), true));
  977. $this->assertSame('2 days', Carbon::now()->diffForHumans(Carbon::now()->addDays(2), true));
  978. });
  979. }
  980. public function testDiffForHumansAbsoluteWeeks()
  981. {
  982. $this->wrapWithTestNow(function () {
  983. $this->assertSame('2 weeks', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(2), true));
  984. $this->assertSame('2 weeks', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(2), true));
  985. });
  986. }
  987. public function testDiffForHumansAbsoluteMonths()
  988. {
  989. $this->wrapWithTestNow(function () {
  990. $this->assertSame('2 months', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(2), true));
  991. $this->assertSame('2 months', Carbon::now()->diffForHumans(Carbon::now()->addMonthsNoOverflow(2), true));
  992. });
  993. }
  994. public function testDiffForHumansAbsoluteYears()
  995. {
  996. $this->wrapWithTestNow(function () {
  997. $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), true));
  998. $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), true));
  999. });
  1000. }
  1001. public function testDiffForHumansWithOptions()
  1002. {
  1003. $this->wrapWithTestNow(function () {
  1004. $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_ABSOLUTE));
  1005. $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_ABSOLUTE));
  1006. $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_AUTO));
  1007. $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_AUTO));
  1008. $this->assertSame('1 year from now', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1009. $this->assertSame('1 year ago', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1010. $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1011. $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1012. $this->assertSame('1 year', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_ABSOLUTE));
  1013. $this->assertSame('1 year', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_ABSOLUTE));
  1014. $this->assertSame('1 year ago', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_AUTO));
  1015. $this->assertSame('1 year from now', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_AUTO));
  1016. $this->assertSame('1 year ago', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1017. $this->assertSame('1 year from now', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1018. $this->assertSame('1 year before', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1019. $this->assertSame('1 year after', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1020. });
  1021. }
  1022. public function testDiffForHumansWithShorterMonthShouldStillBeAMonth()
  1023. {
  1024. $feb15 = Carbon::parse('2015-02-15');
  1025. $mar15 = Carbon::parse('2015-03-15');
  1026. $this->assertSame('1 month after', $mar15->diffForHumans($feb15));
  1027. }
  1028. public function testDiffForHumansWithDateTimeInstance()
  1029. {
  1030. $feb15 = new DateTime('2015-02-15');
  1031. $mar15 = Carbon::parse('2015-03-15');
  1032. $this->assertSame('1 month after', $mar15->diffForHumans($feb15));
  1033. }
  1034. public function testDiffForHumansWithDateString()
  1035. {
  1036. $mar13 = Carbon::parse('2018-03-13');
  1037. $this->assertSame('1 month before', $mar13->diffForHumans('2018-04-13'));
  1038. }
  1039. public function testDiffForHumansWithDateTimeString()
  1040. {
  1041. $mar13 = Carbon::parse('2018-03-13');
  1042. $this->assertSame('1 month before', $mar13->diffForHumans('2018-04-13 08:00:00'));
  1043. }
  1044. public function testDiffWithString()
  1045. {
  1046. $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay();
  1047. $this->assertSame(383, $dt1->diffInHours('2000-01-10'));
  1048. }
  1049. public function testDiffWithDateTime()
  1050. {
  1051. $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay();
  1052. $dt2 = new DateTime('2000-01-10');
  1053. $this->assertSame(383, $dt1->diffInHours($dt2));
  1054. }
  1055. public function testDiffOptions()
  1056. {
  1057. $this->assertSame(1, Carbon::NO_ZERO_DIFF);
  1058. $this->assertSame(2, Carbon::JUST_NOW);
  1059. $this->assertSame(4, Carbon::ONE_DAY_WORDS);
  1060. $this->assertSame(8, Carbon::TWO_DAY_WORDS);
  1061. $this->assertSame(16, Carbon::SEQUENTIAL_PARTS_ONLY);
  1062. $options = Carbon::getHumanDiffOptions();
  1063. $this->assertSame(1, $options);
  1064. $date = Carbon::create(2018, 3, 12, 2, 5, 6, 'UTC');
  1065. $this->assertSame('1 second before', $date->diffForHumans($date));
  1066. Carbon::setHumanDiffOptions(0);
  1067. $this->assertSame(0, Carbon::getHumanDiffOptions());
  1068. $this->assertSame('0 seconds before', $date->diffForHumans($date));
  1069. Carbon::setLocale('fr');
  1070. $this->assertSame('0 seconde avant', $date->diffForHumans($date));
  1071. Carbon::setLocale('en');
  1072. Carbon::setHumanDiffOptions(Carbon::JUST_NOW);
  1073. $this->assertSame(2, Carbon::getHumanDiffOptions());
  1074. $this->assertSame('0 seconds before', $date->diffForHumans($date));
  1075. $this->assertSame('just now', Carbon::now()->diffForHumans());
  1076. Carbon::setHumanDiffOptions(Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS | Carbon::NO_ZERO_DIFF | Carbon::SEQUENTIAL_PARTS_ONLY);
  1077. $this->assertSame(29, Carbon::getHumanDiffOptions());
  1078. Carbon::disableHumanDiffOption(Carbon::SEQUENTIAL_PARTS_ONLY);
  1079. $oneDayAfter = Carbon::create(2018, 3, 13, 2, 5, 6, 'UTC');
  1080. $oneDayBefore = Carbon::create(2018, 3, 11, 2, 5, 6, 'UTC');
  1081. $twoDayAfter = Carbon::create(2018, 3, 14, 2, 5, 6, 'UTC');
  1082. $twoDayBefore = Carbon::create(2018, 3, 10, 2, 5, 6, 'UTC');
  1083. $this->assertSame('1 day after', $oneDayAfter->diffForHumans($date));
  1084. $this->assertSame('1 day before', $oneDayBefore->diffForHumans($date));
  1085. $this->assertSame('2 days after', $twoDayAfter->diffForHumans($date));
  1086. $this->assertSame('2 days before', $twoDayBefore->diffForHumans($date));
  1087. $this->assertSame('tomorrow', Carbon::now()->addDay()->diffForHumans());
  1088. $this->assertSame('yesterday', Carbon::now()->subDay()->diffForHumans());
  1089. $this->assertSame('after tomorrow', Carbon::now()->addDays(2)->diffForHumans());
  1090. $this->assertSame('before yesterday', Carbon::now()->subDays(2)->diffForHumans());
  1091. Carbon::disableHumanDiffOption(Carbon::TWO_DAY_WORDS);
  1092. $this->assertSame(5, Carbon::getHumanDiffOptions());
  1093. Carbon::disableHumanDiffOption(Carbon::TWO_DAY_WORDS);
  1094. $this->assertSame(5, Carbon::getHumanDiffOptions());
  1095. $this->assertSame('tomorrow', Carbon::now()->addDay()->diffForHumans());
  1096. $this->assertSame('yesterday', Carbon::now()->subDay()->diffForHumans());
  1097. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->diffForHumans());
  1098. $this->assertSame('2 days ago', Carbon::now()->subDays(2)->diffForHumans());
  1099. Carbon::enableHumanDiffOption(Carbon::JUST_NOW);
  1100. $this->assertSame(7, Carbon::getHumanDiffOptions());
  1101. Carbon::enableHumanDiffOption(Carbon::JUST_NOW);
  1102. $this->assertSame(7, Carbon::getHumanDiffOptions());
  1103. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1104. $comparison = Carbon::create(2019, 2, 4, 0, 0, 0, 'UTC');
  1105. $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
  1106. 'parts' => 2,
  1107. ]));
  1108. $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
  1109. 'parts' => 2,
  1110. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1111. ]));
  1112. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1113. $comparison = Carbon::create(2019, 2, 11, 0, 0, 0, 'UTC');
  1114. $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
  1115. 'parts' => 2,
  1116. ]));
  1117. $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
  1118. 'parts' => 2,
  1119. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1120. ]));
  1121. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1122. $comparison = Carbon::create(2019, 2, 12, 0, 0, 0, 'UTC');
  1123. $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
  1124. 'parts' => 2,
  1125. ]));
  1126. $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
  1127. 'parts' => 2,
  1128. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1129. ]));
  1130. $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [
  1131. 'parts' => 3,
  1132. ]));
  1133. $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [
  1134. 'parts' => 3,
  1135. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1136. ]));
  1137. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1138. $comparison = Carbon::create(2020, 1, 11, 0, 0, 0, 'UTC');
  1139. $this->assertSame('1 year 1 week before', $origin->diffForHumans($comparison, [
  1140. 'parts' => 2,
  1141. ]));
  1142. $this->assertSame('1 year before', $origin->diffForHumans($comparison, [
  1143. 'parts' => 2,
  1144. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1145. ]));
  1146. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1147. $comparison = Carbon::create(2019, 2, 5, 0, 0, 0, 'UTC');
  1148. $this->assertSame('1 month 1 day before', $origin->diffForHumans($comparison, [
  1149. 'parts' => 2,
  1150. ]));
  1151. $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
  1152. 'parts' => 2,
  1153. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1154. ]));
  1155. $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
  1156. $comparison = Carbon::create(2019, 1, 12, 0, 1, 0, 'UTC');
  1157. $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
  1158. 'parts' => 2,
  1159. ]));
  1160. $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
  1161. 'parts' => 2,
  1162. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1163. ]));
  1164. $this->assertSame('1 week 1 day 1 minute before', $origin->diffForHumans($comparison, [
  1165. 'parts' => 3,
  1166. ]));
  1167. $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
  1168. 'parts' => 3,
  1169. 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
  1170. ]));
  1171. Carbon::setHumanDiffOptions($options);
  1172. }
  1173. public function testDiffForHumansArrayParameter()
  1174. {
  1175. Carbon::setTestNow('2000-01-01 00:00:00');
  1176. $date = Carbon::now()->subtract('2 days, 3 hours and 40 minutes');
  1177. $this->assertSame('2 days and 3 hours ago', $date->diffForHumans([
  1178. 'parts' => 2,
  1179. 'join' => true,
  1180. ]));
  1181. $this->assertSame('hace 2 días y 3 horas', $date->copy()->locale('es')->diffForHumans([
  1182. 'parts' => 2,
  1183. 'join' => true,
  1184. ]));
  1185. $this->assertSame('2 days, 3 hours and 40 minutes ago', $date->diffForHumans([
  1186. 'parts' => -1,
  1187. 'join' => true,
  1188. ]));
  1189. $this->assertSame('3 days, 3 hours and 40 minutes before', $date->diffForHumans(Carbon::now()->addDay(), [
  1190. 'parts' => -1,
  1191. 'join' => true,
  1192. ]));
  1193. $this->assertSame('3 days, 3 hours and 40 minutes before', $date->diffForHumans([
  1194. 'other' => Carbon::now()->addDay(),
  1195. 'parts' => -1,
  1196. 'join' => true,
  1197. ]));
  1198. $this->assertSame('2 days, 3 hours ago', $date->diffForHumans([
  1199. 'parts' => 2,
  1200. 'join' => ', ',
  1201. ]));
  1202. $this->assertSame('2d, 3h ago', $date->diffForHumans([
  1203. 'parts' => 2,
  1204. 'join' => ', ',
  1205. 'short' => true,
  1206. ]));
  1207. $this->assertSame('2 days, 3 hours before', $date->diffForHumans([
  1208. 'parts' => 2,
  1209. 'join' => ', ',
  1210. 'syntax' => CarbonInterface::DIFF_RELATIVE_TO_OTHER,
  1211. ]));
  1212. $this->assertSame('yesterday', Carbon::yesterday()->diffForHumans([
  1213. 'options' => CarbonInterface::ONE_DAY_WORDS,
  1214. ]));
  1215. $this->assertSame('1 day ago', Carbon::yesterday()->diffForHumans([
  1216. 'options' => 0,
  1217. ]));
  1218. }
  1219. public function testFromNow()
  1220. {
  1221. Carbon::setLocale('en');
  1222. $this->assertSame('2 days from now', Carbon::now('UTC')->addDays(2)->fromNow());
  1223. Carbon::setLocale('fr');
  1224. $this->assertSame('dans 2 jours', Carbon::now('UTC')->addDays(2)->fromNow());
  1225. Carbon::setLocale('en');
  1226. $this->assertSame('2 days after', Carbon::now('UTC')->addDays(2)->fromNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1227. $this->assertSame('2d from now', Carbon::now('UTC')->addDays(2)->addHours(5)->fromNow(null, true));
  1228. $this->assertSame('2 days 5 hours', Carbon::now('UTC')->addDays(2)->addHours(5)->fromNow(true, false, 2));
  1229. }
  1230. public function testFromNowBackwardCompatibleSyntax()
  1231. {
  1232. $date = Carbon::parse('-5 days');
  1233. $this->assertSame('5 days', $date->fromNow(Carbon::now(), true));
  1234. $date = Carbon::parse('+5 days');
  1235. $this->assertSame('5 days', $date->fromNow(Carbon::now(), true));
  1236. }
  1237. public function testFrom()
  1238. {
  1239. Carbon::setLocale('en');
  1240. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->from());
  1241. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->from(null));
  1242. $this->assertSame('2 days after', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
  1243. Carbon::setLocale('fr');
  1244. $this->assertSame('2 jours après', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
  1245. Carbon::setLocale('en');
  1246. $this->assertSame('2 days from now', Carbon::now()->addDay()->from(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1247. $this->assertSame('2d after', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), null, true));
  1248. $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), true, false, 2));
  1249. }
  1250. public function testSince()
  1251. {
  1252. Carbon::setLocale('en');
  1253. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->since());
  1254. $this->assertSame('2 days from now', Carbon::now()->addDays(2)->since(null));
  1255. $this->assertSame('2 days after', Carbon::now()->addDay()->since(Carbon::now()->subDay()));
  1256. Carbon::setLocale('fr');
  1257. $this->assertSame('2 jours après', Carbon::now()->addDay()->since(Carbon::now()->subDay()));
  1258. Carbon::setLocale('en');
  1259. $this->assertSame('2 days from now', Carbon::now()->addDay()->since(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1260. $this->assertSame('2d after', Carbon::now()->addDay()->addHours(5)->since(Carbon::now()->subDay(), null, true));
  1261. $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->since(Carbon::now()->subDay(), true, false, 2));
  1262. }
  1263. public function testToNow()
  1264. {
  1265. Carbon::setLocale('en');
  1266. $this->assertSame('2 days ago', Carbon::now('UTC')->addDays(2)->toNow());
  1267. Carbon::setLocale('fr');
  1268. $this->assertSame('il y a 2 jours', Carbon::now('UTC')->addDays(2)->toNow());
  1269. Carbon::setLocale('en');
  1270. $this->assertSame('2 days before', Carbon::now('UTC')->addDays(2)->toNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
  1271. $this->assertSame('2d ago', Carbon::now('UTC')->addDays(2)->addHours(5)->toNow(null, true));
  1272. $this->assertSame('2 days 5 hours', Carbon::now('UTC')->addDays(2)->addHours(5)->toNow(true, false, 2));
  1273. }
  1274. public function testTo()
  1275. {
  1276. Carbon::setLocale('en');
  1277. $this->assertSame('2 days ago', Carbon::now()->addDays(2)->to());
  1278. $this->assertSame('2 days ago', Carbon::now()->addDays(2)->to(null));
  1279. $this->assertSame('2 days before', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
  1280. Carbon::setLocale('fr');
  1281. $this->assertSame('2 jours avant', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
  1282. Carbon::setLocale('en');
  1283. $this->assertSame('2 days ago', Carbon::now()->addDay()->to(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1284. $this->assertSame('2d before', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), null, true));
  1285. $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), true, false, 2));
  1286. }
  1287. public function testUntil()
  1288. {
  1289. Carbon::setLocale('en');
  1290. $this->assertSame('2 days ago', Carbon::now()->addDays(2)->until());
  1291. $this->assertSame('2 days ago', Carbon::now()->addDays(2)->until(null));
  1292. $this->assertSame('2 days before', Carbon::now()->addDay()->until(Carbon::now()->subDay()));
  1293. Carbon::setLocale('fr');
  1294. $this->assertSame('2 jours avant', Carbon::now()->addDay()->until(Carbon::now()->subDay()));
  1295. Carbon::setLocale('en');
  1296. $this->assertSame('2 days ago', Carbon::now()->addDay()->until(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
  1297. $this->assertSame('2d before', Carbon::now()->addDay()->addHours(5)->until(Carbon::now()->subDay(), null, true));
  1298. $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->until(Carbon::now()->subDay(), true, false, 2));
  1299. }
  1300. public function testDiffWithInvalidType()
  1301. {
  1302. $this->expectExceptionObject(new InvalidArgumentException(
  1303. 'Expected null, string, DateTime or DateTimeInterface, integer given'
  1304. ));
  1305. Carbon::createFromDate(2000, 1, 25)->diffInHours(10);
  1306. }
  1307. public function testDiffWithInvalidObject()
  1308. {
  1309. $this->expectExceptionObject(new InvalidArgumentException(
  1310. 'Expected null, string, DateTime or DateTimeInterface, Carbon\CarbonInterval given'
  1311. ));
  1312. Carbon::createFromDate(2000, 1, 25)->diffInHours(new CarbonInterval());
  1313. }
  1314. public function testDiffForHumansWithIncorrectDateTimeStringWhichIsNotACarbonInstance()
  1315. {
  1316. $this->expectExceptionObject(new InvalidFormatException(
  1317. 'Failed to parse time string (2018-04-13---08:00:00) at position 10'
  1318. ));
  1319. $mar13 = Carbon::parse('2018-03-13');
  1320. $mar13->diffForHumans('2018-04-13---08:00:00');
  1321. }
  1322. public function testFloatDiff()
  1323. {
  1324. date_default_timezone_set('UTC');
  1325. $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421')));
  1326. $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01')));
  1327. $this->assertSame(1.0006944444444443 / 7, Carbon::parse('2018-12-01 00:00')->floatDiffInWeeks(Carbon::parse('2018-12-02 00:01')));
  1328. $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421')));
  1329. $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456')));
  1330. $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false));
  1331. $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false));
  1332. $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421')));
  1333. $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421')));
  1334. $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456')));
  1335. $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false));
  1336. $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1337. $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456')));
  1338. $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421')));
  1339. $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421')));
  1340. $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421')));
  1341. $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456')));
  1342. $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false));
  1343. $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1344. $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421')));
  1345. $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456')));
  1346. $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false));
  1347. $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1348. $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')));
  1349. $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00')));
  1350. $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')));
  1351. $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false));
  1352. $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false));
  1353. $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false));
  1354. if (version_compare(PHP_VERSION, '7.2.0-dev', '>=')) {
  1355. $this->assertEqualsWithDelta(0.9999999999884259, Carbon::parse('2020-12-17 00:00:00.000001')->floatDiffInDays('2020-12-16 00:00:00.000002'), 0.0000000001);
  1356. }
  1357. }
  1358. public function testFloatDiffWithRealUnits()
  1359. {
  1360. date_default_timezone_set('UTC');
  1361. $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01')));
  1362. $this->assertSame(1.0006944444444443 / 7, Carbon::parse('2018-12-01 00:00')->floatDiffInRealWeeks(Carbon::parse('2018-12-02 00:01')));
  1363. $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421')));
  1364. $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456')));
  1365. $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false));
  1366. $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false));
  1367. $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421')));
  1368. $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421')));
  1369. $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456')));
  1370. $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false));
  1371. $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1372. $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456')));
  1373. $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421')));
  1374. $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421')));
  1375. $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421')));
  1376. $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456')));
  1377. $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false));
  1378. $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1379. $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421')));
  1380. $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456')));
  1381. $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false));
  1382. $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
  1383. $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')));
  1384. $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00')));
  1385. $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')));
  1386. $this->assertSame(-1, Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris'), false));
  1387. $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false));
  1388. $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris'), false));
  1389. }
  1390. /**
  1391. * https://bugs.php.net/bug.php?id=77007
  1392. * https://github.com/briannesbitt/Carbon/issues/1503
  1393. */
  1394. public function testPhpBug77007()
  1395. {
  1396. $this->assertSame(3, Carbon::now()->addMinutes(3)->diffInMinutes());
  1397. $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
  1398. $endDate = Carbon::parse('2018-10-11 20:59:07.237419');
  1399. $this->assertSame(0, $startDate->diffInSeconds($endDate));
  1400. $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
  1401. $endDate = Carbon::parse('2018-10-11 20:59:07.237419');
  1402. $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1403. $this->assertSame(0, $startDate->diffInSeconds($endDate));
  1404. $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
  1405. $endDate = Carbon::parse('2018-10-11 20:59:05.237419');
  1406. $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1407. $this->assertSame(1, $startDate->diffInSeconds($endDate));
  1408. $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1409. $this->assertSame(-1, $startDate->diffInSeconds($endDate, false));
  1410. $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
  1411. $endDate = Carbon::parse('2018-10-11 20:59:06.237419');
  1412. $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1413. $this->assertSame(0, $startDate->diffInSeconds($endDate));
  1414. $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1415. $this->assertSame(0, $startDate->diffInSeconds($endDate, false));
  1416. $startDate = Carbon::parse('2017-12-31 23:59:59.914653');
  1417. $endDate = Carbon::parse('2018-01-01 00:00:00.237419');
  1418. $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
  1419. $this->assertSame(0, $startDate->diffInSeconds($endDate));
  1420. }
  1421. public function testPHPBug80974()
  1422. {
  1423. $this->assertSame(3, Carbon::parse('2018-07-01 America/Toronto')->diff('2018-07-02 America/Vancouver')->h);
  1424. $this->assertSame(0, Carbon::parse('2018-07-01')->utc()->diff('2018-07-02')->days);
  1425. $this->assertSame(1, Carbon::parse('2018-07-01')->utc()->diff(Carbon::parse('2018-07-02'))->days);
  1426. $this->assertSame(1, Carbon::parse('2018-07-01')->diff(Carbon::parse('2018-07-02')->utc())->days);
  1427. }
  1428. public function testDiffWithZeroAndNonZeroMicroseconds()
  1429. {
  1430. $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00');
  1431. $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00');
  1432. $this->assertSame(0, $serverTime->diffInSeconds($requestTime));
  1433. $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00');
  1434. $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00');
  1435. $this->assertSame(0, $serverTime->diffInSeconds($requestTime));
  1436. }
  1437. public function testNearlyFullDayDiffInSeconds()
  1438. {
  1439. $d1 = Carbon::parse('2019-06-15 12:34:56.123456');
  1440. $d2 = Carbon::parse('2019-06-16 12:34:56.123455');
  1441. // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12:
  1442. $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<')
  1443. ? 86400 // Bad rounding before PHP 7.2.12
  1444. : 86399; // Exact result since PHP 7.2.12
  1445. $this->assertSame($expected, $d2->diffInSeconds($d1));
  1446. }
  1447. public function testNearlyFullDayDiffInMicroseconds()
  1448. {
  1449. $d1 = Carbon::parse('2019-06-15 12:34:56.123456');
  1450. $d2 = Carbon::parse('2019-06-16 12:34:56.123455');
  1451. $this->assertSame(86399999999, $d2->diffInMicroseconds($d1));
  1452. }
  1453. public function testExactMonthDiffInSeconds()
  1454. {
  1455. $d1 = Carbon::make('2019-01-23 12:00:00');
  1456. $d2 = Carbon::make('2019-02-23 12:00:00');
  1457. $this->assertSame(2678400, $d2->diffInSeconds($d1));
  1458. }
  1459. public function testDaysDiffPreservation()
  1460. {
  1461. $deletedDate = Carbon::now()->startOfDay()->addDays(31);
  1462. $this->assertSame('31 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay(), [
  1463. 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
  1464. 'skip' => ['m', 'w'],
  1465. 'minimumUnit' => 'd',
  1466. ]));
  1467. $this->assertSame('31 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay()->subHours(5), [
  1468. 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
  1469. 'skip' => ['m', 'w'],
  1470. 'minimumUnit' => 'd',
  1471. ]));
  1472. $this->assertSame('30 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay()->addHours(5), [
  1473. 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
  1474. 'skip' => ['m', 'w'],
  1475. 'minimumUnit' => 'd',
  1476. ]));
  1477. }
  1478. }