Hi I have one column name as date which will hold advance payment date. I want to fetch current month date advance paid. for what i am using below code.
echo $startDate = date($currentYear . "-" . $currentMonth . "-1");
echo $endDate = date($currentYear . "-" . $currentMonth . "-" . $numberOfDaysInMonth);
DB::enableQueryLog();
echo $advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
->whereDate('date', ">=", $startDate)
->whereDate('date', "<", $endDate)
->get();
Herein am getting data when am applying one date condition but when applying other it is not returning data. Also tried with between there also same issue. Any help is deeply appreciated on this
If I understand what you need to do correctly then it's easier to use Carbon to do it:
$startDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)
$endDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)->endOfMonth();
DB::enableQueryLog();
$advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
->whereDate('date', ">=", $startDate)
->whereDate('date', "<=", $endDate)
->get();
Related
guys! i have automatic code number for invoice. the automatic code number is "xx/month/year". i want "xx" in my automatic code number reset to 1 when the year change or every 1 january. this is my code
function noinvoice()
{
$latest = Pengiriman::latest()->first();
$month = date('m');
$year = date('Y');
if (!$latest) {
return '1/' . $month . '/' . $year;
}
$string = preg_replace("/[^0-9\.]/", '', $latest->noinvoice);
return sprintf($string + 1) . '/' . $month . '/' . $year;
}
You need to inspect your $latest object. If $latest doesn't exist OR its year doesn't match this year, then the invoice number is 1. Otherwise, it's $latest->noinvoice + 1.
You never mentioned where you store your invoice creation date, so I'll assume you use the default created_at Laravel approach. For this task you should add one more condition to your if statement:
if (!$latest or date('Y', strtotime($latest->created_at)) != $year) {
I am new to Laravel and not really sure how to do this.
I record dates in my database as start_day in my times table.
Date is persisted with the following format: 2019-10-03
Right now all I have is:
$months = DB::table('times')->where('start_day', '=', 2019)
How should I select data for each month of the year?
use whereYear
use Carbon\Carbon;
$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();
If you want to select months only then
$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();
You need to change the where() to whereYear() to search by year.
$months = DB::table('times')->whereYear('start_day', '=', 2019)
write
$month = array():
for($i=1;$i<=12;$i++){
$month[] = DB::table('times')->whereYear('start_day','2019')->whereMonth('start_day',$i)->get();
}
OR
$month = array():
for($i=1;$i<=12;$i++){
$month[] = DB::table('times')->where([['YEAR(start_day)', '=', 2019],['MONTH(start_day)','=',(int)$i]])->get();
}
you can try monthly , weekly , yearly in this format
$weeklyNumberOfSales = Sale::whereBetween('order_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
->sum('qty');
$monthlyNumberOfSales = Sale::whereMonth('order_date',Carbon::now()->format('m'))
->sum('qty');
$yearlyNumberOfSales = Sale::whereYear('order_date',Carbon::now()->format('Y'))
->sum('qty');
model name Sale .
'order_date' and 'qty' are the field name
I've a problem here. I need to get a quarter of the year without the past quarter. F.e:
In my database I have a field created_at, which saves the timestamp of service created. I need to not involve those services, which was made in the past quarter. How should I do that?
I'm trying to write a SQL function like this to not involve those services, which was made in the past quarter:
$services= Service::find()
->where([
'client_service.created' => function ($model) {
return ceil(date('n') / 3) - 4;
But I guess I'm wrong. Thanks for any help.
Edited:
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
[client_service.created' => function ($model) {
return ceil(date('n') / 3) - 4;]
You are storing timestemp in db for service date , we can find current Quarter start date and end date from current month and use in query.
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime($current_year.'-01-01 00:00:00');
$end_date = strtotime($current_year.'-03-31 23:59:59');
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime($current_year.'-04-01 00:00:00');
$end_date = strtotime($current_year.'-06-30 23:59:59');
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime($current_year.'-07-01 00:00:00');
$end_date = strtotime($current_year.'-09-30 23:59:59');
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime($current_year.'-10-01 00:00:00');
$end_date = strtotime($current_year.'-12-31 23:59:59');
}
Use this $start_date and $end_date timestemp in Query as below :
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
['between', 'client_service.created', $start_date, $end_date]
])
Find start and end date of quarter
$date = new \DateTime(); // Current Date and Time
$quarter_start = clone($date);
// Find the offset of months
$months_offset = ($date->format('m') - 1) % 3;
// Modify quarter date
$quarter_start->modify(" - " . $months_offset . " month")->modify("first day of this month");
$quarter_end = clone($quarter_start);
$quarter_end->modify("+ 3 month");
$startDate = $quarter_start->format('Y-m-d');
$endDate = $quarter_end->format('Y-m-d');
Query
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
['between', 'date_format(FROM_UNIXTIME(client_service.created), "%Y-%m-%d")', $startDate, $endDate]
You can also use mysql Quarter() to achieve the result.
<?php
$busno=3;
$busname = 'Bus3';
$startdate='09/03/2017';
$enddate='09/11/2017';
$value1 = value_table::where('Start Date', $startdate)->get();
$value1 = value_table::where('End Date', $enddate)->get();
I want to check that the start date & end date should not conflict with any date in the table.
Is there any chance to check it with a single query?
$value1 = value_table::where('Start Date', $startdate)->where('End Date', $enddate)->get();
1) If you need to check if one pair of dates is not contained within another then do this:
$value = value_table::where('Start date',">=",$endDate) //starts after your end date
->orWhere('End date',"<=",$startDate) //Ends before the other start date
2) To check if a pair of dates is fully contained within another pair of dates do this:
$value = value_table::where('Start date',"<",$startDate)
->where('End date',">",$endDate);
3) To check if a pair of dates is partially contained within another pair then do this:
$value = value_table::where(function ($q) {
$q->where('Start date',"<",$startDate);
$q->where('End date',">", $startDate); //$startDate within
})->orWhere(function ($q) {
$q->where('Start date',"<",$endDate);
$q->where('End date',">", $endDate); //$endDate within
});
I think you should try this like:
$value1 = value_table::whereDate('Start Date', $startdate)
->whereDate('End Date', $enddate)
->get();
OR
$value1 = value_table::where('Start Date', $startdate)
->orWhere('End Date', $enddate)
->get();
I want to filter the result of the records within the period, but i can't find any example of how to use the where clause to get the exact range of data.
These are the data posted from form:
[monthRangeStart] => 12
[yearRangeStart] => 2013
[monthRangeEnd] => 12
[yearRangeEnd] => 2013
and the following is the format of updated_at timestamp
2013-12-18 07:22:34
How should i finish the line of code
$trans = Transaction::where('updated_at', )
In my opinion if you decided to use some framework you should use tools it provides in order to create better solutions. Laravel has very beautiful Carbon class for date manipulation. This is solution I came to:
$trans = Transaction::whereBetween('updated_at', [
\Carbon\Carbon::createFromDate(Input::get('yearRangeStart'), Input::get('monthRangeStart'))->startOfMonth(),
\Carbon\Carbon::createFromDate(Input::get('yearRangeEnd'), Input::get('monthRangeEnd'))->endOfMonth()
])->get();
In this case you will never go wrong with number of days in any specific month.
You may try something like this
$start = Input::get('yearRangeStart') . '-' . Input::get('monthRangeStart');
$end = Input::get('yearRangeEnd') . '-' . Input::get('monthRangeEnd') . '-' . '31';
$trans = Transaction::whereBetween('updated_at', array($start, $end))->get();
Update :
$start = Input::get('yearRangeStart') . '-' . Input::get('monthRangeStart');
$endYear = Input::get('yearRangeEnd');
$endMonth = Input::get('monthRangeEnd');
$endDays = cal_days_in_month(CAL_GREGORIAN, $endMonth, $endYear);
$end = $endYear . '-' . $endMonth . '-' . $endDays;
$trans = Transaction::whereBetween('updated_at', array($start, $end))->get();
This should do it...
$trans = Transaction::where('updated_at', '>', "$yearRangeStart-$monthRangeStart-00 00:00:00")
->where('updated_at', '<', "$yearRangeEnd-$monthRangeEnd-31 99:99:99")
->get();
If you decide you need to filter on the days/hours/minutes/seconds as well, you can replace those as you go.