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

Neeraj1005's avatar

Carbon date format How to get date in this format=>2020-05-09 11::14PM

This date is stored in my database 2020-05-09 23:14:00 But I want to get the date in the format of 2020-05-09 11::14PM How can I get this?

#this is my blade this is the script
<div class="col">
                                    <div class="form-group">
                                        <label>Due date:</label>
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">
                                                    <i class="far fa-calendar-alt"></i>
                                                </span>
                                            </div>
                                            <input
                                            type="text"
                                            class="form-control float-right"
                                            id="duetime"
                                            name="due_time"
                                            value="{{$task->due_time}}"
                                            />
                                        </div>
                                    </div>
                                </div>
#script
$("#duetime").daterangepicker({

                // startDate: moment().startOf('hour'),
                // startDate: moment().add(1, 'days'),
                startDate: false,
                // minDate: moment().add(1, 'days'),//This will not do accessing the previous date
                minYear: 1901,
                // maxYear: parseInt(moment().format('YYYY'),10),//For this year not show future years
                showDropdowns: true,
                singleDatePicker: true,
                timePicker: true,
                timePicker24Hour: false,
                timePickerIncrement: 05,
                drops:"up",
                locale: {
                    format: 'MM/DD/YYYY hh:mm A'
                }
            });

and I'm using the daterangepicker script https://www.daterangepicker.com/#examples

0 likes
11 replies
Nakov's avatar

by changing this:

format: 'MM/DD/YYYY hh:mm A'

to this:

format: 'YYYY-MM-DD hh:mmA'

The Carbon way would be this:

$now = Carbon\Carbon::parse('2020-05-09 23:14:00');
  
$now->format('Y-m-d h:iA')
Neeraj1005's avatar

@nakov @tray2 In my edit front page I'm getting result in due_time field is like

05/09/2020 12:00 AM But the date stored in my database is 2020-05-09 21:15:00

I'm getting the correct date but time is not getting proper. Is this a problem of script or code?

Nakov's avatar

@neeraj1005 make sure in your task model you have this:

protected $dates = ['due_time'];

this will give you a Carbon instance.

Then you can also add this to your model:

public function getDueTimeAttribute()
{
    return $this->due_time->format('Y-m-d h:iA');
}	

If that field is nullable you can use optional() or ?? null to set a default as well.

Neeraj1005's avatar

@nakov This is my model I already set the dates field

<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Todo extends Model
{
    use SoftDeletes;

    protected $fillable = [

        'title',
        'description',
        'user_id',
        'outcome_id',
        'tasktype_id',
        'due_time',
        'complete_time',
    ];

 	protected $casts = [
        'due_time' => 'datetime',
        'complete_time',
    ];

    protected $dates = [
        'due_time',
        'complete_time',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function setDueTimeAttribute($date)
    {

        // $this->attributes['due_time'] = Carbon::createFromFormat('d-m-Y H:i:s', $date);
        $this->attributes['due_time'] = Carbon::parse($date)->format('Y-m-d H:i:s');;
    }

    public function outcome() {

        $this->belongsTo('App\Outcome');
    }

    public function tasktype() {

        $this->belongsTo('App\Tasktype');
    }


}

Neeraj1005's avatar

@nakov This error comes

"Undefined property: App\Todo::$due_time (View: C:\xampp\htdocs\crm\resources\views\taskmanagement\cruds\edit.blade.php)"
<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Todo extends Model
{
    use SoftDeletes;

    protected $fillable = [

        'title',
        'description',
        'user_id',
        'outcome_id',
        'tasktype_id',
        'due_time',
        'complete_time',
    ];

 	protected $casts = [
        'due_time' => 'datetime',
        'complete_time',
    ];

    protected $dates = [
        'due_time',
        'complete_time',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function setDueTimeAttribute($date)
    {

        // $this->attributes['due_time'] = Carbon::createFromFormat('d-m-Y H:i:s', $date);
        $this->attributes['due_time'] = Carbon::parse($date)->format('Y-m-d H:i:s');;
    }

    public function getDueTimeAttribute($date)
    {
        return $this->due_time->format('Y-m-d h:iA');
    }


}

Nakov's avatar
Nakov
Best Answer
Level 73

Okay @neeraj1005

then try this:

public function getDueTimeAttribute($date)
{
    return  Carbon::parse($date)->format('Y-m-d h:iA');
}

Again you should know if the due_time can be null or not. If it can be, then you should add the check, otherwise it will throw an exception.

1 like
Neeraj1005's avatar

@nakov Date is getting in proper format now but in my front-end this select date not selecting the date.plz see this image in description box I have called the date https://imgur.com/0taVCch

This is the script datetimerangepicker
      $("#duetime").daterangepicker({

                // autoUpdateInput: false,
                startDate: false,
                minYear: 1901,
                showDropdowns: true,
                singleDatePicker: true,
                timePicker: true,
                timePicker24Hour: false,
                timePickerIncrement: 05,
                drops:"up",
                locale: {
                    format: 'MM/DD/YYYY hh:mm A'
                    // format: 'YYYY/MM/DD hh:mm A'
                }
            });
Nakov's avatar

Oh @neeraj1005 this is taking years now :)

Remove now :

public function getDueTimeAttribute($date)
{
    return  Carbon::parse($date)->format('Y-m-d h:iA');
}

because it is not what you need, and use this

value="{{$task->due_time->timestamp}}"

and let Moment JS format your date.

Neeraj1005's avatar

@nakov using Timestamp I'm getting this type of result.

1589114580

Now I am getting proper thing what I basically want's. I made some little change in datetimerangepicker script.

            $("#duetime").daterangepicker({

                autoUpdateInput: false,
                startDate: false,
                minYear: 1901,
                showDropdowns: true,
                singleDatePicker: true,
                timePicker: true,
                timePicker24Hour: false,
                timePickerIncrement: 05,
                drops:"up",
                locale: {
                    // format: 'MM/DD/YYYY hh:mm A'
                    format: 'DD/MM/YYYY hh:mm A'
                },
            });
            $("#duetime").on('apply.daterangepicker', function(ev, picker) {
                $(this).val(picker.startDate.format('DD/MM/YYYY hh:mm A'));
            });

            $("#duetime").on('cancel.daterangepicker', function(ev, picker) {
                $(this).val('');
            });

Please or to participate in this conversation.