I have an Laravel app that has a time limitation for task completion, like 10 am till 11 am. Users should be able to complete their tasks in their local time. so out of the time limit boundaries they will get a save not allowed message.
The app default time zone is UTC. and limitation is defined in task type as a datetime entry like: limit_start
= 2019/02/03 10:00:00
, limit_end
2018/05/16 11:00:00
. And as you can see after retrieving data from task type as it is a daily task I only use the time part of the data and the date part is not important.
This is my limitation implementation right now:
private function dailyLimitation()
{
$timezone = auth()->user()->timezone;
/** @var Carbon $limit_start */
/** @var Carbon $limit_end */
$limit_start = $this->limit_start;
$limit_end = $this->limit_end;
$user_time = Carbon::parse(Timezone::ConvertToUTC(now(),$timezone));
$limit_start->year($user_time->year);
$limit_start->month($user_time->month);
$limit_start->day($user_time->day);
$limit_end->year($user_time->year);
$limit_end->month($user_time->month);
$limit_end->day($user_time->day);
$diff = new \stdClass();
$interval = $limit_start->diff($user_time);
if ($interval->invert)
{
$diff->diff = $limit_start->diffForHumans($user_time);
$diff->type = 'before';
$diff->allowed = false;
return $diff;
}
$interval = $limit_end->diff($user_time);
if ($interval->invert)
{
$diff->diff = $limit_end->diffForHumans($user_time);
$diff->type = 'ok';
$diff->allowed = true;
return $diff;
}
$diff->diff = $limit_end->diffForHumans();
$diff->type = 'past';
$diff->allowed = false;
return $diff;
}
any help would be appreciated regarding implementation of this process.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire