Laravel 如何获取当前时间和日期值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44739199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel how to get current Time and Date value
提问by Dasun
I`m developing an Attendance Management system.so that my supervisor ask me to develop following view to get attendance time and date value.
我正在开发一个考勤管理系统,以便我的主管要求我开发以下视图以获得考勤时间和日期值。
All the records need to be saved in the database based on Trainee Id.according to that view if one having above ID check the box and then press the mark button that time value and Trainee id must be saved in the database as on time.
Then if once mark it Date value also need to be saved in the database.
- Then again check it and press the mark button it must go their as Off time.only 2 records need to be allowed otherwise their can be many on off values.
所有记录都需要根据受训者ID保存在数据库中。根据该视图,如果具有以上ID的人选中该框,然后按标记按钮,时间值和受训者ID必须按时间保存在数据库中。
那么如果一旦标记它日期值也需要保存在数据库中。
- 然后再次检查它并按下标记按钮,它必须作为关闭时间。只需要允许 2 条记录,否则它们可能有很多开启关闭值。
can anyone suggest me the relevant controller function for that?
谁能建议我相关的控制器功能?
here is the relevant above view
这是上面的相关视图
<table class="table table-striped">
<thead>
<th>Trainee ID</th>
<th>Name with Initials</th>
<th>Time</th>
<th>Mark Here!</th>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{ $item->trainee_id }}</td>
<td>{{ $item->name_with_initials }}</td>
<td>
<label><input type="checkbox" value=""> Time</label>
</td>
<td>
<a class="btn btn-info" href="Bankdetails/{{ $item->trainee_id }}">Mark Here</a>
</td>
</tr>
@endforeach
</tbody>
</table>
回答by Exprator
try this
尝试这个
public function store(Request $request)
{
$request['time']=date("Y-m-d h:i:s a", time());
attendance::create($request->all());
return view('traineeattendance.attendanceo');
}
回答by Leo
Laravel has the Carbon dependency attached to it.
Laravel 附加了 Carbon 依赖项。
Carbon::now()
, include the Carbon\Carbon
namespace if necessary.
Carbon::now()
,Carbon\Carbon
如有必要,包括命名空间。
Edit (usage and docs)
编辑(用法和文档)
Say I want to retrieve the date and time and output it as a string.
假设我想检索日期和时间并将其作为字符串输出。
$mytime = Carbon\Carbon::now();
echo $mytime->toDateTimeString();
This will output in the usual format of Y-m-d H:i:s
, there are many pre-created formats and you will unlikely need to mess with PHP date time strings again with Carbon.
这将以通常的格式输出Y-m-d H:i:s
,有许多预先创建的格式,您不太可能需要再次使用 Carbon 弄乱 PHP 日期时间字符串。
Documentation: https://github.com/briannesbitt/Carbon
文档: https://github.com/briannesbitt/Carbon
String formats for Carbon: http://carbon.nesbot.com/docs/#api-formatting
Carbon 的字符串格式: http://carbon.nesbot.com/docs/#api-formatting
回答by Jaskaran Singh Puri
You might want to use Carbon
function here
你可能想在Carbon
这里使用函数
Refer some docs: https://github.com/briannesbitt/Carbon