Carbon its parsing my date range +1 day - laravel

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;

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

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

Calculate the average for the last 30 days excluding today. DAX

I would like to calculate the average revenue for the last 30 days (not including today)
I tried the following formula but the amount calculated is incorrect:
CALCULATE(
AVERAGE(table[Revenue]),
FILTER(table,DATEADD(table[date],-30,DAY))
)
How can I exclude today in the average?
If i wanted to compare that result with the 30 days before that (i.e between -30 days and -60days) should i use datesinperiod?
The DATESBETWEEN function is the most intuitive to me.
Previous30DayAverage =
VAR CurrentDate = LASTDATE(table[date]) --Or TODAY() or whatever you choose
RETURN
CALCULATE(
AVERAGE(table[Revenue]),
DATESBETWEEN(table[date], CurrentDate - 30, CurrentDate - 1)
)
I think you can see how to tweak this for -30 to -60 days.

Get what date it is based on week number and day number asp classic?

I need to know what date it is based on a week number and a day number.
So if I have weeknr=3 and day=1(for today- sunday) and lets say the year=2016 how can I make this into 2016-01-24.
Any input really appreciated, thanks.
Your best help will be the DatePart function (see here for docs). It's not altogether straightforward, because there are options to consider like "first day of week" and "first week of year", but you can define those in DatePart.
This would be my solution:
option explicit
function getDateByWeek(year, week, day)
dim dt, woy, add_days
dt = DateSerial(year, 1, 1)
do
woy = DatePart("ww", dt, vbSunday, vbFirstFullWeek)
' possibly change options [,firstdayofweek[,firstweekofyear]]
dt = DateAdd("d", 1, dt)
loop while woy<>1
add_days = week * 7 + day - 2
' -1 because we already added one day extra in the loop
' -1 to correct given day (sunday = 1)
getDateByWeek = DateAdd("d", add_days, dt)
end function
Response.Write "RESULT: " & getDateByWeek(2016, 3, 1) ' -> 24.01.2016
I start by finding the first day of the first week in a loop and then adding the cumulative amount of days to have a result.
Well the best way is to work out what the week start of the year is and take it from there.
Firstly, the first Thursday of any year is always in the first week of the year, so a simple calculation of adding the correct number onto the 1st January is obvious. After this we simply mulitply out the weeks and add the days:
Const C_THURSDAY = 5
Function GetDateFromYWD(y, w, d)
Dim tDate, aDate
'Get the first of the year...
tDate = DateSerial(y, 1, 1)
'Workout the start of the first week in the year...
aDate = tDate + Int(WeekDay(tDate)>C_THURSDAY And 7) - WeekDay(tDate)
'Add on the number of weeks plus days we're looking for...
aDate = aDate + (w * 7) + d
GetDateFromYWD = aDate
End Function

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

Resources