Laravel how to add minute to time - laravel

I want to make estimate time when user will come. I have 2 variable to save, open and closed time of the store.
$openTime = Carbon::parse($open);
$closedTime = Carbon::parse($closed);
i can get the duration between 2 variable. And i can get duration for each person who come.
$Duration = $startTime->diffInSeconds($endTime);
$perpersonDuration = gmdate("H:i:s", $Duration/$quota);
The question is how to add $perpersonduration to time. In this case i want to add $perpersonduration with $opentime. So it will be like $opentime + $perpersonduration.
Please help

In Carbon there is a method called addMinutes() and you can pass the number of minutes as you won't like this
$openTime = Carbon::now(); // 02:00:00
$closeTime = Carbon::parse($openTime)->addMinutes(30); // 02:30:00

Related

Dax First time pass rate

I'm trying to create a measure to calculate first time past rate for a location. 
So if I have a day where there are ten failures and one pass on an item, it would be a first time pass rate of zero%.
So the First time pass rate should be 60%:
FirsTimePassed = CALCULATE(MIN(ATP[Result Date]), ALLEXCEPT(ATP,ATP[Location Name]),FILTER(ATP,ATP[Pass/Fail]="Pass"))
firstime = CALCULATE(MIN(ATP[Result Date]), ALLEXCEPT(ATP,ATP[Location Name]))
FirstTimePassedCount =
VAR SerailCount = CALCULATE(DISTINCTCOUNT(ATP[Location Name]),ALL(ATP))
VAR PassedCount = CALCULATE(DISTINCTCOUNT(ATP[Location Name]),FILTER(ATP,ATP[firstime]=ATP[FirsTimePassed]))
Return PassedCount/SerailCount

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

Shopify Time difference

I am newbie to shopify, I have to display a time counter up-to 9 PM everyday. The logic includes to deduct current time from specified time and the difference time will display as counter(remaining time).
I am able to retrieve current timestamp in shopify using the code below
{% assign timestamp = 'now' | date %}
Now I have a date "21-03-2016 21:00:00" and wants to convert in timestamp but not able to get the solution.
Let me know if any one can help me in this. Thank You.
Note that the value will be the current time of when the page was last
generated from the template, not when the page is presented to a user
if caching or static site generation is involved.
http://shopify.github.io/liquid/filters/date/
This is probably best done using Javascript, not pure-Liquid. You generally want your pages to be as cache-able as possible for Shopify's servers to keep loading times as lean as possible.
You will want to use Liquid to compensate for timezones, however. You can get the store's configured timezone and drop that into a Javascript variable:
var timezone = {{ 1 | date: '%z' | json }};
The above liquid statement takes some arbitrary input, runs it through the date filter and outputs only the timezone component configured for that store (using '%z'), then runs that through the json filter to ensure that whatever the output will always be javascript-legal.
Now that you know your timezone offset, you can assemble your timer logic in Javascript using all your normal date/time tricks. For example:
var now = new Date();
var nowStr = now.toISOString(); // Output format: '2019-01-01T00:00'
// Chop off the time portion, then append your desired time & store timezone
var closingTimeStr = nowStr.split('T')[0] + 'T21:00' + timezone;
// We make a new date using today's date/time string
var closingTime = new Date(closingTimeStr);
// And now we can do math based on the difference
var difference_ms = closingTime - now;
if(difference_ms > 0){
// Parse the difference into hours, minutes and seconds if a positive amount of time remains. Writing a better parsing function is left as an exercise for the reader.
var hours = parseInt(difference_ms / (60 * 60 * 1000) );
difference_ms %= (60 * 60 * 1000);
var minutes = parseInt(difference_ms / (60 * 1000) );
difference_ms %= (60 * 1000);
var seconds = parseInt(difference_ms / (1000) );
console.log('Hurry down! Closing in ' + hours + ' hours, ' + minutes + ' minutes, and ' + seconds + ' seconds!');
} else {
// Show appropriate message if we're out-of-bounds
console.log('Closed for today - try again tomorrow!');
}

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

How to get datetime differance in laravel 4

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

Resources