Carbon Date startOfDay give me endOfDay date
Asked Answered
C

3

13
$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...

Cordey answered 16/5, 2017 at 8:55 Comment(1)
Can you try: $dt = Carbon::now(); $dt2 = Carbon::now(); dd($dt->startOfDay(), $dt2->endOfDay()); Just curious what the result will be.Unship
I
39

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/

Indohittite answered 16/5, 2017 at 10:2 Comment(1)
Thank you for your help.Cordey
A
10

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().

Anthemion answered 16/5, 2017 at 8:57 Comment(2)
Thank you for your help!Cordey
No problem. Don't forget to accept one of the answers here. :)Singlefoot
B
2

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();

Brach answered 2/9, 2022 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.