How to count two related dates in Laravel using Query Builder? - laravel

I have this list of dates
I want to count the times that the time is higher than 24 hours between each dates in a row... tried with this ..
$tiempo1 = DB::table('incidencia_tiempo')
->whereTime('time', '>', '0000-00-00 24:00:00')
->count();
Beetween time and created at... I would like to count... if beetween the two times the hours are higher than 24 hours then add +1 to the count
I have to make a comparation with created_at and time and I don't know how

Use TIMESTAMPDIFF() to get the time between two datetime,
If you want to count the time high than the created_at 24 hours
$count = DB::table('incidencia_tiempo')
->where(DB::raw('TIMESTAMPDIFF(HOUR, time, created_at)'), '>', 24)
->count();
if you want to count the time difference between two datetimes high than 24 hours:
$count = DB::table('incidencia_tiempo')
->where(DB::raw('ABS(TIMESTAMPDIFF(HOUR, time, created_at))'), '>', 24)
->count();

what is yoiur mean time is higher than 24 hours?

Try This Method for Compare and Count Dates
$text_count = Text::whereBetween('created_at', [$date1, $date2])->count();

first of all you have to run simple query for getting all the data like
$tiempo1 = DB::table('incidencia_tiempo')->get();
and add one loop for this query data
$count = 0;
foreach($tiempo1 as $time) {
$start = new Carbon($time->time);
$end = new Carbon($time->created_at);
$hours = $start->diff($end)->format('%H');
if ($hours > 24){
$count++;
}
}
hope this will help :)

Related

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');```

Best way to sum time in Laravel?

I have a table called transactions with two relevant fields to my question, _start_timestamp_ and _end_timestamp_. I need to sum the amount of time passed between all transactions where _end_timestamp_ is not null. So, the result must be something like Total Time of Transactions: 1 hour and 18 minutes
I've tried using Carbon, but I don't know how to sum all the lines of the table using it.
foreach($timestampStarts as $timestampStart){
$time = new Carbon($timestampStart->start_timestamp);
$shift_end_time =new Carbon($timestampStart->end_timestamp);
dd($time->diffForHumans($shift_end_time));
}
You can use the MySQL TIMESTAMPDIFF function to calculate the difference:
Transaction::whereNotNull('_end_timestamp_')
->sum(DB::raw('TIMESTAMPDIFF(SECOND, _start_timestamp_, _end_timestamp_)'));
This will give you the total in seconds.
You need to retrieve the total difference in minutes. Add the total difference and retrieve the diff for humans. You can retrieve like below:
$diffForHumans = null;
$nowDate = Carbon::now();
$diffInMinutes = 0;
foreach($timestampStarts as $timestampStart){
if(!empty($timestampStart->end_timestamp)){ // if the end timestamp is not empty
$time = new Carbon($timestampStart->start_timestamp);
$shift_end_time =new Carbon($timestampStart->end_timestamp);
//Adding difference in minutes
$diffInMinutes+= $shift_end_time->diffInMinutes($time);
}
}
$totalDiffForHumans = $now->addMinutes($diffInMinutes)->diffForHumans();

Carbon its parsing my date range +1 day

I have the date range
{"from":"2018-02-12T23:00:00.703Z","to":"2018-02-13T22:59:59.703Z"}
When I do a
$periodFrom = Carbon::parse($request->from);
$periodTo = Carbon::parse($request->to);
And then I want to get the number of dates between those dates I do
$days = $periodTo->diffInDays($periodFrom);
And for some reason its giving me 0 days instead of 1 day
Edit:
You can do this:
$days = round($periodTo->diffInHours($periodFrom) / 24);
Instead of round() you can also use ceil() method, it depends on what exactly result you want to get for 1.2 days, 1.8 days etc.
If you want to get 1 day instead of 0, but for all other cases you want to get just full days (8 for 8.9 for example), do this:
$diff = $periodTo->diffInHours($periodFrom);
$days = $diff === 0 ? 1 : $diff;

Getting daily aggregates/sum sorted by day in Laravel

So getting a sum()/count() is really easy in Laravel...
but how would I look at the past month, and get the sum of rows every day?
EG...grouped by day that they were created at.
So I want to return a count such as 3, 2, 4, 5
Meaning 3 rows were created on todays date, 2 rows yesterday, 4 rows the day before...etc
How to do this in Laravel easily?
When I use the group by created_at it always just returns 1.
Anybody know how to do it?
Thanks
I've provided the same answer on another post. Shortening it:
$date = new DateTime('tomorrow -1 month');
// lists() does not accept raw queries,
// so you have to specify the SELECT clause
$days = Object::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`')
))
->where('created_at', '>', $date)
->group_by('date')
->order_by('date', 'DESC')
->lists('count', 'date');
// Notice lists returns an associative array with its second and
// optional param as the key, and the first param as the value
foreach ($days as $date => $count) {
print($date . ' - ' . $count);
}

Resources