我的模型有一个event_datetime字段,我希望使用日期时间选择字段在表单上设置该字段。 Rails有datetime_select,它输出以下内容并保存到MySQL datetime列。
必须默认为当前日期并使用共享的添加/编辑表单。
请不要使用javascript datepickers。
答案 0 :(得分:5)
将此添加给您
{{ Form::datetime('event_date') }}
将此宏放入一些自动加载的文件(或视图文件)
Form::macro('datetime', function($name) {
$years = value(function() {
$startYear = (int) date('Y');
$endYear = $startYear - 5;
$years = ['' => 'year'];
for($year = $startYear; $year > $endYear; $year--) {
$years[ $year ] = $year;
};
return $years;
});
$months = value(function() {
$months = ['' => 'month'];
for($month = 1; $month < 13; $month++) {
$timestamp = strtotime(date('Y'). '-'.$month.'-13');
$months[ $month ] = strftime('%B', $timestamp);
}
return $months;
});
$days = value(function() {
$days = ['' => 'day'];
for($day = 1; $day < 32; $day++) {
$days[ $day ] = $day;
}
return $days;
});
$hours = value(function() {
$hours = ['' => 'hour'];
for($hour = 0; $hour < 24; $hour++) {
$hours[ $hour ] = $hour;
}
return $hours;
});
$minutes = value(function() {
$minutes = ['' => 'minute'];
for($minute = 0; $minute < 60; $minute++) {
$minutes[ $minute ] = $minute;
}
return $minutes;
});
return Form::select($name.'[year]', $years) .
Form::select($name.'[month]', $months) .
Form::select($name.'[day]', $days) . ' - ' .
Form::select($name.'[hour]', $hours) .
Form::select($name.'[minute]', $minutes);
});