Search code examples
phplaravelphp-carbon

Adding day/incrementing with a carbon formatted date


I'm using carbon to get today's date and format it in a laravel blade template, which is not a problem at all, but when it comes to incrementing I'm having an issue

I'm currently testing with just adding a single day, but I want to actually create table headers for today and the next 7 days.

In my blade I have:

<?php 
use Carbon\Carbon;
$date = Carbon::now()->format('m/d/Y');
?>

<thead>
  <tr>
    <th>{{$date}}</th>
    <th>{{$date->addDay()}}</th>
  </tr>
</thead>

But it's saying that I'm calling addDay on a string, which makes sense because I'm formatting the date that way.

Is there a way I can keep this date format of mm/dd/yyyy but also create a loop using something like addDay on that format? I just want to increment for the next 7 days and use those dates as table headers


Solution

  • could you try this

    <?php 
    use Carbon\Carbon;
    $format = 'm/d/Y';
    $date = Carbon::now();
    ?>
    
    <thead>
      <tr>
        <th>{{$date->format($format)}}</th>
        <th>{{$date->addDay()->format($format)}}</th>
      </tr>
    </thead>