Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

92
Views
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.

8 months ago · Santiago Trujillo
1 answers
Answer question

0

$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);
8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs