The problem is that it gives me this error
DateTime::__construct(): Failed to parse time string (17/07/2019) at position 0 (1): Unexpected character
View in my form I have the date and time input separately, but with carbon I want to join them and save them in a single field.
I use Bootstrap 4 and TempusDominus
<div class="col-5 col-xl-5">
<div class="form-group">
<div class="input-group date" name="event_start_date" id="event_start_date" data-target-input="nearest">
<input type="text" name="event_start_date" required="" id="event_start_date" class="form-control datetimepicker-input" data-target="#event_start_date" placeholder="Fecha de inicio"/>
<div class="input-group-append" data-target="#event_start_date" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
<div class="col-4 col-xl-5" id="event_start_time_area" style="display: none">
<div class="form-group">
<div class="input-group date" id="event_start_time" data-target-input="nearest">
<input type="text" name="event_start_time" id="event_start_time" value="00:00" class="form-control datetimepicker-input" data-target="#event_start_time" placeholder="Hora de inicio"/>
<div class="input-group-append" data-target="#event_start_time" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-clock"></i></div>
</div>
</div>
</div>
</div>
the error refers directly to the line $dataTimeFecha_i = new Carbon($fecha_i);
of my controller
try this way $dataTimeFecha_i = new DateTime($fecha_i);
but i get the same error
$fecha_i= $request['event_start_date'];
$dataTimeFecha_i = new Carbon($fecha_i);
$hora_inicio = $request['event_start_time'];
$fecha_hora_inicio = Carbon::instance($dataTimeFecha_i)->setTimeFromTimeString($hora_inicio)->toIso8601String();
This is the configuration of my script regarding the Tempus Dominus format
<script type="text/javascript">
$(function () {
$('#event_start_date').datetimepicker({
locale: 'es',
format: 'DD/MM/YYYY',
minDate: "now",
pickTime: false
});
$('#event_end_date').datetimepicker({
locale: 'es',
format: 'DD/MM/YYYY',
minDate: "now",
pickTime: false
});
$('#event_start_time').datetimepicker({
locale: 'es',
format: 'LT'
});
$('#event_end_time').datetimepicker({
locale: 'es',
format: 'LT'
});
});
</script>
Carbon can recognize some common formats , but for a string like
$fecha_i = "17/07/2019"
it won't be able to recognize it without you specifying it. You can usecreateFromFormat
, passing it the correct format:You must parse the date:
That should be it.