InvalidSyntax
InvalidSyntax

Reputation: 9509

Carbon returning the wrong date bug

I am manipulating my dates using the php Carbon package within my Laravel app. I am having some weird results when trying to generate a date (x days in the future)

Please take a look at this code:

$start_date = Carbon::tomorrow('Europe/London');
$end_date = $start_date->addDays($tier->duration_days);
Log::debug('Carbon::now(): '.Carbon::now());
Log::debug('Carbon::tomorrow(Europe/London): '.Carbon::tomorrow('Europe/London'));
Log::debug('$start_date: '.$start_date);
Log::debug('$end_date: '.$end_date);

The code above will print out the following debug lines:

2017-04-17 21:46:31] local.DEBUG: Carbon::now(): 2017-04-17 21:46:31
[2017-04-17 21:46:31] local.DEBUG: Carbon::tomorrow(Europe/London): 2017-04-18 00:00:00 [2017-04-17 21:46:31] local.DEBUG: $start_date: 2017-05-16 00:00:00 [2017-04-17 21:46:31] local.DEBUG: $end_date: 2017-05-16 00:00:00

Using Carbon::tomorrow() will print out the correct dates, however using $start_time which technically uses the same function returns the wrong date. Can someone advise what could be going wrong here?

FYI I have set Europe/London as my timezone in my config/app.php file.

Upvotes: 4

Views: 1495

Answers (1)

Alex
Alex

Reputation: 1418

$date->addDays doesn't actually return an instance with the days added, it returns the same instance after modifying the days (which makes a great deal of difference).

Therefore, you should first copy the date into a new instance and then add the days.

$start_date->copy()->addDays($tier->duration_days);

Upvotes: 6

Related Questions