# How to patch Entity from Strava date time format

**URL:** <https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938>\
**Category:** Need Help\
**Created:** [March 14, 2024, 4:29pm UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938 "2024-03-14T16:29:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![general2000vn](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@general2000vn](https://discourse.cakephp.org/u/general2000vn)\
**Post date:** [March 14, 2024, 4:29pm UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/1 "2024-03-14T16:29:43Z")

</div>

I am writing an app which get data from Strava API.  
When create new Entity, it does not allow getting date time from returned text.  
My field is Date Time type, and the returned value from API is:

```auto
"start_date": "2023-04-15T10:49:06Z",
"start_date_local": "2023-04-15T17:49:06Z",

```

I really dont know that type of that string is.

Can anyone help me patch that value into my DateTime field in Entity?

Thank you so much.

---

<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:** [March 14, 2024, 6:13pm UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/2 "2024-03-14T18:13:05Z")

</div>

That’s the ISO standard format for dates. I’d think that it would be handled directly by the patch process, if it knows that `start_date` is a date time field.

As for `start_date_local`, that seems less useful. You should be storing all dates in UTC in your database, and generate local time for the current user on demand based on some profile preference.

---

<div class="post-metadata">

**Author:** ![general2000vn](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@general2000vn](https://discourse.cakephp.org/u/general2000vn)\
**Post date:** [March 15, 2024, 2:03am UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/3 "2024-03-15T02:03:58Z")

</div>

Thank you Zuluru,

However, the Entity does not know how to patch that fields.  
In database, the field is **datetime** , in CakePHP, the table defines the filed as datetime too, with validator as below:

```auto
$validator
            ->dateTime('start_date')
            ->notEmptyDateTime('start_date');

```

However, when patching, there is error, and the error message is as below:

```auto
'dateTime' => 'The provided value must be a date and time of one of these formats: `ymd`',

```

I also made a test newEntity with different datetime string format as below, it works

```auto
                $testData = array();
                $testData['name'] = 'test act';
                $testData['user_id'] = '3';
                $testData['elapsed_time'] = 600;
                $testData['start_date'] = '2023-04-15 10:49:06';
                $testData['start_date_local'] = '2023-04-15 17:49:06';

                $testAct = $activityTable->newEntity($testData); 

```

I think that the little different in datetime string format causing this.

```auto
'2023-04-15 10:49:06'

```

vs

```auto
"2023-04-15T10:49:06Z"

```

I am using CakePHP 5.0.5, if that is relevant.

---

<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:** [March 15, 2024, 3:14am UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/4 "2024-03-15T03:14:30Z")

</div>

Is [this](https://book.cakephp.org/5/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data) of any help?

---

<div class="post-metadata">

**Author:** ![general2000vn](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@general2000vn](https://discourse.cakephp.org/u/general2000vn)\
**Post date:** [March 15, 2024, 5:09am UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/5 "2024-03-15T05:09:46Z")

</div>

Thanks Zuluru but it does not work.

I have to manipulate data manually to get rid of the “T” and “Z” in the datetime string before patching entity.

---

<div class="post-metadata">

**Author:** ![jmcd73](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jmcd73/32/481_2.png) [@jmcd73](https://discourse.cakephp.org/u/jmcd73)\
**Post date:** [March 17, 2024, 11:32pm UTC](https://discourse.cakephp.org/t/how-to-patch-entity-from-strava-date-time-format/11938/6 "2024-03-17T23:32:50Z")

</div>

When ingesting those dates can you use DateTimeImmutable or Chronos?

```auto
$table = $this->fetchTable('Strava');

        // Chronos::ATOM = 'Y-m-d\\TH:i:sP';

        $strava = $table->newEntity([
            'date_1' => Chronos::createFromFormat(
                format: Chronos::ATOM,
                time: "2023-04-15T10:49:06Z"
            ),
            'date_2' => Chronos::createFromFormat(
                Chronos::ATOM,
                "2023-04-15T17:49:06Z"
            ),
        ]);

        dd($strava);

```

Output

```auto
########## DEBUG ##########
object(App\Model\Entity\Strava) id:0 {
  'date_1' => object(Cake\Chronos\Chronos) id:1 {
    'hasFixedNow' => false
    'time' => '2023-04-15 10:49:06.000000'
    'timezone' => 'UTC'
  }
  'date_2' => object(Cake\Chronos\Chronos) id:2 {
    'hasFixedNow' => false
    'time' => '2023-04-15 17:49:06.000000'
    'timezone' => 'UTC'
  }
  '[new]' => true
  '[accessible]' => [
    'date_1' => true,
    'date_2' => true
  ]
  '[dirty]' => [
    'date_1' => true,
    'date_2' => true
  ]
  '[original]' => []
  '[originalFields]' => [
    (int) 0 => 'date_1',
    (int) 1 => 'date_2'
  ]
  '[virtual]' => []
  '[hasErrors]' => false
  '[errors]' => []
  '[invalid]' => []
  '[repository]' => 'Strava'
}
###########################

```
