My Code is like below
# 'date' is `2017-12-13`
$date = $request->get('date');
Carbon::setWeekStartsAt(Carbon::SATURDAY);
Carbon::setWeekEndsAt(Carbon::FRIDAY);
$now = Carbon::now();
if (!$date) {
$date = $now;
} else {
try {
$date = Carbon::parse($date);
} catch ( \Exception $exception ) {
$date = $now;
}
}
__Output__
IF dd($date, $date->endOfWeek());
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-15 23:59:59.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-15 23:59:59.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
IF dd($date, $date->startOfWeek());
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-09 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-09 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
IF dd($date, $date->startOfWeek(), $date->endOfWeek());
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-15 23:59:59.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-15 23:59:59.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
object(Carbon\Carbon)#123 (3) {
["date"]=>
string(26) "2017-12-15 23:59:59.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "Asia/Dhaka"
}
The thing is, the date reference is changed and this is not supposed to happen. Please solve this issue or how can be solved.
Now, I want to get all the dates from the start of the week to end of the week. so I did it.
for ( $currentDate = $startDateOfTheWeek; $currentDate->lte($endDateOfTheWeek); $currentDate->addDay() ) {
$weekDates[] = $currentDate->toDateString();
}
This loop never breaks.
And, Thanks for the package. 馃憤
use clone, and I think you should set the days of the week in the object, not globally
Re. 1. This is just how Carbon works. Methods like addDay change the properties of the object, they don't just return a value. If that was to be changed now it would probably break hundreds of projects that use Carbon and rely on this behaviour.
You're essentially asking for immutable object semantics, i.e. like the PHP built in DateTimeImmutable class, rather than DateTime . Carbon doesn't have it but you may be interested in CakeChronos which does, and which was originally based on Carbon.
Carbon is mutable for many reason (PHP 5.3 compatibility, ease of modify) and you can simply use clone or ->copy() before calling any method that change the date.
Most helpful comment
Re. 1. This is just how Carbon works. Methods like addDay change the properties of the object, they don't just return a value. If that was to be changed now it would probably break hundreds of projects that use Carbon and rely on this behaviour.
You're essentially asking for immutable object semantics, i.e. like the PHP built in DateTimeImmutable class, rather than DateTime . Carbon doesn't have it but you may be interested in CakeChronos which does, and which was originally based on Carbon.