# Cakephp 4.0 Can't save date data

**URL:** <https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110>\
**Category:** Need Help\
**Created:** [January 16, 2020, 6:01am UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110 "2020-01-16T06:01:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samantha](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Samantha](https://discourse.cakephp.org/u/Samantha)\
**Post date:** [January 16, 2020, 6:01am UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110/1 "2020-01-16T06:01:04Z")

</div>

I tried to add new data for start date & end date. The data entered the array but somehow there’s error “Call to a member function format() on null” at index.php, line 33.

 ![index](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/91ad3ea503600950934b96bcc28743c60958dc3f.png)

This is my SaleController Function Add:  
public function add()  
{  
$sale = $this-\>Sales-\>newEmptyEntity();

```
        // pr($sale);die;
    // $data = $this->request->getData();
    // pr($data);exit;
    if ($this->request->is('post')) { 
        $data = $this->request->getData();
        // pr($data);exit;
        $date['start_date'] = !empty($data['start_date']) ? date('d-m-Y', strtotime($data['start_date'])) : '';
        $date['end_date'] = !empty($data['end_date']) ? date('d-m-Y', strtotime($data['end_date'])) : '';
        // $start_date = !empty($data['start_date']) ? date('Y-m-d', strtotime($sale->start_date->format('Y-m-d H:i:s')));
        // $end_date = !empty($data['end_date']) ? date('Y-m-d', strtotime($sale->end_date->format('Y-m-d H:i:s')));
        $sale = $this->Sales->patchEntity($sale, $data); 
        if ($this->Sales->save($sale)) {
            $this->Flash->success(__('The sale has been saved.'));
     
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The sale could not be saved. Please, try again.'));
    }
    $vehicles = $this->Sales->Vehicles->find('list', ['limit' => 200]);

    $users = $this->Sales->Users->find('list', ['limit' => 200]);
    $this->set(compact('sale', 'vehicles', 'users'));
}
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 16, 2020, 6:14am UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110/2 "2020-01-16T06:14:34Z")

</div>

Do all of your `start_date` and `end_date` records in the database have values, or are some of them null? If they can be null, then you need to account for that possibility in your templates, e.g. `<?= $sale->start_date ? $sale->start_date->format('d M Y') : 'Unknown' ?>`. If they are not supposed to be null but are being saved that way, then we’ll need to see how you create the form inputs for those fields. There are a couple of potential issues with your `add` function, but it seems to me that they should cancel each other out. I can explain more once you’ve shown your form creation.

---

<div class="post-metadata">

**Author:** ![Samantha](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Samantha](https://discourse.cakephp.org/u/Samantha)\
**Post date:** [January 16, 2020, 7:17am UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110/3 "2020-01-16T07:17:31Z")

</div>

i sent mutiple data including date, but only date save as null in database… already change the format date based on db but nothing works.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 16, 2020, 4:05pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110/4 "2020-01-16T16:05:54Z")

</div>

Well, as a wise man once said, “If they are not supposed to be null but are being saved that way, then we’ll need to see how you create the form inputs for those fields.”

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [January 16, 2020, 10:41pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-cant-save-date-data/7110/5 "2020-01-16T22:41:41Z")

</div>

It sounds like the problem is in the save process.

Possibly you need to look more closely at the data you’re putting in those date fields and whether they are passing validation.

Are you sure your edits are being saved as you seem to assume they are?

Are there any errors on the entity after the save call?

Is it possible something is allowing the entity to save but preventing those fields from being included? I believe the ‘accessible’ property on the entity might do that?

[https://book.cakephp.org/4/en/orm/entities.html#mass-assignment](https://book.cakephp.org/4/en/orm/entities.html#mass-assignment)
