Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

djmhdi's avatar

Save carbon date toDateString

Hi , i want store a model record , i have RULE VALIDATION DATE the problem is not accept dates with days greater than 12 . For example : it allow 2017-08-03 and deny 2017-08-15 .

        $commande = Commande::create([
        'id'                         =>  $request->get('id'),
        'date_cmd'                   => Carbon::createFromFormat('Y-m-d',$date_tmp3)->toDateString(),
        'validation_fournisseur'     =>  $request->get('validation_client'),
        'validation_client'          =>  $request->get('validation_fournisseur'),
        'user_id'                    =>  $request->get('superClient'), 
        ]);

I'm using laravel 5.4, Can you help me to resolve my problem ?

0 likes
5 replies
aurawindsurfing's avatar

Nah it works as expected on my end:

echo \Carbon\Carbon::createFromFormat('Y-m-d', '2017-08-15')->toDateTimeString();

2017-08-15 00:26:20

Note if you ommit the 'H' it will still generate an hour and minutes so I might be wrong but your validation might not work as expected

You should be using instead something like that:

$rules = [
    'start_at'      => 'required|date|date_format:Y-m-d|before:end_at',
    'end_at'        => 'required|date|date_format:Y-m-d|after:start_at',
];
djmhdi's avatar

why i use 'start_at' and 'end_at' i want to store'date_cmd'

36864's avatar

the problem is not accept dates with days greater than 12

Is this a database error or a php exception?

Sounds like you're trying to use Y-m-d when something expects Y-d-m, or the other way around. Check the format of $date_tmp3 and the date format on your database.

djmhdi's avatar

I have exception validation of rule date .

I can't able to change the default date format in table MySql. Default DATE format is 'YYYY-MM-DD'.

I change format $date_tmp3 by this method :

        $date_tmp1 = $request->get('date_cmd');
        $date_tmp2 = str_replace('/', '-', $date_tmp1);
        $date_tmp3=date('Y-m-d', strtotime($date_tmp2));
djmhdi's avatar
djmhdi
OP
Best Answer
Level 1

I find a solution ,i use DatePicker i must add changeMonth: true and changeYear: true

$('#datepicker').datepicker({ autoclose: true, format: "dd-mm-yyyy", changeMonth: true, changeYear: true });

Thank you for all

Please or to participate in this conversation.