在 PHP LARAVEL 中管理 POST 时间输出
Manage POST time output in PHP LARAVEL
在智能 时间管理器 上工作,通过表单发送的信息,我想转换表单时间输出,捕获:
public function getFormOutput(Request $request)
{
$request->except('_token');
$startTimeInput = $request->input('starttime');
$endTimeInput = $request->input('endtime');
$breakTimeInput = $request->input('breaktime');
}
这里没问题,数值格式像"xx:xx"
我现在正在尝试计算 startTime 和 endTime 之间的时差。
使用 strtotime()
是行不通的 :
$startTime = strtotime($startTimeInput);
$endTime = strtotime($endTimeInput);
他们都取值:
1552550400
这似乎是 1970 年 1 月 1 日的时间(以毫秒为单位)。
这是我的 timeManager.blade.php 观点:
{!! Form::open(['url'=>'agenda']) !!}
{!! Form::label('starttime', 'Heure de début: ') !!}
{!! Form::input('time', 'starttime') !!}
<tr>
{!! Form::label('endtime', 'Heure de fin: ') !!}
{!! Form::input('time', 'endtime') !!}
<tr>
{!! Form::label('breaktime', 'Temps de pause: ') !!}
{!! Form::input('time', 'breaktime') !!}
<tr>
{!! Form::submit('Envoyer' ) !!}
{!! Form::close() !!}
我的问题是:如何计算开始时间和结束时间之间的差异?
试试这个代码来计算时差:
第一种方法:
// calculate the start timestamp
$startdatetime = strtotime($startTimeInput);
// calculate the end timestamp
$enddatetime = strtotime($endTimeInput);
// calulate the difference in seconds
$difference = $enddatetime - $startdatetime;
// hours is the whole number of the division between seconds and SECONDS_PER_HOUR
$hoursDiff = $difference / 3600;
// and the minutes is the remainder
$minutesDiffRemainder = $difference % 3600;
// output the result
echo $hoursDiff . "hours " . $minutesDiffRemainder . "mins";
第二种方法:
$datetime1 = new DateTime($startTimeInput);
$datetime2 = new DateTime($endTimeInput);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y years, %M months, %D days, %I minutes, %S seconds');
希望对您有所帮助。
在智能 时间管理器 上工作,通过表单发送的信息,我想转换表单时间输出,捕获:
public function getFormOutput(Request $request)
{
$request->except('_token');
$startTimeInput = $request->input('starttime');
$endTimeInput = $request->input('endtime');
$breakTimeInput = $request->input('breaktime');
}
这里没问题,数值格式像"xx:xx"
我现在正在尝试计算 startTime 和 endTime 之间的时差。
使用 strtotime()
是行不通的 :
$startTime = strtotime($startTimeInput);
$endTime = strtotime($endTimeInput);
他们都取值:
1552550400
这似乎是 1970 年 1 月 1 日的时间(以毫秒为单位)。
这是我的 timeManager.blade.php 观点:
{!! Form::open(['url'=>'agenda']) !!}
{!! Form::label('starttime', 'Heure de début: ') !!}
{!! Form::input('time', 'starttime') !!}
<tr>
{!! Form::label('endtime', 'Heure de fin: ') !!}
{!! Form::input('time', 'endtime') !!}
<tr>
{!! Form::label('breaktime', 'Temps de pause: ') !!}
{!! Form::input('time', 'breaktime') !!}
<tr>
{!! Form::submit('Envoyer' ) !!}
{!! Form::close() !!}
我的问题是:如何计算开始时间和结束时间之间的差异?
试试这个代码来计算时差:
第一种方法:
// calculate the start timestamp
$startdatetime = strtotime($startTimeInput);
// calculate the end timestamp
$enddatetime = strtotime($endTimeInput);
// calulate the difference in seconds
$difference = $enddatetime - $startdatetime;
// hours is the whole number of the division between seconds and SECONDS_PER_HOUR
$hoursDiff = $difference / 3600;
// and the minutes is the remainder
$minutesDiffRemainder = $difference % 3600;
// output the result
echo $hoursDiff . "hours " . $minutesDiffRemainder . "mins";
第二种方法:
$datetime1 = new DateTime($startTimeInput);
$datetime2 = new DateTime($endTimeInput);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y years, %M months, %D days, %I minutes, %S seconds');
希望对您有所帮助。