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?

4
  • 1
    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 === $end
    – m02ph3u5
    Sep 15, 2015 at 14:50
  • 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+.
    – Yimin Rong
    Sep 15, 2015 at 14:51
  • @YiminRong server is configured fine, I am using Laravel on a vm with PHP 5.6
    – allisius
    Sep 15, 2015 at 15:17
  • 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.
    – Yimin Rong
    Sep 15, 2015 at 15:29

4 Answers 4

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);
1
  • yep, you were right, the objects are mutable and your intelligent guess was correct, thanks :)
    – allisius
    Sep 15, 2015 at 15:15
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",
   }
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');
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.