Carbon addWeeks() function not working
Asked Answered
P

4

5

I am creating some dates with Carbon in PHP, but I seem to be doing something wrong.

This here is my code:

$start = Carbon::create(2015, rand(6,7), rand(1,30), 0);
$end = $start->addWeeks(3);

echo "start time: " . $start;
echo "<br />";
echo "end time: " . $end;

And the output of the above is two exact same dates, e.g.:

start time: 2015-07-01 00:00:00
end time: 2015-07-01 00:00:00

I reffered to the docs, which can be found here: http://carbon.nesbot.com/docs/#api-addsub. Does anybody know what am I doing wrong?

Pinna answered 15/9, 2015 at 14:43 Comment(4)
Are those carbon object mutable or not? If yes you might be printing the same twice. addWeeks() might return $start (modified) for method chaining. Thus $start === $endYggdrasil
Does addWeek() work? I'd test myself, but don't have access to Carbon. Also, I'd confirm if the PHP version is sufficient and any dependencies are up-to-date.The home page says 1.x is compatible with PHP 5.3+. A 2.x version is in the works and will require PHP 5.4+.Croak
@YiminRong server is configured fine, I am using Laravel on a vm with PHP 5.6Pinna
You can try moving echo "start time: " . $start; to before the addWeeks line. This will tell you whether the operation succeeded. It might be that some Carbon functions modify the caller, as opposed to returning a new object. Some of the PHP array functions do that.Croak
Y
8

I haven't worked with Carbon yet but I'd say those Carbon object are mutable. Also most functions seem to return $this for method chaining (aka fluent interface).

Thus, when doing $end = $start->addWeeks(3); your $end is actually the same object as $start. (just an intelligent guess)

To avoid this try to either clone the object before manipulation (if possible) or create another one.

Version 1

$start = Carbon::create(2015, rand(6,7), rand(1,30), 0);
$end = clone $start;
$start->addWeeks(3);

Version 2

$start = Carbon::create(2015, rand(6,7), rand(1,30), 0);
$end = Carbon::create(2015, rand(6,7), rand(1,30), 0);
$start->addWeeks(3);
Yggdrasil answered 15/9, 2015 at 14:56 Comment(1)
yep, you were right, the objects are mutable and your intelligent guess was correct, thanks :)Pinna
P
1

$end takes the same value as $start after the addition and it looks like it has not changed. But it has:

>>> use Carbon\Carbon;
=> false
>>> $start = Carbon::create(2015, rand(6,7), rand(1,30), 0);
=> Carbon\Carbon {#766
     +"date": "2015-07-16 00:00:00",
     +"timezone_type": 3,
     +"timezone": "Asia/Bangkok",
   }
>>> $end = $start->addWeeks(3);
=> Carbon\Carbon {#766
     +"date": "2015-08-06 00:00:00",
     +"timezone_type": 3,
     +"timezone": "Asia/Bangkok",
   }
>>> $end
=> Carbon\Carbon {#766
     +"date": "2015-08-06 00:00:00",
     +"timezone_type": 3,
     +"timezone": "Asia/Bangkok",
   }
>>> $start
=> Carbon\Carbon {#766
     +"date": "2015-08-06 00:00:00",
     +"timezone_type": 3,
     +"timezone": "Asia/Bangkok",
   }
Pruter answered 15/9, 2015 at 14:50 Comment(0)
B
1

Carbon dates are mutable. Try this:

$rand_date = Carbon::create(2015, rand(6,7), rand(1,30), 0);


echo "start time: " . $rand_date->format('Y-m-d');
echo "<br />";
echo "end time: " . $rand_date->addWeeks(3)->format('Y-m-d');
Bondage answered 30/10, 2019 at 12:57 Comment(0)
G
0

because you'r not parsing date ($start) in carbon parse method,

$start = Carbon::create(2015, rand(6,7), rand(1,30), 0);
$end = Carbon::parse($start)->addWeeks(3);

i have not tested your code but hope so it work.

Googol answered 31/5, 2021 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.