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');.
Related
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'));
I just wanted to extract unique year value from this date type column. But I always get this error "You might need to add explicit type casts".
If there are 2020-06-23, 2020-07-01, 2019-01-02, 2019-02-05 dates, my desired output is to return the unique year values. So the output should be 2020 and 2019 only.Please help. Thank you.
Here is my code:
$year= DB::table('loans')
->select('date_release', DB::raw('YEAR(date_release) as year'))
->groupBy('year')
->get();
The query could be (returns all when there are different dates from different years);
SELECT EXTRACT(YEAR FROM loans.date_release) AS year FROM loans group by year;
The query builder will be
return DB::table('loans')
->select([DB::raw('EXTRACT(YEAR FROM loans.date_release) as year')])
->groupBy('year')
->pluck('year');
it prints following for multiple years
[2018, 2019, 2020]
I think that there are only 2 ways to get what you want
first you need add another column with name year and store data only year and your query will be like this
return DB::table('loans')
->select('year')->distinct('year');
second one is before return you should some algorithm to extract year and return only distinct year it will be like this
$year = DB::table('loans')
->select([DB::raw('YEAR(date_release) as year')])
->groupBy('year')
->value('year');
$distinct_year = array();
foreach($year as $item){
$years = date('Y', strtotime($item->year))
if(!in_array($years, $distinct_year)){
array_push($distinct_year, $years);
}
}
return $distinct_year;
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');```
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
I am using laravel 4. But I am facing problem with finding the difference between two date: one coming from database table and another one is current datetime. From the date difference I am expecting 1 hour or 1 day. I've tried few solution but can't fix this yet. And also I don't know the better way to solve it. If you guys have any solution, please provide me an example. Please tell me if I need any library. Here is my code:
$lecture_id = Input::get('lecture_id');
$delegate_id = Input::get('delegate_id');
// $newDate = new Datetime();
$lecture = Lecture::find($lecture_id);
// $lec_date = Date::forge($lecture->start_time);
// $lec_date = new Datetime($lecture->start_time);
$lec_date = $lecture->start_time->diffForHumans(Carbon::now());
if ( $lec_date > 1) {
LectureDelegate::create(array(
'lecture_id' => Input::get('lecture_id'),
'delegate_id'=> Input::get('delegate_id')
));
return Redirect::to('/')->with('message', 'Your are successfully apply to the lecture');
}
Should be:
$lec_date = Carbon::createFromTimeStamp( strtotime( $lecture->start_time ) )->diffForHumans();
or possibly:
$lec_date = $lecture->start_time->diffForHumans();
If you add this to your Lecture.php model:
public function getDates()
{
return array('created_at', 'updated_at', 'deleted_at', 'start_time');
}
From the documentation:
By default, Eloquent will convert the created_at, updated_at, and
deleted_at columns to instances of Carbon...
You may customize which fields are automatically mutated, and even
completely disable this mutation, by overriding the getDates method of
the model.
As for diffForHumans the documentation states:
The lone argument for the function is the other Carbon instance
to diff against, and of course it defaults to now() if not specified.
update
If the timestamp from the database being passed to diffForHumans is in the future, Carbon automatically makes the return like:
When comparing a value in the future to default now:
1 hour from now
5 months from now
When comparing a value in the past to another value:
1 hour before
5 months before