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

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

Related

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

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 :)

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;

Laravel Date comparison not working in Eloquent query

I do not understand why but following query return null resultset.
due_date is Carbon date and $now=Carbon:today();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>=',$now)
->where('due_date','<',$now->addMonth())
->get();
Also when I use whereBetween it doesn't work.
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$now, $now->addMonth()])
->get();
But when I just to greater than or lesser than it works
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>',$now->addWeek())
->get();
What am I missing here?
The problem here is that you are using the same instance for both range limits. When you call addMonth you add the month to the instance stored in $now. The two examples below illustrate the issue:
1. Using and modifying the same variable in two separate statements works as you'd expect:
$now = Carbon::now();
dump($now); // prints 2015-12-12 14:50:00.000000
dump($now->addMonth); // prints 2016-01-12 14:50:00.000000
2. Using the same variable and modifying it in the same statement that passes the values to a method, will work differently, because it will be evaluated before being passed to the method. Meaning that both parameters will be equal because they both contain the same instance from the $now variable, which after getting evaluated will contain the DateTime of one month from now.
$now = Carbon::now();
// Calling `addMonth` will change the value stored in `$now`
dump($now, $now->addMonth());
// The above statement prints two identical DateTime values a month from now:
// 2016-01-12 14:50:00.000000 and 2016-01-12 14:50:00.000000
This means that your current code was checking if the entries were due only exactly one month from now.
To fix it you need to use two instances in two separate variables:
$from = Carbon::now();
$to = Carbon::now()->addMonth();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$from, $to])
->get();
It looks like it is because I used '$now' in the query.
Like is said before the query I did $now=Carbon::today(); and use $now in the query.
But then I got rid of that and changed the query to use Carbon::today() it worked.
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[Carbon::today(), Carbon::today()->addMonth())
->get();
It is weird.
Thanks,
K

How to get the month duration in 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

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