1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of the Carbon package.
- *
- * (c) Brian Nesbitt <brian@nesbot.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Tests\CarbonImmutable;
- use Carbon\CarbonImmutable as Carbon;
- use Carbon\CarbonInterface;
- use Carbon\CarbonInterval;
- use Carbon\Exceptions\InvalidFormatException;
- use Closure;
- use DateTime;
- use InvalidArgumentException;
- use Tests\AbstractTestCase;
- class DiffTest extends AbstractTestCase
- {
- public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null)
- {
- parent::wrapWithTestNow($func, $dt ?: Carbon::createMidnightDate(2012, 1, 1));
- }
- public function testDiffAsCarbonInterval()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertCarbonInterval($dt->diffAsCarbonInterval($dt->copy()->addYear()), 1, 0, 0, 0, 0, 0);
- }
- public function testDiffInYearsPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()));
- }
- public function testDiffInYearsNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false));
- }
- public function testDiffInYearsNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear()));
- }
- public function testDiffInYearsVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(1, Carbon::now()->subYear()->diffInYears());
- });
- }
- public function testDiffInYearsEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7)));
- }
- public function testDiffInMonthsPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth()));
- }
- public function testDiffInMonthsNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false));
- }
- public function testDiffInMonthsNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth()));
- }
- public function testDiffInMonthsVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(12, Carbon::now()->subYear()->diffInMonths());
- });
- }
- public function testDiffInMonthsEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16)));
- }
- public function testDiffInDaysPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear()));
- }
- public function testDiffInDaysNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false));
- }
- public function testDiffInDaysNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear()));
- }
- public function testDiffInDaysVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(7, Carbon::now()->subWeek()->diffInDays());
- });
- }
- public function testDiffInDaysEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13)));
- }
- public function testDiffInDaysFilteredPositiveWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === 1;
- }, $dt->copy()->endOfMonth()));
- }
- public function testDiffInDaysFilteredPositiveWithSecondObject()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 1);
- $dt2 = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === Carbon::SUNDAY;
- }, $dt2));
- }
- public function testDiffInDaysFilteredNegativeNoSignWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(5, $dt->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === Carbon::SUNDAY;
- }, $dt->copy()->startOfMonth()));
- }
- public function testDiffInDaysFilteredNegativeNoSignWithSecondObject()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 31);
- $dt2 = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(5, $dt1->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === Carbon::SUNDAY;
- }, $dt2));
- }
- public function testDiffInDaysFilteredNegativeWithSignWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(-5, $dt->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === 1;
- }, $dt->copy()->startOfMonth(), false));
- }
- public function testDiffInDaysFilteredNegativeWithSignWithSecondObject()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 31);
- $dt2 = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-5, $dt1->diffInDaysFiltered(function (Carbon $date) {
- return $date->dayOfWeek === Carbon::SUNDAY;
- }, $dt2, false));
- }
- public function testDiffInHoursFiltered()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay();
- $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
- $this->assertSame(31, $dt1->diffInHoursFiltered(function (Carbon $date) {
- return $date->hour === 9;
- }, $dt2));
- }
- public function testDiffInHoursFilteredNegative()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 31)->endOfDay();
- $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
- $this->assertSame(-31, $dt1->diffInHoursFiltered(function (Carbon $date) {
- return $date->hour === 9;
- }, $dt2, false));
- }
- public function testDiffInHoursFilteredWorkHoursPerWeek()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 5)->endOfDay();
- $dt2 = Carbon::createFromDate(2000, 1, 1)->startOfDay();
- $this->assertSame(40, $dt1->diffInHoursFiltered(function (Carbon $date) {
- return $date->hour > 8 && $date->hour < 17;
- }, $dt2));
- }
- public function testDiffFilteredUsingMinutesPositiveWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1)->startOfDay();
- $this->assertSame(60, $dt->diffFiltered(CarbonInterval::minute(), function (Carbon $date) {
- return $date->hour === 12;
- }, Carbon::createFromDate(2000, 1, 1)->endOfDay()));
- }
- public function testDiffFilteredPositiveWithSecondObject()
- {
- $dt1 = Carbon::create(2000, 1, 1);
- $dt2 = $dt1->copy()->addSeconds(80);
- $this->assertSame(40, $dt1->diffFiltered(CarbonInterval::second(), function (Carbon $date) {
- return $date->second % 2 === 0;
- }, $dt2));
- }
- public function testDiffFilteredNegativeNoSignWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(2, $dt->diffFiltered(CarbonInterval::days(2), function (Carbon $date) {
- return $date->dayOfWeek === Carbon::SUNDAY;
- }, $dt->copy()->startOfMonth()));
- }
- public function testDiffFilteredNegativeNoSignWithSecondObject()
- {
- $dt1 = Carbon::createFromDate(2006, 1, 31);
- $dt2 = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(7, $dt1->diffFiltered(CarbonInterval::year(), function (Carbon $date) {
- return $date->month === 1;
- }, $dt2));
- }
- public function testDiffFilteredNegativeWithSignWithMutated()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(-4, $dt->diffFiltered(CarbonInterval::week(), function (Carbon $date) {
- return $date->month === 12;
- }, $dt->copy()->subMonths(3), false));
- }
- public function testDiffFilteredNegativeWithSignWithSecondObject()
- {
- $dt1 = Carbon::createFromDate(2001, 1, 31);
- $dt2 = Carbon::createFromDate(1999, 1, 1);
- $this->assertSame(-12, $dt1->diffFiltered(CarbonInterval::month(), function (Carbon $date) {
- return $date->year === 2000;
- }, $dt2, false));
- }
- public function testBug188DiffWithSameDates()
- {
- $start = Carbon::create(2014, 10, 8, 15, 20, 0);
- $end = $start->copy();
- $this->assertSame(0, $start->diffInDays($end));
- $this->assertSame(0, $start->diffInWeekdays($end));
- }
- public function testBug188DiffWithDatesOnlyHoursApart()
- {
- $start = Carbon::create(2014, 10, 8, 15, 20, 0);
- $end = $start->copy();
- $this->assertSame(0, $start->diffInDays($end));
- $this->assertSame(0, $start->diffInWeekdays($end));
- }
- public function testBug188DiffWithSameDates1DayApart()
- {
- $start = Carbon::create(2014, 10, 8, 15, 20, 0);
- $end = $start->copy()->addDay();
- $this->assertSame(1, $start->diffInDays($end));
- $this->assertSame(1, $start->diffInWeekdays($end));
- }
- public function testBug188DiffWithDatesOnTheWeekend()
- {
- $start = Carbon::create(2014, 1, 1, 0, 0, 0);
- $start = $start->next(Carbon::SATURDAY);
- $end = $start->copy()->addDay();
- $this->assertSame(1, $start->diffInDays($end));
- $this->assertSame(0, $start->diffInWeekdays($end));
- }
- public function testDiffInWeekdaysPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(21, $dt->diffInWeekdays($dt->copy()->addMonth()));
- }
- public function testDiffInWeekdaysNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(20, $dt->diffInWeekdays($dt->copy()->startOfMonth()));
- }
- public function testDiffInWeekdaysNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(-20, $dt->diffInWeekdays($dt->copy()->startOfMonth(), false));
- }
- public function testDiffInWeekendDaysPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->endOfMonth()));
- }
- public function testDiffInWeekendDaysNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(10, $dt->diffInWeekendDays($dt->copy()->startOfMonth()));
- }
- public function testDiffInWeekendDaysNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 31);
- $this->assertSame(-10, $dt->diffInWeekendDays($dt->copy()->startOfMonth(), false));
- }
- public function testDiffInWeeksPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(52, $dt->diffInWeeks($dt->copy()->addYear()));
- }
- public function testDiffInWeeksNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-52, $dt->diffInWeeks($dt->copy()->subYear(), false));
- }
- public function testDiffInWeeksNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(52, $dt->diffInWeeks($dt->copy()->subYear()));
- }
- public function testDiffInWeeksVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(1, Carbon::now()->subWeek()->diffInWeeks());
- });
- }
- public function testDiffInWeeksEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(0, $dt->diffInWeeks($dt->copy()->addWeek()->subDay()));
- }
- public function testDiffInHoursPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2)));
- }
- public function testDiffInHoursNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false));
- }
- public function testDiffInHoursNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2)));
- }
- public function testDiffInHoursVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours());
- }, Carbon::create(2012, 1, 15));
- }
- public function testDiffInHoursEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31)));
- }
- public function testDiffInHoursWithTimezones()
- {
- date_default_timezone_set('Africa/Algiers');
- Carbon::setTestNow();
- $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto');
- $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver');
- $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver');
- $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');
- sleep(2);
- $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');
- $this->assertSame(0, $dtVancouver->diffInHours($dtToronto) % 24);
- $dtToronto = Carbon::createMidnightDate(2012, 1, 1, 'America/Toronto');
- $dtVancouver = Carbon::createMidnightDate(2012, 1, 1, 'America/Vancouver');
- $this->assertSame(3, $dtVancouver->diffInHours($dtToronto), 'Midnight in Toronto is 3 hours from midnight in Vancouver');
- }
- public function testDiffInMinutesPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2)));
- }
- public function testDiffInMinutesPositiveALot()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2)));
- }
- public function testDiffInMinutesNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false));
- }
- public function testDiffInMinutesNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2)));
- }
- public function testDiffInMinutesVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes());
- });
- }
- public function testDiffInMinutesEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31)));
- }
- public function testDiffInSecondsPositive()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2)));
- }
- public function testDiffInSecondsPositiveALot()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2)));
- }
- public function testDiffInSecondsNegativeWithSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false));
- }
- public function testDiffInSecondsNegativeNoSign()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2)));
- }
- public function testDiffInSecondsVsDefaultNow()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds());
- });
- }
- public function testDiffInSecondsEnsureIsTruncated()
- {
- $dt = Carbon::createFromDate(2000, 1, 1);
- $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9)));
- }
- public function testDiffInSecondsWithTimezones()
- {
- $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
- $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
- $this->assertSame(0, $dtOttawa->diffInSeconds($dtVancouver) % (24 * 3600));
- $dtOttawa = Carbon::createMidnightDate(2000, 1, 1, 'America/Toronto');
- $dtVancouver = Carbon::createMidnightDate(2000, 1, 1, 'America/Vancouver');
- $this->assertSame(3 * 60 * 60, $dtOttawa->diffInSeconds($dtVancouver));
- }
- public function testDiffInSecondsWithTimezonesAndVsDefault()
- {
- $vanNow = Carbon::now('America/Vancouver');
- $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz);
- $this->wrapWithTestNow(function () use ($vanNow) {
- $this->assertSame(0, $vanNow->diffInSeconds());
- }, $hereNow);
- }
- public function testDiffForHumansNowAndSecond()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 second ago', Carbon::now()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndSecondWithTimezone()
- {
- $vanNow = Carbon::now('America/Vancouver');
- $hereNow = $vanNow->copy()->setTimezone(Carbon::now()->tz);
- $this->wrapWithTestNow(function () use ($vanNow) {
- $this->assertSame('1 second ago', $vanNow->diffForHumans());
- }, $hereNow);
- }
- public function testDiffForHumansNowAndSeconds()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 seconds ago', Carbon::now()->subSeconds(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 seconds ago', Carbon::now()->subSeconds(59)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 minute ago', Carbon::now()->subMinute()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndMinutes()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 minutes ago', Carbon::now()->subMinutes(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 minutes ago', Carbon::now()->subMinutes(59)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 hour ago', Carbon::now()->subHour()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndHours()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 hours ago', Carbon::now()->subHours(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('23 hours ago', Carbon::now()->subHours(23)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 day ago', Carbon::now()->subDay()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndDays()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 days ago', Carbon::now()->subDays(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('6 days ago', Carbon::now()->subDays(6)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 week ago', Carbon::now()->subWeek()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndWeeks()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 weeks ago', Carbon::now()->subWeeks(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('3 weeks ago', Carbon::now()->subWeeks(3)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndMonth()
- {
- Carbon::setTestNow('2018-12-01');
- $this->assertSame('4 weeks ago', Carbon::now()->subWeeks(4)->diffForHumans());
- $this->assertSame('1 month ago', Carbon::now()->subMonth()->diffForHumans());
- }
- public function testDiffForHumansNowAndMonths()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 months ago', Carbon::now()->subMonthsNoOverflow(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('11 months ago', Carbon::now()->subMonthsNoOverflow(11)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year ago', Carbon::now()->subYear()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndYears()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 years ago', Carbon::now()->subYears(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureSecond()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 second from now', Carbon::now()->addSecond()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureSeconds()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 seconds from now', Carbon::now()->addSeconds(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 seconds from now', Carbon::now()->addSeconds(59)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 minute from now', Carbon::now()->addMinute()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureMinutes()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 minutes from now', Carbon::now()->addMinutes(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 minutes from now', Carbon::now()->addMinutes(59)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 hour from now', Carbon::now()->addHour()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureHours()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 hours from now', Carbon::now()->addHours(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('23 hours from now', Carbon::now()->addHours(23)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 day from now', Carbon::now()->addDay()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureDays()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('6 days from now', Carbon::now()->addDays(6)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 week from now', Carbon::now()->addWeek()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureWeeks()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 weeks from now', Carbon::now()->addWeeks(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('3 weeks from now', Carbon::now()->addWeeks(3)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('4 weeks from now', Carbon::now()->addWeeks(4)->diffForHumans());
- $this->assertSame('1 month from now', Carbon::now()->addMonth()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureMonths()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 months from now', Carbon::now()->addMonths(2)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndNearlyFutureYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('11 months from now', Carbon::now()->addMonths(11)->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year from now', Carbon::now()->addYear()->diffForHumans());
- });
- }
- public function testDiffForHumansNowAndFutureYears()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 years from now', Carbon::now()->addYears(2)->diffForHumans());
- });
- }
- public function testDiffForHumansOtherAndSecond()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 second before', Carbon::now()->diffForHumans(Carbon::now()->addSecond()));
- });
- }
- public function testDiffForHumansOtherAndSeconds()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 seconds before', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 seconds before', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(59)));
- });
- }
- public function testDiffForHumansOtherAndMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 minute before', Carbon::now()->diffForHumans(Carbon::now()->addMinute()));
- });
- }
- public function testDiffForHumansOtherAndMinutes()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 minutes before', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 minutes before', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(59)));
- });
- }
- public function testDiffForHumansOtherAndHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 hour before', Carbon::now()->diffForHumans(Carbon::now()->addHour()));
- });
- }
- public function testDiffForHumansOtherAndHours()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 hours before', Carbon::now()->diffForHumans(Carbon::now()->addHours(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('23 hours before', Carbon::now()->diffForHumans(Carbon::now()->addHours(23)));
- });
- }
- public function testDiffForHumansOtherAndDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 day before', Carbon::now()->diffForHumans(Carbon::now()->addDay()));
- });
- }
- public function testDiffForHumansOtherAndDays()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 days before', Carbon::now()->diffForHumans(Carbon::now()->addDays(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('6 days before', Carbon::now()->diffForHumans(Carbon::now()->addDays(6)));
- });
- }
- public function testDiffForHumansOverWeekWithDefaultPartsCount()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 week ago', Carbon::now()->subDays(8)->diffForHumans());
- });
- }
- public function testDiffForHumansOverWeekWithPartsCount1()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(
- '1 week ago',
- Carbon::now()->subDays(8)->diffForHumans(null, false, false, 1)
- );
- });
- }
- public function testDiffForHumansOverWeekWithPartsCount2()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(
- '1 week 1 day ago',
- Carbon::now()->subDays(8)->diffForHumans(null, false, false, 2)
- );
- });
- }
- public function testDiffForHumansOverWeekWithMicrosecondsBuggyGap()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame(
- '23 hours 59 minutes 59 seconds after',
- Carbon::parse('2018-12-03 12:34:45.123456')
- ->diffForHumans('2018-12-02 12:34:45.123476', ['parts' => 3])
- );
- });
- }
- public function testDiffForHumansOtherAndWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 week before', Carbon::now()->diffForHumans(Carbon::now()->addWeek()));
- });
- }
- public function testDiffForHumansOtherAndWeeks()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('3 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(3)));
- });
- }
- public function testDiffForHumansOtherAndMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('4 weeks before', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(4)));
- $this->assertSame('1 month before', Carbon::now()->diffForHumans(Carbon::now()->addMonth()));
- });
- }
- public function testDiffForHumansOtherAndMonths()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 months before', Carbon::now()->diffForHumans(Carbon::now()->addMonths(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('11 months before', Carbon::now()->diffForHumans(Carbon::now()->addMonths(11)));
- });
- }
- public function testDiffForHumansOtherAndYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYear()));
- });
- }
- public function testDiffForHumansOtherAndYears()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 years before', Carbon::now()->diffForHumans(Carbon::now()->addYears(2)));
- });
- }
- public function testDiffForHumansOtherAndFutureSecond()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 second after', Carbon::now()->diffForHumans(Carbon::now()->subSecond()));
- });
- }
- public function testDiffForHumansOtherAndFutureSeconds()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 seconds after', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 seconds after', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(59)));
- });
- }
- public function testDiffForHumansOtherAndFutureMinute()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 minute after', Carbon::now()->diffForHumans(Carbon::now()->subMinute()));
- });
- }
- public function testDiffForHumansOtherAndFutureMinutes()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 minutes after', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 minutes after', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(59)));
- });
- }
- public function testDiffForHumansOtherAndFutureHour()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 hour after', Carbon::now()->diffForHumans(Carbon::now()->subHour()));
- });
- }
- public function testDiffForHumansOtherAndFutureHours()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 hours after', Carbon::now()->diffForHumans(Carbon::now()->subHours(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('23 hours after', Carbon::now()->diffForHumans(Carbon::now()->subHours(23)));
- });
- }
- public function testDiffForHumansOtherAndFutureDay()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 day after', Carbon::now()->diffForHumans(Carbon::now()->subDay()));
- });
- }
- public function testDiffForHumansOtherAndFutureDays()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 days after', Carbon::now()->diffForHumans(Carbon::now()->subDays(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('6 days after', Carbon::now()->diffForHumans(Carbon::now()->subDays(6)));
- });
- }
- public function testDiffForHumansOtherAndFutureWeek()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 week after', Carbon::now()->diffForHumans(Carbon::now()->subWeek()));
- });
- }
- public function testDiffForHumansOtherAndFutureWeeks()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureMonth()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('3 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(3)));
- });
- }
- public function testDiffForHumansOtherAndFutureMonth()
- {
- Carbon::setTestNow('2018-12-01');
- $this->assertSame('4 weeks after', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(4)));
- $this->assertSame('1 month after', Carbon::now()->diffForHumans(Carbon::now()->subMonth()));
- }
- public function testDiffForHumansOtherAndFutureMonths()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(2)));
- });
- }
- public function testDiffForHumansOtherAndNearlyFutureYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('11 months after', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(11)));
- });
- }
- public function testDiffForHumansOtherAndFutureYear()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYear()));
- });
- }
- public function testDiffForHumansOtherAndFutureYears()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 years after', Carbon::now()->diffForHumans(Carbon::now()->subYears(2)));
- });
- }
- public function testDiffForHumansAbsoluteSeconds()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('59 seconds', Carbon::now()->diffForHumans(Carbon::now()->subSeconds(59), true));
- $this->assertSame('59 seconds', Carbon::now()->diffForHumans(Carbon::now()->addSeconds(59), true));
- });
- }
- public function testDiffForHumansAbsoluteMinutes()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('30 minutes', Carbon::now()->diffForHumans(Carbon::now()->subMinutes(30), true));
- $this->assertSame('30 minutes', Carbon::now()->diffForHumans(Carbon::now()->addMinutes(30), true));
- });
- }
- public function testDiffForHumansAbsoluteHours()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('3 hours', Carbon::now()->diffForHumans(Carbon::now()->subHours(3), true));
- $this->assertSame('3 hours', Carbon::now()->diffForHumans(Carbon::now()->addHours(3), true));
- });
- }
- public function testDiffForHumansAbsoluteDays()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 days', Carbon::now()->diffForHumans(Carbon::now()->subDays(2), true));
- $this->assertSame('2 days', Carbon::now()->diffForHumans(Carbon::now()->addDays(2), true));
- });
- }
- public function testDiffForHumansAbsoluteWeeks()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 weeks', Carbon::now()->diffForHumans(Carbon::now()->subWeeks(2), true));
- $this->assertSame('2 weeks', Carbon::now()->diffForHumans(Carbon::now()->addWeeks(2), true));
- });
- }
- public function testDiffForHumansAbsoluteMonths()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('2 months', Carbon::now()->diffForHumans(Carbon::now()->subMonthsNoOverflow(2), true));
- $this->assertSame('2 months', Carbon::now()->diffForHumans(Carbon::now()->addMonthsNoOverflow(2), true));
- });
- }
- public function testDiffForHumansAbsoluteYears()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), true));
- $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), true));
- });
- }
- public function testDiffForHumansWithOptions()
- {
- $this->wrapWithTestNow(function () {
- $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_ABSOLUTE));
- $this->assertSame('1 year', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_ABSOLUTE));
- $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_AUTO));
- $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_AUTO));
- $this->assertSame('1 year from now', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('1 year ago', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('1 year after', Carbon::now()->diffForHumans(Carbon::now()->subYears(1), CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- $this->assertSame('1 year before', Carbon::now()->diffForHumans(Carbon::now()->addYears(1), CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- $this->assertSame('1 year', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_ABSOLUTE));
- $this->assertSame('1 year', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_ABSOLUTE));
- $this->assertSame('1 year ago', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_AUTO));
- $this->assertSame('1 year from now', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_AUTO));
- $this->assertSame('1 year ago', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('1 year from now', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('1 year before', Carbon::now()->subYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- $this->assertSame('1 year after', Carbon::now()->addYears(1)->diffForHumans(null, CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- });
- }
- public function testDiffForHumansWithShorterMonthShouldStillBeAMonth()
- {
- $feb15 = Carbon::parse('2015-02-15');
- $mar15 = Carbon::parse('2015-03-15');
- $this->assertSame('1 month after', $mar15->diffForHumans($feb15));
- }
- public function testDiffForHumansWithDateTimeInstance()
- {
- $feb15 = new DateTime('2015-02-15');
- $mar15 = Carbon::parse('2015-03-15');
- $this->assertSame('1 month after', $mar15->diffForHumans($feb15));
- }
- public function testDiffForHumansWithDateString()
- {
- $mar13 = Carbon::parse('2018-03-13');
- $this->assertSame('1 month before', $mar13->diffForHumans('2018-04-13'));
- }
- public function testDiffForHumansWithDateTimeString()
- {
- $mar13 = Carbon::parse('2018-03-13');
- $this->assertSame('1 month before', $mar13->diffForHumans('2018-04-13 08:00:00'));
- }
- public function testDiffWithString()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay();
- $this->assertSame(383, $dt1->diffInHours('2000-01-10'));
- }
- public function testDiffWithDateTime()
- {
- $dt1 = Carbon::createFromDate(2000, 1, 25)->endOfDay();
- $dt2 = new DateTime('2000-01-10');
- $this->assertSame(383, $dt1->diffInHours($dt2));
- }
- public function testDiffOptions()
- {
- $this->assertSame(1, Carbon::NO_ZERO_DIFF);
- $this->assertSame(2, Carbon::JUST_NOW);
- $this->assertSame(4, Carbon::ONE_DAY_WORDS);
- $this->assertSame(8, Carbon::TWO_DAY_WORDS);
- $this->assertSame(16, Carbon::SEQUENTIAL_PARTS_ONLY);
- $options = Carbon::getHumanDiffOptions();
- $this->assertSame(1, $options);
- $date = Carbon::create(2018, 3, 12, 2, 5, 6, 'UTC');
- $this->assertSame('1 second before', $date->diffForHumans($date));
- Carbon::setHumanDiffOptions(0);
- $this->assertSame(0, Carbon::getHumanDiffOptions());
- $this->assertSame('0 seconds before', $date->diffForHumans($date));
- Carbon::setLocale('fr');
- $this->assertSame('0 seconde avant', $date->diffForHumans($date));
- Carbon::setLocale('en');
- Carbon::setHumanDiffOptions(Carbon::JUST_NOW);
- $this->assertSame(2, Carbon::getHumanDiffOptions());
- $this->assertSame('0 seconds before', $date->diffForHumans($date));
- $this->assertSame('just now', Carbon::now()->diffForHumans());
- Carbon::setHumanDiffOptions(Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS | Carbon::NO_ZERO_DIFF | Carbon::SEQUENTIAL_PARTS_ONLY);
- $this->assertSame(29, Carbon::getHumanDiffOptions());
- Carbon::disableHumanDiffOption(Carbon::SEQUENTIAL_PARTS_ONLY);
- $oneDayAfter = Carbon::create(2018, 3, 13, 2, 5, 6, 'UTC');
- $oneDayBefore = Carbon::create(2018, 3, 11, 2, 5, 6, 'UTC');
- $twoDayAfter = Carbon::create(2018, 3, 14, 2, 5, 6, 'UTC');
- $twoDayBefore = Carbon::create(2018, 3, 10, 2, 5, 6, 'UTC');
- $this->assertSame('1 day after', $oneDayAfter->diffForHumans($date));
- $this->assertSame('1 day before', $oneDayBefore->diffForHumans($date));
- $this->assertSame('2 days after', $twoDayAfter->diffForHumans($date));
- $this->assertSame('2 days before', $twoDayBefore->diffForHumans($date));
- $this->assertSame('tomorrow', Carbon::now()->addDay()->diffForHumans());
- $this->assertSame('yesterday', Carbon::now()->subDay()->diffForHumans());
- $this->assertSame('after tomorrow', Carbon::now()->addDays(2)->diffForHumans());
- $this->assertSame('before yesterday', Carbon::now()->subDays(2)->diffForHumans());
- Carbon::disableHumanDiffOption(Carbon::TWO_DAY_WORDS);
- $this->assertSame(5, Carbon::getHumanDiffOptions());
- Carbon::disableHumanDiffOption(Carbon::TWO_DAY_WORDS);
- $this->assertSame(5, Carbon::getHumanDiffOptions());
- $this->assertSame('tomorrow', Carbon::now()->addDay()->diffForHumans());
- $this->assertSame('yesterday', Carbon::now()->subDay()->diffForHumans());
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->diffForHumans());
- $this->assertSame('2 days ago', Carbon::now()->subDays(2)->diffForHumans());
- Carbon::enableHumanDiffOption(Carbon::JUST_NOW);
- $this->assertSame(7, Carbon::getHumanDiffOptions());
- Carbon::enableHumanDiffOption(Carbon::JUST_NOW);
- $this->assertSame(7, Carbon::getHumanDiffOptions());
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2019, 2, 4, 0, 0, 0, 'UTC');
- $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2019, 2, 11, 0, 0, 0, 'UTC');
- $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2019, 2, 12, 0, 0, 0, 'UTC');
- $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 month 1 week before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 3,
- ]));
- $this->assertSame('1 month 1 week 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 3,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2020, 1, 11, 0, 0, 0, 'UTC');
- $this->assertSame('1 year 1 week before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 year before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2019, 2, 5, 0, 0, 0, 'UTC');
- $this->assertSame('1 month 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 month before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $origin = Carbon::create(2019, 1, 4, 0, 0, 0, 'UTC');
- $comparison = Carbon::create(2019, 1, 12, 0, 1, 0, 'UTC');
- $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- ]));
- $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 2,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- $this->assertSame('1 week 1 day 1 minute before', $origin->diffForHumans($comparison, [
- 'parts' => 3,
- ]));
- $this->assertSame('1 week 1 day before', $origin->diffForHumans($comparison, [
- 'parts' => 3,
- 'options' => CarbonInterface::SEQUENTIAL_PARTS_ONLY,
- ]));
- Carbon::setHumanDiffOptions($options);
- }
- public function testDiffForHumansArrayParameter()
- {
- Carbon::setTestNow('2000-01-01 00:00:00');
- $date = Carbon::now()->subtract('2 days, 3 hours and 40 minutes');
- $this->assertSame('2 days and 3 hours ago', $date->diffForHumans([
- 'parts' => 2,
- 'join' => true,
- ]));
- $this->assertSame('hace 2 días y 3 horas', $date->copy()->locale('es')->diffForHumans([
- 'parts' => 2,
- 'join' => true,
- ]));
- $this->assertSame('2 days, 3 hours and 40 minutes ago', $date->diffForHumans([
- 'parts' => -1,
- 'join' => true,
- ]));
- $this->assertSame('3 days, 3 hours and 40 minutes before', $date->diffForHumans(Carbon::now()->addDay(), [
- 'parts' => -1,
- 'join' => true,
- ]));
- $this->assertSame('3 days, 3 hours and 40 minutes before', $date->diffForHumans([
- 'other' => Carbon::now()->addDay(),
- 'parts' => -1,
- 'join' => true,
- ]));
- $this->assertSame('2 days, 3 hours ago', $date->diffForHumans([
- 'parts' => 2,
- 'join' => ', ',
- ]));
- $this->assertSame('2d, 3h ago', $date->diffForHumans([
- 'parts' => 2,
- 'join' => ', ',
- 'short' => true,
- ]));
- $this->assertSame('2 days, 3 hours before', $date->diffForHumans([
- 'parts' => 2,
- 'join' => ', ',
- 'syntax' => CarbonInterface::DIFF_RELATIVE_TO_OTHER,
- ]));
- $this->assertSame('yesterday', Carbon::yesterday()->diffForHumans([
- 'options' => CarbonInterface::ONE_DAY_WORDS,
- ]));
- $this->assertSame('1 day ago', Carbon::yesterday()->diffForHumans([
- 'options' => 0,
- ]));
- }
- public function testFromNow()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days from now', Carbon::now('UTC')->addDays(2)->fromNow());
- Carbon::setLocale('fr');
- $this->assertSame('dans 2 jours', Carbon::now('UTC')->addDays(2)->fromNow());
- Carbon::setLocale('en');
- $this->assertSame('2 days after', Carbon::now('UTC')->addDays(2)->fromNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- $this->assertSame('2d from now', Carbon::now('UTC')->addDays(2)->addHours(5)->fromNow(null, true));
- $this->assertSame('2 days 5 hours', Carbon::now('UTC')->addDays(2)->addHours(5)->fromNow(true, false, 2));
- }
- public function testFromNowBackwardCompatibleSyntax()
- {
- $date = Carbon::parse('-5 days');
- $this->assertSame('5 days', $date->fromNow(Carbon::now(), true));
- $date = Carbon::parse('+5 days');
- $this->assertSame('5 days', $date->fromNow(Carbon::now(), true));
- }
- public function testFrom()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->from());
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->from(null));
- $this->assertSame('2 days after', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
- Carbon::setLocale('fr');
- $this->assertSame('2 jours après', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
- Carbon::setLocale('en');
- $this->assertSame('2 days from now', Carbon::now()->addDay()->from(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('2d after', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), null, true));
- $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), true, false, 2));
- }
- public function testSince()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->since());
- $this->assertSame('2 days from now', Carbon::now()->addDays(2)->since(null));
- $this->assertSame('2 days after', Carbon::now()->addDay()->since(Carbon::now()->subDay()));
- Carbon::setLocale('fr');
- $this->assertSame('2 jours après', Carbon::now()->addDay()->since(Carbon::now()->subDay()));
- Carbon::setLocale('en');
- $this->assertSame('2 days from now', Carbon::now()->addDay()->since(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('2d after', Carbon::now()->addDay()->addHours(5)->since(Carbon::now()->subDay(), null, true));
- $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->since(Carbon::now()->subDay(), true, false, 2));
- }
- public function testToNow()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days ago', Carbon::now('UTC')->addDays(2)->toNow());
- Carbon::setLocale('fr');
- $this->assertSame('il y a 2 jours', Carbon::now('UTC')->addDays(2)->toNow());
- Carbon::setLocale('en');
- $this->assertSame('2 days before', Carbon::now('UTC')->addDays(2)->toNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
- $this->assertSame('2d ago', Carbon::now('UTC')->addDays(2)->addHours(5)->toNow(null, true));
- $this->assertSame('2 days 5 hours', Carbon::now('UTC')->addDays(2)->addHours(5)->toNow(true, false, 2));
- }
- public function testTo()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days ago', Carbon::now()->addDays(2)->to());
- $this->assertSame('2 days ago', Carbon::now()->addDays(2)->to(null));
- $this->assertSame('2 days before', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
- Carbon::setLocale('fr');
- $this->assertSame('2 jours avant', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
- Carbon::setLocale('en');
- $this->assertSame('2 days ago', Carbon::now()->addDay()->to(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('2d before', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), null, true));
- $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), true, false, 2));
- }
- public function testUntil()
- {
- Carbon::setLocale('en');
- $this->assertSame('2 days ago', Carbon::now()->addDays(2)->until());
- $this->assertSame('2 days ago', Carbon::now()->addDays(2)->until(null));
- $this->assertSame('2 days before', Carbon::now()->addDay()->until(Carbon::now()->subDay()));
- Carbon::setLocale('fr');
- $this->assertSame('2 jours avant', Carbon::now()->addDay()->until(Carbon::now()->subDay()));
- Carbon::setLocale('en');
- $this->assertSame('2 days ago', Carbon::now()->addDay()->until(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
- $this->assertSame('2d before', Carbon::now()->addDay()->addHours(5)->until(Carbon::now()->subDay(), null, true));
- $this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->until(Carbon::now()->subDay(), true, false, 2));
- }
- public function testDiffWithInvalidType()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Expected null, string, DateTime or DateTimeInterface, integer given'
- ));
- Carbon::createFromDate(2000, 1, 25)->diffInHours(10);
- }
- public function testDiffWithInvalidObject()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Expected null, string, DateTime or DateTimeInterface, Carbon\CarbonInterval given'
- ));
- Carbon::createFromDate(2000, 1, 25)->diffInHours(new CarbonInterval());
- }
- public function testDiffForHumansWithIncorrectDateTimeStringWhichIsNotACarbonInstance()
- {
- $this->expectExceptionObject(new InvalidFormatException(
- 'Failed to parse time string (2018-04-13---08:00:00) at position 10'
- ));
- $mar13 = Carbon::parse('2018-03-13');
- $mar13->diffForHumans('2018-04-13---08:00:00');
- }
- public function testFloatDiff()
- {
- date_default_timezone_set('UTC');
- $this->assertSame(8986.665965, Carbon::parse('2018-03-31 23:55:12.321456')->floatDiffInSeconds(Carbon::parse('2018-04-01 02:24:58.987421')));
- $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInDays(Carbon::parse('2018-12-02 00:01')));
- $this->assertSame(1.0006944444444443 / 7, Carbon::parse('2018-12-01 00:00')->floatDiffInWeeks(Carbon::parse('2018-12-02 00:01')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false));
- $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false));
- $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-30 14:24:58.987421')));
- $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421')));
- $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false));
- $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-02-13 20:55:12.321456')));
- $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-30 14:24:58.987421')));
- $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-01-30 14:24:58.987421')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2019-06-12 14:24:58.987421'), false));
- $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421')));
- $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInYears(Carbon::parse('2023-06-12 14:24:58.987421'), false));
- $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')));
- $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00')));
- $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')));
- $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));
- $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInMonths(Carbon::parse('2018-10-28 00:00:00'), false));
- $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));
- if (version_compare(PHP_VERSION, '7.2.0-dev', '>=')) {
- $this->assertEqualsWithDelta(0.9999999999884259, Carbon::parse('2020-12-17 00:00:00.000001')->floatDiffInDays('2020-12-16 00:00:00.000002'), 0.0000000001);
- }
- }
- public function testFloatDiffWithRealUnits()
- {
- date_default_timezone_set('UTC');
- $this->assertSame(1.0006944444444443, Carbon::parse('2018-12-01 00:00')->floatDiffInRealDays(Carbon::parse('2018-12-02 00:01')));
- $this->assertSame(1.0006944444444443 / 7, Carbon::parse('2018-12-01 00:00')->floatDiffInRealWeeks(Carbon::parse('2018-12-02 00:01')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456')));
- $this->assertSame(0.9714742503779745, Carbon::parse('2018-03-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2018-04-12 14:24:58.987421'), false));
- $this->assertSame(-0.9714742503779745, Carbon::parse('2018-04-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-03-13 20:55:12.321456'), false));
- $this->assertSame(16.557633744585264, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-30 14:24:58.987421')));
- $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421')));
- $this->assertSame(15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(15.971474250377973, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealMonths(Carbon::parse('2019-06-12 14:24:58.987421'), false));
- $this->assertSame(-15.971474250377973, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealMonths(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(1, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-02-13 20:55:12.321456')));
- $this->assertSame(1.3746000338015283, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-30 14:24:58.987421')));
- $this->assertSame(0.9609014036645421, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-01-30 14:24:58.987421')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(1.3252849653083778, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2019-06-12 14:24:58.987421'), false));
- $this->assertSame(-1.3252849653083778, Carbon::parse('2019-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421')));
- $this->assertSame(5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456')));
- $this->assertSame(5.325284965308378, Carbon::parse('2018-02-13 20:55:12.321456')->floatDiffInRealYears(Carbon::parse('2023-06-12 14:24:58.987421'), false));
- $this->assertSame(-5.325284965308378, Carbon::parse('2023-06-12 14:24:58.987421')->floatDiffInRealYears(Carbon::parse('2018-02-13 20:55:12.321456'), false));
- $this->assertSame(1, Carbon::parse('2018-10-01 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-01 00:00:00', 'Europe/Paris')));
- $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00')));
- $this->assertSame(1, Carbon::parse('2018-10-28 00:00:00', 'Europe/Paris')->floatDiffInRealMonths(Carbon::parse('2018-11-28 00:00:00', 'Europe/Paris')));
- $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));
- $this->assertSame(-1, Carbon::parse('2018-11-28 00:00:00')->floatDiffInRealMonths(Carbon::parse('2018-10-28 00:00:00'), false));
- $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));
- }
- /**
- * https://bugs.php.net/bug.php?id=77007
- * https://github.com/briannesbitt/Carbon/issues/1503
- */
- public function testPhpBug77007()
- {
- $this->assertSame(3, Carbon::now()->addMinutes(3)->diffInMinutes());
- $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
- $endDate = Carbon::parse('2018-10-11 20:59:07.237419');
- $this->assertSame(0, $startDate->diffInSeconds($endDate));
- $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
- $endDate = Carbon::parse('2018-10-11 20:59:07.237419');
- $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(0, $startDate->diffInSeconds($endDate));
- $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
- $endDate = Carbon::parse('2018-10-11 20:59:05.237419');
- $this->assertSame('+ 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(1, $startDate->diffInSeconds($endDate));
- $this->assertSame('- 00-00-00 00:00:01.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(-1, $startDate->diffInSeconds($endDate, false));
- $startDate = Carbon::parse('2018-10-11 20:59:06.914653');
- $endDate = Carbon::parse('2018-10-11 20:59:06.237419');
- $this->assertSame('+ 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(0, $startDate->diffInSeconds($endDate));
- $this->assertSame('- 00-00-00 00:00:00.677234', $startDate->diffAsCarbonInterval($endDate, false)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(0, $startDate->diffInSeconds($endDate, false));
- $startDate = Carbon::parse('2017-12-31 23:59:59.914653');
- $endDate = Carbon::parse('2018-01-01 00:00:00.237419');
- $this->assertSame('+ 00-00-00 00:00:00.322766', $startDate->diffAsCarbonInterval($endDate)->format('%R %Y-%M-%D %H:%I:%S.%F'));
- $this->assertSame(0, $startDate->diffInSeconds($endDate));
- }
- public function testPHPBug80974()
- {
- $this->assertSame(3, Carbon::parse('2018-07-01 America/Toronto')->diff('2018-07-02 America/Vancouver')->h);
- $this->assertSame(0, Carbon::parse('2018-07-01')->utc()->diff('2018-07-02')->days);
- $this->assertSame(1, Carbon::parse('2018-07-01')->utc()->diff(Carbon::parse('2018-07-02'))->days);
- $this->assertSame(1, Carbon::parse('2018-07-01')->diff(Carbon::parse('2018-07-02')->utc())->days);
- }
- public function testDiffWithZeroAndNonZeroMicroseconds()
- {
- $requestTime = new Carbon('2018-11-14 18:23:12.0 +00:00');
- $serverTime = new Carbon('2018-11-14 18:23:12.307628 +00:00');
- $this->assertSame(0, $serverTime->diffInSeconds($requestTime));
- $requestTime = new Carbon('2019-02-10 18:23:12.0 +00:00');
- $serverTime = new Carbon('2019-02-10 18:23:12.307628 +00:00');
- $this->assertSame(0, $serverTime->diffInSeconds($requestTime));
- }
- public function testNearlyFullDayDiffInSeconds()
- {
- $d1 = Carbon::parse('2019-06-15 12:34:56.123456');
- $d2 = Carbon::parse('2019-06-16 12:34:56.123455');
- // Due to https://bugs.php.net/bug.php?id=77007, this changed in 7.2.12:
- $expected = version_compare(PHP_VERSION, '7.2.12-dev', '<')
- ? 86400 // Bad rounding before PHP 7.2.12
- : 86399; // Exact result since PHP 7.2.12
- $this->assertSame($expected, $d2->diffInSeconds($d1));
- }
- public function testNearlyFullDayDiffInMicroseconds()
- {
- $d1 = Carbon::parse('2019-06-15 12:34:56.123456');
- $d2 = Carbon::parse('2019-06-16 12:34:56.123455');
- $this->assertSame(86399999999, $d2->diffInMicroseconds($d1));
- }
- public function testExactMonthDiffInSeconds()
- {
- $d1 = Carbon::make('2019-01-23 12:00:00');
- $d2 = Carbon::make('2019-02-23 12:00:00');
- $this->assertSame(2678400, $d2->diffInSeconds($d1));
- }
- public function testDaysDiffPreservation()
- {
- $deletedDate = Carbon::now()->startOfDay()->addDays(31);
- $this->assertSame('31 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay(), [
- 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
- 'skip' => ['m', 'w'],
- 'minimumUnit' => 'd',
- ]));
- $this->assertSame('31 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay()->subHours(5), [
- 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
- 'skip' => ['m', 'w'],
- 'minimumUnit' => 'd',
- ]));
- $this->assertSame('30 days', $deletedDate->diffForHumans(Carbon::now()->startOfDay()->addHours(5), [
- 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
- 'skip' => ['m', 'w'],
- 'minimumUnit' => 'd',
- ]));
- }
- }
|