#how to check if a week has passed

7 messages · Page 1 of 1 (latest)

compact badger
#

for displaying dates i want to use two different formats, when a week has passed, the format should be: "June 11th 2023" and when the week limit hasn't been met, it should be something like: "4 days ago". that's the idea.

i have one method on my model to display one format:

    public function getFormattedDate()
    {
        return $this->published_at->format('F jS Y');
    }

on the blade i use the "x days ago" format on an if statement:

  @if ($post->published_at=Carbon\Carbon::now()->subDays(6))
      {{-- {{ $post->published_at->diffForHumans() }} --}}
      a week has passed
  @else
      {{-- {{ $post->getFormattedDate() }} --}}
      a week has not passed
  @endif

I need to check if a week has passed because I want to show different formats but something is wrong, thanks.

raven plume
#

You have one (1) equals (=) sign.

You need 2 or 3 equals sign for comparison == or ===

coarse flicker
#

= for exactly a week

#

Also your subtracting 6 days?

compact badger
#

im struggling to format the date after a week has passed, seems like diffForHumans starts printing the date as soon as the 2nd week is passed, after that you get the format on my else statement, will try that, @edgy bear thank you.

#

ok

#

a week has not passed = 4 days ago
a week has passed = October 4th 2004