How to convert an input string into strtotime in laravel

10,647

You can convert string to date time timestamp using strtotime()

Here is an example

strtotime('09-03-2018');

Another way is to use carbon

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

Also make sure your model has these keys in $fillable array

$fillable = ['your colums'];

In your controller you do this

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

Promotion::create($input);

Hope this helps

Share:
10,647

Related videos on Youtube

Hassaan
Author by

Hassaan

A Software Engineer having the learning attitude of a beginner with a can-do approach. Finding unique ways to solve problems and helping my team in the process, while improving daily. Always up for new and ever-changing tech trends with a cup of coffee. While being an experienced software developer and working with different firms along the way on the products used by thousands of users daily, I have learned and grown a lot (both personally and professionally). Despite that, I feel like there’s way more to go, which motivates and excites me. So far in Web Technologies, I have done Data Modeling, AWS Lambda, EC2, S3, written REST API’s (bringing Data Management into play) and utilizing Data Structure and Algorithms to improve their performance and have worked on React Projects that have seen some huge successes and currently are being used by hundreds of users daily. I have used GitHub, GitLab, Bitbucket for version control. These days I’m exploring ways to improve Microservices and learning TypeScript, CI/CD pipelines, and GCP. I would like to advance my career as a software developer by enhancing my skills along the way in a dynamic and fast-paced workspace.

Updated on June 04, 2022

Comments

  • Hassaan
    Hassaan almost 2 years

    I am using jQuery calendar to input dates in the form like

    <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>
    

    and I am getting the value in the form of 09-03-2018 from form.

    Model code and I tried converting it with mutator but it didn't work to model code

    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 code

    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');
        } 
    

    migration code

    $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');
    

    I want to convert it into unix_timestamp any help would be really appreciated

    • online Thomas
      online Thomas about 6 years
      Carbon is your friend
    • Hassaan
      Hassaan about 6 years
      @ThomasMoors i've tried my friend but he's(carbon) not answering.
  • Hassaan
    Hassaan about 6 years
    where should i be doing that?? actually i've converted it long ago in controller with strtotime but it's not storing in database prolperly
  • Hassaan
    Hassaan about 6 years
    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);
  • PULL STACK DEV
    PULL STACK DEV about 6 years
    if you want to store it into database then you have to format it accordingly to your database column type
  • PULL STACK DEV
    PULL STACK DEV about 6 years
    column type in which you want to store ?
  • Hassaan
    Hassaan about 6 years
    i've, it's integer now but only first digit of that converted value is getting save. i thought it maybe due to the lenght of the integer but its 11 already. so theres is no issue in that.
  • Hassaan
    Hassaan about 6 years
  • PULL STACK DEV
    PULL STACK DEV about 6 years
    @DaShInGSid show your model too. make sure you these values are $fillable array