'Laravel : How to add days to datetime field?

How to add days to datetime field in Laravel?

For example,
there is a updated_at field in articles table:

$article = Article::find(1);
$updated_at=$article->updated_at;

I want to add 30 days to updated_at field.

In Carbon, it could be done like this:

$expired_at=Carbon::now()->addDays(30);

But how to do it in above example?



Solution 1:[1]

you can use Carbon::parse

$article = Article::find(1);

$updated_at=Carbon::parse( $article->updated_at)->addDays(30);

Suppose if updated_at is 2017-09-30 22:43:47 then output will be 2017-10-30 22:43:47.000000

Solution 2:[2]

Have you tried like this with raw php? If you want you can change your datetime format inside first parameter of date()

$updated_at=date('Y-m-d H:i:s', strtotime('+30 day', $article->updated_at));

Solution 3:[3]

Without Carbon

For full example go to - https://www.php.net/manual/en/datetime.add.php

The below code is for only add days

$d->add(new DateInterval('P10D'));

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vision Coderz
Solution 2 Always Sunny
Solution 3 Mr. A