Laravel Eloquent Select Between current month and previous 3 months - php

i'm trying to build a query that will select all of the records in my DB between now (current month) and the previous 3 months.
My query somewhat works, but i want to ignore the day of the month. At the moment, it's selecting the last # of months to the current DAY as well but i want to ignore the current day and use the start and end of the months.
Here's my query:
$dateS = Carbon::now()->subMonth(3);
$dateE = Carbon::now();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
Any help would be appreciated. It's really bugging me!

This will do the trick I guess
$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
startOfMonth() begins with 1st date of the month

Use Carbon:
$data = Data::where("created_at",">", Carbon::now()->subMonths(6))->get();
You need to import the namespace to use Carbon:
use Carbon\Carbon;

Related

Get the date after six months from a specific date and count how many days have passed since that day in Laravel

I'm trying to get the employees that have worked for more then 6 months and how many days have passed since that day.
I've fetched the employees with six months experience using the following query:
$empsWithSixMonthsExp = Employees::whereDate('joining_date', '<=', Carbon::now()->subMonths(6))->where('status', 1)->get();
But now I want to get the date when their 6 month was fulfilled and how many days have passed since completing their 6 month mark.
Thanks!
Here is the example for single record
$joiningDate = '05/06/2021';
$AfterSixMonthDate = \Carbon\Carbon::createFromFormat('d/m/Y',
$joiningDate)->addMonths(6);
$todaysDate = \Carbon\Carbon::now();
$daysDifferent = $todaysDate->diff( $AfterSixMonthDate)->format('%a Days');
you can dump this code
dd( $daysDifferent,$AfterSixMonthDate,\Carbon\Carbon::now());

Retrieve all rows from the last possible 3 years (Laravel + Eloquent)

I'm trying to get all the rows I can in a Model, however it must be the last 3 possible years, as if I have a result in the year 2020 but nothing in the year 2019 then it must search in 2018, 2017, but if I had a result in 2019 then it must search only for the results in 2018.
I've already gotten the hang of whereBetween, however I don't get any results from the database using the following query:
$year = $request->all();
$fromDate = $year['yearFrom'];
$toDate = $year['yearTo'];
return Tipico::whereBetween(
DB::raw('date(created_at)'), [$fromDate, $toDate]
)
->get();
As you can see, I get the year from a post method which gives me the initial and finish year, however it doesn't validate the information I'm searching for, whats the best way to do it?
Use Carbon to create your dates:
$fromDate = Carbon::createFromDate($year['yearFrom'], 1, 1)->startOfDay();
$toDate = Carbon::createFromDate($year['yearTo'], 12, 31)->endOfDay();
Then in your query, created_at is already a datetime, so:
return Tipico::whereBetween('created_at', [$fromDate, $toDate])->get();
Remember to include Carbon in your Class at the top:
use Illuminate\Support\Carbon;
or only
use Carbon\Carbon;
To get the data from the past 3 years, one option would be to run a query to see what years have the data you need.
So, let's use the yearTo as reference to his and select the top 3 years with data:
$years = Tipico::select(\DB::raw('YEAR(created_at) as years'))
->where('created_at', '<=', $toDate)
->distinct()
->orderByRaw('YEAR(created_at) DESC')
->limit(3)
->get();
Now in $years you will have the 3 years. Now you can run the query with this years as parameter:
return Tipico::whereIn(\DB::raw('YEAR(created_at)'), $years)->get();
*Not sure if you can pass $years directly or need to transform to array. Take a look.

Query CodeIgniter NOW() Sysdate() or .?

i have a problem with query SQL.
I need do a query that it result my order to last week ( 7days to now) I use this code
$this->db->where("account_orders.data > now()-7",NULL);
date in my db is -> 2019-04-05
Why don't it work ?
I dont know why are using this now()-7, you can use range here with where clause if you want orders between 7 days to now, as:
$dateLastWeek = date('Y-m-d', strtotime('-7 days')); // last week day
$dateCurrent = date('Y-m-d'); // current day
In CI, you can use range as like:
$this->db->where("account_orders.data >=", $dateLastWeek);
$this->db->where("account_orders.data <=", $dateCurrent);
If you just need last week orders then, you can use just $dateLastWeek date in your query as:
$this->db->where("account_orders.data", $dateLastWeek);
Note that, i am using Y-m-d date format due your input which you mentioned in your question 2019-04-05

PHP Carbon / Laravel Eloquent - Check if DateTime value is for the current day

I am working on a booking system and need to select only the records where a certain field is for today. Not today and in the future, but only today.
The query currently is:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', ?????);
$advancedBookings->get();
The requested_booking_time is the field that I wish to be checked, the date stored is in this format:
2017-08-23 08:00:00
So I want to only select rows that are on the same day as the current day.
As i understand, you want that records which is created today; then just get the today's date.
$today = date("Y-m-d");
now your query is like this.
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', $today)->get();
You can use whereDate and format your dates with Carbon:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
$advancedBookings->get();
Or format the your date before the query with Carbon.
You should try this:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=', date('Y-m-d'))
->get();

php MySQL Get Missing day from a Month or BETWEEN days

I need to get an array with days missing from a month.
Get year and month: to Between:
$dateS //have the current given date to make the filter
$year = mb_substr($dateS, 0, 4);
$month = mb_substr($dateS, 5, 7);
$tday = cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year);
$cmonthS = $year."-".$month."-01 00:00:00";
$cmonthE = $year."-".$month."-".$tday." 23:59:59";
I am getting the array with this statement:
$stmtpre = "SELECT days FROM registry WHERE days BETWEEN '2016-12-01 00:00:00' AND '2016-12-31 00:00:00';";
All data have the current format as date time in MySQL:
XXXX-XX-XX 00:00:00
But it returns the current day, and I need to get in PHP the missing days.
Does anyone know how can i do it?
i create and array with Total days of the month and other array with de Event day i run this function to get the diference into this 2 array:
http://php.net/manual/en/function.array-diff.php
and get finaly my No Event Array

Categories