user3746695
user3746695

Reputation:

Carbon Date startOfDay give me endOfDay date

$dt = Carbon::now();
dd($dt->startOfDay(), $dt->endOfDay());

Carbon {#324 ▼
  +"date": "2017-05-15 23:59:59.000000"
  +"timezone_type": 3
  +"timezone": "Europe/Paris"
}
Carbon {#324 ▼
  +"date": "2017-05-15 23:59:59.000000"
  +"timezone_type": 3
  +"timezone": "Europe/Paris"
}

First variable is the date and hour actually, dd() function is for display content of variables.

startOfDay() method give me the same thing of the endOfDay() method...

Upvotes: 13

Views: 34245

Answers (3)

Aruna Warnasooriya
Aruna Warnasooriya

Reputation: 339

Sorry for the late, but someone might see this answer. The thing is when you use

dd($dt->startOfDay(), $dt->endOfDay());

it mutate $dt object that's why it shows both times as one. So make sure to copy it or clone it before use.

$dt->clone()->startOfDay();
$dt->clone()->endOfDay();

Upvotes: 2

Rashedul Islam Sagor
Rashedul Islam Sagor

Reputation: 2059

Best practices to use copy() method for different date time.

$startDay = Carbon::now()->startOfDay();
$endDay   = $startDay->copy()->endOfDay();

To know more details :

http://carbon.nesbot.com/docs/

Upvotes: 40

Patryk Woziński
Patryk Woziński

Reputation: 746

Have you tried to use copy() or assign to variable, and then use Carbon methods?

$dt = Carbon::now();
dd($dt->copy()->startOfDay(), $dt->copy()->endOfDay());

Don't change $dt value, only copy and then make startOfDay() or endOfDay().

Upvotes: 10

Related Questions