hello friends I have a problem with the dates pause .. in my view I have a datetimepicker of (bootstrap 4) with the format 12/06/2019 12:00:00 and what I need is to pass it to the format that accepts mysql 2019-06-12 12:00:00
try to do it from js but it did not work
$(function () {
$('#startdate').datetimepicker({
useCurrent: false,
format: 'Y-MM-D H:mm',
autoclose:true,
minDate: moment()
});
that's how I'm trying from my controller but it has given me an error :(
$promociones->fecha_inicio = Carbon::createFromFormat('Y-m-d H:i',$request->fecha_inicio)->toDateTimeString();
$promociones->fecha_fin = Carbon::createFromFormat('Y-m-d H:i',$request->fecha_fin)->toDateTimeString();
</div>
I had to investigate a lot and be trial and error but I managed to solve my problem ... thank you very much to all of you who helped me in providing ideas for my solution. Thanks for your help, it helped me a lot to solve my problem.
I started fixing my datetimepicker so
$('#startdate').datetimepicker({
useCurrent: false,
minDate: moment()
});
$('#enddate').datetimepicker({
useCurrent: false,
minDate: moment()
});
I had to remove the format I had given him....after that pass to the controller as some suggested to me ... pass the format that was receiving my input that is d / m / YH: m after that I passed the time zone of my country (in case there was a problem of dates) then I just had to pass the format with which I wanted to save it to the database in my case mysql by default accepts the format Ymd H: i ... and ready problem solved
$promociones->fecha_inicio = Carbon::createFromFormat('d/m/Y H:i',$request->fecha_inicio,'America/El_Salvador')->format('Y-m-d H:i');
$promociones->fecha_fin = Carbon::createFromFormat('d/m/Y H:i',$request->fecha_fin,'America/El_Salvador')->format('Y-m-d H:i');
Just try to use this
\Carbon\Carbon::parse($request->fecha_inicio);
OR Carbon::parse($request->fecha_inicio);
You need to match the incoming format with Carbon::createFromFormat()
, not the outgoing format.
Carbon::createFromFormat('d/m/Y H:i','16/07/2019 10:00')->toDateTimeString();
I'm assuming the first digits are days based on the title since there is no 16th month. It's impossible to tell from 12/06/2019 12:00:00
.
EDIT (timezone issue)
Since you say the timezone is set in config/app.php
, it's also possible to pass the timezone as the third parameter:
$date = Carbon::createFromFormat('d/m/Y H:i','16/07/2019 21:37','America/El_Salvador')->toDateTimeString();
Also, if you have recently made changes to the config, you may need to run:
php artisan config:clear
List timezone: https://www.php.net/manual/en/timezones.php
$promociones->fecha_inicio = Carbon::createFromFormat('Y-m-d H:i',$request->fecha_inicio)->toDateTimeString();
$promociones->fecha_fin = Carbon::createFromFormat('Y-m-d H:i',$request->fecha_fin)->toDateTimeString();
In "config/app.php" the timezone is set this way: 'timezone' => '...'
Ex:
'timezone' => 'America/Guatemala'
Thanks