Carbon: I know how it happens, but would suggest to change it

Created on 2 Jan 2018  路  4Comments  路  Source: briannesbitt/Carbon

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. 馃憤

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.

All 4 comments

use clone, and I think you should set the days of the week in the object, not globally

  1. Cloning works. but should not it be done without manipulating the whole object? why not just return a new object with the new data?
  2. Yes, weekdays should be set globally. Cause it's country-specific.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chanux picture chanux  路  5Comments

Yo1L picture Yo1L  路  5Comments

yica00 picture yica00  路  4Comments

nocive picture nocive  路  5Comments

rohstar picture rohstar  路  4Comments