How to get the month duration in Laravel - laravel

I have a startdate and and enddatein the format Y-m-d.
'startdate'=>date('Y-m-d', strtotime(Input::get('startdate'))),
'enddate'=>date('Y-m-d', strtotime(Input::get('enddate')))
How can I get the Duration between these two days, like 2 months or 1 month or 2 weeks in Laravel?

You can use the Carbon class, which is already included in Laravel 4, to get the difference between two days in a human-readable format.
$startDate = Carbon::createFromFormat('Y-m-d', Input::get('startdate'));
$endDate = Carbon::createFromFormat('Y-m-d', Input::get('enddate'));
echo $startDate->diffForHumans($endDate);

Basically, it is Carbon
$start = new Carbon(date('Y-m-d', strtotime(Input::get('startdate'))));
$end = new Carbon(date('Y-m-d', strtotime(Input::get('enddate'))));
$diff_days = $start->diff($end)->days;
Well, there are more, please look up the documentation for details: https://github.com/briannesbitt/Carbon#api-difference

Related

Laravel : calculate before inserting into DB

I have a form and before saving it in the DB is it possible to make a calculation?
I get a date of birth and want to make an account with the current year.
Example: 10-10-1990 and I want it to subtract from the current year.
1990 - 2021
$tabel->datenasc = $request->datenasc;
You can still do your calculations before inserting into DB
// if $request->datenasc == 10-10-1990
$current_year = date("Y");
list($day, $month, $year) = explode("-", $request->datenasc);
$year_diff = $current_year - $year;
you can now insert $year_diff in the database;
You can use setYear method from carbon library
$date = Carbon::parse("10-10-1990");
$dateWithCurrentYear = $date->setYear(now()->format('Y'));

How to get array of dates between two dates in laravel? [duplicate]

This question already has answers here:
PHP Carbon, get all dates between date range?
(11 answers)
Closed 2 years ago.
I want to get dates between two dates in an array. The scenario is the following: I want to get daily sales cash collection and there is one filter of $fromdate and $todate. So even if I don't get any sales for an specific date I have to show that in the table.
You can use CarbonPeriod for this.
I found something helpful at https://stackoverflow.com/a/50854594/13642447.
Use DatePeriod class to create the range of dates based on months or days
<?php
$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2013-10-31' );
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
$dates[] = $date->format("Y-m-d");
}
var_dump($dates);
?>
if you want the range of dates to be by days, say from 2012-08-01 to 2012-08-25 then just change the interval like this $interval = new DateInterval('P1D');.

How obtain 24h of diference between two TIMESTAMPS using Laravel Query Builder?

I have two variables, and I would like to count the hours between this variable 2020-01-16 15:20:24 and this other variable 2020-01-15 11:01:54
I'm doing a count and I would like to add +1 if the difference is more than 24 hours
I tried with this code:
$time = DB::table('incidencia_tiempo')
->whereColumn('timeA', '>=', '24:00:00', and, 'timeA', '-', 'TimeB = 24:00:00'))
->count();
I have a problem because I want to work with days too, for example if have passed two days shouldn't add +1 to the count.. but I don't now how work with days.
try with carbon
$dateStart = Carbon::parse($dateStart);
$dateEnd = Carbon::parse($dateEnd);
$diff = $dateStart->diffInHours($dateEnd,false);

Carbon datetime not working for sum amount laravel

i want to sum amount logs created today in my Repeat table in database i tried followings but not working
$start = (new Carbon('now'))->hour(0)->minute(0)->second(0);
$end = (new Carbon('now'))->hour(23)->minute(59)->second(59);
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->where('created_at',[$start , $end])->sum('amount');
also tried
$start = carbon::today();
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->where('created_at',$start)->sum('amount');
If you want to get today's record by using carbon you can do it as below.
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->whereDate('created_at', Carbon::today())->sum('amount');
you can use Carbon::now() or Carbon::today() but check this amount should be integer or float if it string then sum will be 0(zero)
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->whereDate('created_at', Carbon::now())->sum('amount');```

Retrieve data between two dates in laravel

I am using an eloquent query to retrieve data from a table. The table columns look like this:
id started_at finished_at
1 2016-06-01 2016-06-30
2 2016-05-03 2016-05-28
What I want to do is, given a $date (ex: 2016-06-18 ) and get the data of the row, that the $date between started at and finished_at columns.
I have found whereBetween clauses in Laravel documentation, but I do not have an idea to use it correctly.
If you can use Carbon then this code will work fine for you.
$dateS = new Carbon('first day of January 2016');
$dateE = new Carbon('first day of November 2016');
$result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get();
Or you can check the issue by debugging query using DB::enableQueryLog(); and DB::getQueryLog(); functions like
DB::enableQueryLog();
$dateS = new Carbon('first day of January 2016');
$dateE = new Carbon('first day of November 2016');
$result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get();
var_dump($result, DB::getQueryLog());
Try to do something like this:
$date1 = Carbon::today()->toDateString();
$date2 = Carbon::today()->toDateString();
$myModel = MyModel::find(1);
$myModel->whereBetween('created_at', [$date1, $date2]);
$myModel->get();
Of course, you will need to change the dates.
You can use whereBetween something like this:-
ModelName::whereBetween('date',['2022-08-01','2022-10-30'])->get();

Resources