1

In my Laravel project I have a MySQL database table with the following column:

 |   requested_at  |
 2018-02-03 12:00:00
 2018-03-03 11:00:00

I want to get all items from this column where the timestamp (requested_at) is older than 60 days:

$now = Carbon::now ();  

$60days  = DB::table('exampletable')->where('requested_at', '=', $now->subDays(60))->get();

For my following code I need to format the Date format from my column requested at.

I only want to get the date like this 2018-03-03 without the time.

I play around with some carbon methods but It didn't work at all:

$format60 = Carbon::createFromFormat('Y-m-d', $60days);
1
  • 1
    FYI your variable $60days is invalid, variables must start with an underscore OR a letter. As for the Carbon part it should look something like this, Carbon::createFromFormat('Y-m-d', $days60['FIELD THAT HAS YOUR TIME VALUE']); - note I changed your variable name. Commented May 3, 2018 at 13:52

2 Answers 2

1

It seems that you are using TIMESTAMP type so

->where('requested_at', '<=', now()->subDays(60)->startOfDay())->get();

As you can see there is no need to format Carbon instance.

Break it all down:

now() // Laravel helper to get Carbon::now()
->subDays(60) // subtract 60 days
->startOfDay() // set time part of timestamp to start of the day

Note: add requested_at into $dates array in your model for easier use

Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve this by mysql only, no need of carbon.
Just using datediff() and now() of mysql.
Do like this

$days60  = DB::table('exampletable')
    ->whereRaw('datediff(now(),requested_at) = 60')
    ->get();

Example :- enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.