Laravel fetch data where carbon last month - php

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

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 get latest date from now on a given date using carbon in laravel

I want the nearest date passed from given date using carbon
$givenDate = "2020-07-18";
Today is = 2020-12-01
So the result should be the 18th last month. $result = "2020-11-18"
you can use subMonth() from now to get the last month then create a new datetime with suitable values:
$givenDate =Carbon::parse( "2020-12-20");
if($givenDate>Carbon::now())
$result=Carbon::create(Carbon::now()->year,Carbon::now()->month,$givenDate->day)
->format('yy-m-d');
else
$result=Carbon::create(Carbon::now()->year,Carbon::now()->subMonth()->month,$givenDate->day)
->format('yy-m-d');

I get an specific day from relative date and also the last day of the relative date, how can I find the difference?

$result = DB::table('customer')->where('isp',2)->get();
// start_date is stored date in database
$date= $result->start_date;
$dts = Carbon::create($date);
// in order to get the last day of relative month
// get the last day of month for instance is equal to 2010-09-30 00:00:00
// find the difference between in days
}
When I use from diffindays of Laravel the output is 0. Any ideas why?

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();

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");
?>

Categories