Laravel data monthly count - php

I want to display how many records were inserted every month, today is Feb 1, it should be Zero but it shows 180?
Card::make(
'Incidents this month',
IncidentReport::where('created_at', '>', now()->subDays(30))->count(),
),

use whereBetween and Carbon::now()->startOfMonth() and Carbon::now()->endOfMonth()
IncidentReport::whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()])->count();
The issue is you are subtracting days from today's day.
now()->subDays(30)
This will return 01-Jan-2023
Ref: 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());

How to query between two x amount of days using Laravel and Carbon?

I'm trying to retrieve data from the past 30 to 60 days. Which means I'm leaving out days 0 to 30 and everything past day 60.
My current progress is as such:
$previousMonth = DB::table('table')
->select('table.*')
->whereBetween('date', [Carbon::now()->subDays(30), Carbon::now()->subDays(60)])
->get();
return $previousMonth;
"date" has the datatype of DATETIME in mysql database.
Change the order, because Carbon::now()->subDays(30) is greater than Carbon::now()->subDays(60), and you can use startOfDay() to get start of day:
->whereBetween('date', [Carbon::now()->subDays(60)->startOfDay(), Carbon::now()->subDays(30)->startOfDay()])
Try this startOfDay()
use Carbon\Carbon;
$previousMonth = DB::table('table')
->select('table.*')->whereBetween('date', [Carbon::now()->subDays(60)->startOfDay(), Carbon::now()->subDays(30)->startOfDay()])->get();
return $previousMonth;
Refer This Link
http://carbon.nesbot.com/docs/

Laravel fetch data where carbon last month

I'm trying to fetch the first result from my data where the date is equal to last months date range.
This is can example of my database contents -
id date_recorded
1 2016-07-22 15:21:33
2 2016-08-13 12:22:22
3 2016-07-06 12:22:22
4 2016-09-12 12:45:22
This is my query to fetch the 2nd result (the most recent from the last month).
$seoScoreLastMonth = Manual::where('account_id', Auth::user()->account)
->where('date_recorded', '>=', Carbon::now()->subMonth())
->orderBy('date_recorded', 'desc')
->pluck('seo_score')
->first();
The result returned is null, this is because the first result in the database by id does not match the date?
I needed this month. Here is my query. I think You'll be able to modify it if you need
$start = new Carbon('first day of this month');
$start = $start->startOfMonth()->format('Y-m-d H:i:s'); // startOfMonth for 00:00 time
Your query..->where('date_recorded','>',$start)..
With help of Carbon - get first day of month enter link description here

How to Calculate number of days between two dates in PHP and MYSQL

I need to calculate number of days between two dates.
Required date entered by me,fetch the record from the database by the given dates.
If the database have 'startdate' as 1Jan2015 'enddate' as 5Feb2015.
For January month it should return 30 and for February 5 days.
My table:
id Name Type Project Place Start Date End Date Details
1 Sai Local Site Bangalore 2015-09-03 11:32:47 2015-09-05 11:32:47 test
2 Ram Local IGCAR Chennai 2015-04-01 15:15:36 2015-04-09 15:15:36 Installation
3 Mani Local IGCAR Chennai 2015-04-16 15:16:18 2015-05-21 15:16:18 Training
My coding
///////////Employee Outstation(Travel) details/////////////
$employeeTravel = new EmployeeTravelRecord();
//date_start = '2015-04-01' ;
//date_end = '2015-04-30';
$TravelEntryList = $employeeTravel->Find("(travel_date between ? and ? or return_date between ? and ? )",array($req['date_start'], $req['date_end'],$req['date_start'], $req['date_end']));
foreach($TravelEntryList as $Travelentry){
$amount = (strtotime($Travelentry->return_date) - strtotime($Travelentry->travel_date));
}
For second record, it returns correct value, but for third record it calculates including May month. But i want only 30 days of april.
DATEDIFF() returns value in days from one date to the other.
select *,datediff( end Date, Start Date) as days from My table;
Please have a look at this post, you should find what you're looking for :
How to get the number of days of difference between two dates on mysql?
There is a function in PHP called as date_diff for difference between two dates.
<?php
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

How many weeks are inside of two dates

I have starting dates and ending dates in my database (MySQL).
How can I get the answer, how many weeks(or days) are inside of those 2 dates? (mysql or php)
For example I have this kind of database:
Started and | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26
...
Update to the question:
How to use DATEDIFF?
How can I make this to work? or should I use DATEDIFF completly differently?
SELECT DATEDIFF('Started ','will_end') AS 'Duration' FROM my_table WHERE id = '110';
If the two columns $d1 and $d2 store unix timestamp obtained from time() then this simple line suffices:
$diffweek = abs($d1 - $d2) / 604800;
Otherwise if the columns are of DATETIME type, then:
$diffweek = abs(strtotime($d1) - strtotime($d2)) / 604800;
p/s: 604800 is the number of seconds in a week (60 * 60 * 24 * 7)
p/s2: you might want to intval($diffweek) or round($diffweek)
Calculating the number of days and dividing by seven won't give you the number of weeks between the two dates. Instead it will return the result of division by 7 that doesn't always correspond to the number of weeks between the two dates when thinking in terms of the number of weeks in the ISO calculation.
For example, given start_date = "2010-12-26" and end_date = "2011-01-25" you will be going through W51,52,01,02,03,04 and those are 6 weeks as per ISO, but if you simply calculate the difference and divide by 7, you'll get 5.
The issue appears when the start date and end date belong to different years.
The best way to do the calculation is to get the last week number of the start_date year and it should refer to the December, 28.
function weeks($ladate2,$ladate3) {
$start_week= date("W",strtotime($ladate2));
$end_week= date("W",strtotime($ladate3));
$number_of_weeks= $end_week - $start_week;
$weeks=array();
$weeks[]=$start_week;
$increment_date=$ladate2;
$i="1";
if ($number_of_weeks<0){
$start_year=date("Y",strtotime($ladate2));
$last_week_of_year= date("W",strtotime("$start_year-12-28"));
$number_of_weeks=($last_week_of_year-$start_week)+$end_week;
}
while ($i<=$number_of_weeks)
{
$increment_date=date("Y-m-d", strtotime($ladate2. " +$i week"));
$weeks[]=date("W",strtotime($increment_date));
$i=$i+1;
}
return $weeks;
}
function diff_weeks($ladate2,$ladate3) {
$weeks=weeks($ladate2,$ladate3);
$diff_weeks=count($weeks);
return $diff_weeks;
}
Best regards,
Manikam
MySQL has datediff which returns the difference in days between two dates, since MySQL 4.1.1.
Do note that, as per the manual, DATEDIFF(expr1,expr2) returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
You can use the TO_DAYS function on each date and subtract the two to calculate the difference in days.
DATEDIFF
Find the days and divide by 7
<?php
$dayDif = date('z',strtotime('2009-12-17)') - date('z',strtotime('2009-12-24)');
$numWeeks = $dayDif / 7;
?>
The z option for php's date function gives you the day of the year (0 - 365). By subtracting the two values you find how many days between dates. Then factor by seven for the number of weeks.
Read this page closely, the date() function is rich. http://php.net/manual/en/function.date.php

Categories