gpt4 book ai didi

laravel - 如何在 laravel 中将输入字符串转换为 strtotime

转载 作者:行者123 更新时间:2023-12-02 15:01:39 25 4
gpt4 key购买 nike

我正在使用 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>
<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>

我从表单中获取 09-03-2018 形式的值。

模型代码,我尝试用 mutator 转换它,但它对模型代码不起作用

use SoftDeletes;
protected $fillable =
['name','valid_from','valid_to','city_id','p_limit','is_active','type'];
protected $dates = [
// 'valid_from',
// 'valid_to',
'deleted_at'
];
// public function setDateOfBirthAttribute($dates)
// {
// $this->attributes['valid_from'] = Carbon::parse($dates);
// $this->attributes['valid_to'] = Carbon::parse($dates);
// $this->attributes['deleted_at'] = Carbon::parse($dates);
// }
// protected function getDateFormat()
// {
// return 'U';
// }
// protected function getDateFormat()
// {
// return 'd-m-Y ';
// }
// public function setFirstNameAttribute($dates)
// {
// $this->attributes['valid_from'] = strtotime($dates);
// $this->attributes['valid_to'] = strtotime($dates);
// $this->attributes['deleted_at'] = strtotime($dates);
// }
// public function getValidTillValueAttribute($date)
// {
// return Carbon::now()->toRfc2822String($date);
// }
// public function setValidTillValueAttribute($value)
// {
//// $this->attributes['valid_to'] =
Carbon::createFromTimestamp($value);
// $this->attributes['valid_to'] = strtotime($value);
// dd($value);
// }
// public function setValidFromValueAttribute($value)
// {
// dd($value);
//// $this->attributes['valid_from'] =
Carbon::createFromTimestamp($value);
// $this->attributes['valid_from'] = strtotime($value);
// }

Controller 代码

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_timestamp 任何帮助将不胜感激

最佳答案

您可以使用 strtotime()

将字符串转换为日期时间戳

这是一个例子

strtotime('09-03-2018');

另一种方法是使用碳

Carbon::parse('string')->timestamp;

还要确保你的模型在 $fillable 数组中有这些键

$fillable = ['your colums'];

在你的 Controller 中你这样做

$input['valid_from'] = strtotime($date);
$input['valid_to'] = strtotime($date1);

Promotion::create($input);

希望对你有帮助

关于laravel - 如何在 laravel 中将输入字符串转换为 strtotime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com