PHP Carbon get toDay date by formatting date
Asked Answered
S

3

31

Simply I can format PHP date such as:

$current_date_time = new DateTime("now");
$user_current_date = $current_date_time->format("Y-m-d");

to get toDay date. how to do this action by using Carbon without time in date?

$now = Carbon::now();
echo $now;    // 2015-11-11 12:38:36
Sketchy answered 22/11, 2015 at 11:58 Comment(0)
H
68

The documentation is a good place to find this sort of thing. There are plenty of examples how to do it

$dt = Carbon::now()

var_dump($dt->toDateTimeString() == $dt);          // bool(true) => uses     __toString()
echo $dt->toDateString();                          // 1975-12-25
echo $dt->toFormattedDateString();                 // Dec 25, 1975
echo $dt->toTimeString();                          // 14:15:16
echo $dt->toDateTimeString();                      // 1975-12-25 14:15:16
echo $dt->toDayDateTimeString();                   // Thu, Dec 25, 1975 2:15 PM

// ... of course format() is still available
echo $dt->format('l jS \\of F Y h:i:s A');         // Thursday 25th of December 1975 02:15:16 PM
Hafner answered 22/11, 2015 at 12:4 Comment(0)
L
24

You can add the static method today() at the end of the Carbon Object:

$today = Carbon::today()->toDateString();

Then ->toDateString() to convert the carbon object to a date string.

This will take the Carbon Object of: date: 2023-01-27 00:00:00.0 UTC (+00:00), and modify it to a string 2023-01-27 without time stamp.

Long answered 7/12, 2017 at 9:21 Comment(0)
B
9

You can format the output like this: Carbon::now()->format('Y-m-d');

Brenza answered 19/8, 2020 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.