Hey @vkot91
If they are both instances of Carbonthen you can simply compare them like this:
$difference = $startDate->diffInDays($endDate);
Hope it helps!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello guys,i have two tables , rooms and bookings:
public function up()
{
if(! Schema::hasTable('bookings')) {
Schema::create('bookings', function (Blueprint $table) {
$table->increments('id');
$table->datetime('time_from')->nullable();
$table->datetime('time_to')->nullable();
$table->integer('diff_days')->nullable();
$table->text('additional_information')->nullable();
$table->string('first_name');
$table->string('last_name');
$table->string('address')->nullable();
$table->string('phone')->nullable();
$table->string('email');
$table->integer('all_price')->nullable();
$table->string('isActive')->default('Inactive');
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
}
public function up()
{
if(! Schema::hasTable('rooms')) {
Schema::create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('room_number');
$table->integer('floor')->nullable();
$table->text('description')->nullable();
$table->integer('price');
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
}
Its my BookingController.php
public function create()
{
if (!Gate::allows('booking_create')) {
return abort(401);
}
$rooms = Room::get()->pluck('room_number', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$users = User::get()->pluck('id','id')->prepend(trans('quickadmin.qa_please_select'),'');
$rooms2 = Room::get()->pluck('price', 'id');
return view('admin.bookings.create', compact('rooms','users','rooms2'));
}
And some code frome my booking creating:
<div class="col-xs-12 form-group">
{!! Form::label('email', trans('quickadmin.bookings.fields.email').'*', ['class' => 'control-label']) !!}
{!! Form::email('email', old('email'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('email'))
<p class="help-block">
{{ $errors->first('email') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('room_id', trans('quickadmin.bookings.fields.room').'', ['class' => 'control-label']) !!}
{!! Form::select('room_id', $rooms, old('room_id'), ['class' => 'form-control ','placeholder' => '','required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('room_id'))
<p class="help-block">
{{ $errors->first('room_id') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('time_from', trans('quickadmin.bookings.fields.time-from').'*', ['class' => 'control-label']) !!}
{!! Form::text('time_from', old('time_from'), ['class' => 'form-control datetimepicker','id'=>'datetimepicker1', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('time_from'))
<p class="help-block">
{{ $errors->first('time_from') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('time_to', trans('quickadmin.bookings.fields.time-to').'*', ['class' => 'control-label']) !!}
{!! Form::text('time_to', old('time_to'), ['class' => 'form-control datetimepicker','id'=>'datetimepicker2', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('time_to'))
<p class="help-block">
{{ $errors->first('time_to') }}
</p>
@endif
</div>
</div>
Create dates with Carbon in my Booking php
public function setTimeFromAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['time_from'] = Carbon::createFromFormat(config('app.date_format') . ' H:i', $input)->format('Y-m-d H:i');
} else {
$this->attributes['time_from'] = null;
}
}
public function getTimeFromAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
} else {
return '';
}
}
public function setTimeToAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['time_to'] = Carbon::createFromFormat(config('app.date_format') . ' H:i', $input)->format('Y-m-d H:i');
} else {
$this->attributes['time_to'] = null;
}
}
public function getTimeToAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i');
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
} else {
return '';
}
}
Now i want to check this two dates and calculate total price by multiplying diffence between this two dates and room price.How i can do it?
Change the creation to this:
// get input as an array
$attributes = $request->all();
// add a new key to the attributes
$attributes['diff_days'] = $diff_days;
$booking = Booking::create($attributes);
This will add a new value to the reuslt of $request->all()
The way you were doing you will end up with an array inside other array which Laravel doesn't know how to parse.
Please or to participate in this conversation.