Get Date time differnce base result (laravel 5.3) - php

I have a deliverables table and i want to get results where difference between current date time is 2 days from created date.
e.g today is 4-19-2017 i want to get all records where deadline is 4-21-2017 because here is difference of 2 days between 4-19-2017 and 4-21-2017 or also if 4-20-2017 it should also retrieve how ever if difference is more than 2 it should not be retrieve.
In short if Difference between current date and deadline is 2 days or less should be retrieve
I tried as
$deliverables_dead = DB::table('deliverables')->where('user_id' , Auth::user()->id)->where('deadline' , '>' , Carbon\Carbon::now())->get();
its get all records where is deadline greater than current time.
Please help how can i get answer of above bold statement......

Use whereBetween() and Carbon's addDays():
DB::table('deliverables')
->where('user_id', auth()->id())
->whereBetween('deadline' , [Carbon\Carbon::now(), Carbon\Carbon::now()->addDays(2)])
->get();

Related

Filter available time slot for today in laravel using carbon

I want to filter available time for today for a user. He can only make an appointment for a time slot that is bigger than time right now + 6 hours. But the problem is when + 6 hours transfers to the next day so it could be like 00:44 next day and in that case when it compares to the time slots it shows that time slot at 8:00 for example is bigger than 00:44 so it shows that time slot for today although it shouldn't be possible to make an appointment for that time slot anymore. Is there a way to filter it in the eloquent? I basically want addHours to stop if it reaches 23:59. My Eloquent function:
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->get();
Carbon provides a tomorrow() function that you can use in the same way you've made use of now().
I assume you don't want people booking appointments up until midnight (although perhaps you do?) so I would also use the subHours() function to limit timeslots to before a certain time.
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->whereTime('time', '<', Carbon::tomorrow()->subHours(2))
->get();
You can create timestamp for specfic time using create function with time zone then you can aad hours in current timestamp by using Carbon::now()->addHours(6)
$dtToronto = Carbon::create('2022','8','2', 23,59,59, 'America/Toronto');
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->whereTime('time', '<=', $dtToronto)->get();

Laravel eloquent grabbing results between two days

I have a model for which I have a database field called day. This is a unique field and it can contain a day like monday, tuesday, and so on. So monday can only be used once, etc. The value is literally stored as a string.
Now I am trying to display only upcoming x results, for example, only the results from the next 3 days. Which means, I want to display the result of today, tomorrow and the day after tomorrow. I have been trying like below:
Model::where('day', '>=', Carbon::now())
->where('day', '<', Carbon::now()->addDays(3))
->get();
But this does not give me any results. I want to prevent having to change the field to a date field, for now. But I was wondering if there is any way to achieve what I am looking for with a simple eloquent query.
if the day is unique you can get the next three days and select their results:
$today=Carbon::now()->dayName;
$tomorrow=Carbon::tomorrow()->dayName;
$afterTomorrow=Carbon::tomorrow()->addDay()->dayName;
$acceptableDays=[$today,$tomorrow,$afterTomorrow];
Model::whereIn('day', $acceptableDays)
->get();
pay attention to letter case, Carbon::now()->dayName; will gave you the day name with first capital letter, you should convert it to match the way it stored in your db

How to select from database using laravel where clause and created_at

I want to select all the data on a database using laravel db query or Eloquent ORM. So i want all the data that are other than 3 days from the day they where created. Data 'A' is created on 15th i come in on 16th to check, i won't see Data 'A' but i may see others older than 3 days or equal to 3 days. If i come back on the 18th or 19th i should see Data 'A', obviously because it now older than 3 days or equal to.
so i wrote this code that is not working below
$users = DB::table('matched_users')->where( 'created_at', '>=',
Carbon::now()->subDays(3));
so can anyone correct this for me.
when i did that it work but it shows both someone that his date is just one day one. so is that not enough code
Data Name - created_at
ozil 21/04/2012 16:09:22
mark 21/04/2012 16:09:22
cyril 22/04/2012 16:19:21
so today is 25/04/2012 if run the query of run the above query as it is i get all the result back.
But if change the >= to <= the result is an empty collection
I guess you mean 'older than 3 days from the day they where created'. In this case the query should look like this:
DB::table('matched_users')->where('created_at', '<=', Carbon::now()->subDays(3));
$users = DB::table('matched_users')->whereDate( 'created_at', '<=', Carbon::now()->subDays(3))->get();
is the right way to write the code

Get Last n Months Data Group by Week - Laravel

I am trying to get last 4 months data filtered by week in Laravel. This is code I currently have:
$analytics =
Data::select([
DB::raw('WEEK(p_date_time) AS week'),
DB::raw('COUNT(id) AS count'),
])
->where('p_id', $p_id)
->where('p_date_time', '>=', Carbon::now()->subMonth(4))
->groupBy('week')
->orderBy('week')
->get()
->toArray();
This code gives me 52 results based on Data for 1 full year (As I am using WEEK). But I need around 17 results for the last 4 months.
I can make that happen by gathering of each day and then adding 7 days data as chunk for last 4 months, but how can I do it with query alone?
Thanks.
Maybe just a typo in your Carbon query. Change subMonth(4) to subMonths(4). With 's'.

Propel filter all rows that have today's date as a timestamp in mysql database

How can I filter results only for current day (today) using propel:
$checkIfMaxNrOfPointsTransactionsForToday = PointsTransactionsQuery::create()
->filterByTimeCreated(array('max' => 'today'))
->filterByUserId($this->userId)
->count();
Thanks
Here is an answer i found usefull. Just put min and max within the current date.

Categories