简体   繁体   中英

Converting date to Carbon format to apply Add/Sub days in Laravel

The format of $day variable as follows:


"2019-10-30 18:29:19"

I'm trying to subtract 1 day as follows:

$prv_day = $day->subDays(1);

I receive this error:

Call to a member function subDays() on a non-object

How do I convert this $day format to the Carbon::now(); format in order to be able to apply subDays() to it?

It's a straight-forward format, so this will work:

$day = '2019-10-30 18:29:19';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $day);
$date->subDay(); // Subtracts 1 day
echo $date->format('Y-m-d h:i:s');

RESULT

2019-10-29 06:29:19

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM