如何在laravel中将输入字符串转换为strotime
我使用jQuery日历以如下形式输入日期如何在laravel中将输入字符串转换为strotime,laravel,laravel-5.3,string-to-datetime,Laravel,Laravel 5.3,String To Datetime,我使用jQuery日历以如下形式输入日期 <div class="form-group"> <label>Valid From:</label> <div class="input-group date"> <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div> &l
<div class="form-group">
<label>Valid From:</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" tabindex="4" data-date-start-date="0d" class="form-
control pull-right datepicker" name="valid_from" id="datepicker2" value="
{{old('valid_from')}}">
</div>
@if ($errors->has('valid_from'))
<span class="has-error" style="color: red">
<strong>{{ $errors->first('valid_from') }}</strong>
</span>
@endif
</div>
控制器代码
public function store(PromotionRequest $request)
{
$input = $request->all();
// return $request->all();
$date = $request->valid_from;
$date1 = $request->valid_to;
$_newDate = strtotime($date);
$_newDate1 = strtotime($date1);
// dd($_newDate);
// dd($_newDate1);
// return $input;
// return $_newDate1;
// return $_newDate;
// dd($request->all());
// return $request->$_newDate;
// return $request->$_newDate1;
Promotion::create($input,$_newDate,$_newDate1);
// Promotion::create($input);
// return redirect('admin/promotion');
}
迁移代码
$table->increments('id');
$table->string('name')->unique();
$table->integer('valid_from');
$table->integer('valid_to');
$table->tinyInteger('is_active')->comment('0 = Not Active, 1 = Active');
$table->tinyInteger('type')->comment('1 = percentage, 2 = in currency');
$table->integer('city_id')->default(1)->comment = "foreign key to cities
table";
$table->integer('p_limit')->unsigined();
$table->integer('updated_at')->unsigined();
$table->integer('created_at')->unsigined();
$table->integer('deleted_at')->unsigined()->nullable(true);
$table->foreign('city_id')->refrences('id')->on('cities');
我想将其转换为unix时间戳。非常感谢您的帮助。您可以使用
strotime()将字符串转换为日期时间戳。
这里有一个例子
strtotime('09-03-2018');
另一种方法是使用碳
Carbon::parse('string')->timestamp;
还要确保您的模型在$fillable数组中包含这些键
$fillable = ['your colums'];
在控制器中,您可以执行此操作
$input['valid_from'] = strtotime($date);
$input['valid_to'] = strtotime($date1);
Promotion::create($input);
希望这对你有所帮助friend@ThomasMoors我试过我的朋友,但他(碳)没有回答。我应该在哪里做呢??实际上,我很久以前就用strotime在controller中对它进行了转换,但它并没有存储在数据库prolperyInput=$request->all();/返回$request->all()$日期=$request->valid\u from$date1=$request->valid\u to$_newDate=strottime($date)$_newDate1=strotime($date1);//dd($_newDate);//dd($_newDate1);//返回$input;//返回$\u newDate1;//返回$\u newDate;//dd($request->all());//返回$request->$\u newDate;//返回$request->$\u newDate1;促销:创建($input,$\u newDate,$\u newDate1);如果要将其存储到数据库中,则必须根据要存储的数据库列类型column type对其进行相应的格式化?我知道,它现在是整数,但只有转换值的第一位数字被保存。我想可能是因为整数的长度,但它已经是11了。所以这没有什么问题。
$input['valid_from'] = strtotime($date);
$input['valid_to'] = strtotime($date1);
Promotion::create($input);