Cms: Save date as timestamp

Created on 23 Mar 2021  路  17Comments  路  Source: statamic/cms

Bug Description

I have a Date fieldtype in my blueprint. The data looks like this when saving the entry: created_at: '2021-03-24 20:39'. I would like to save the date as a timestamp instead of a string. The same way Statamic does here: updated_at: 1616528480.

Environment

Statamic 3.0.48 Pro
Laravel 8.34.0
PHP 7.4.16

All 17 comments

Add format: U to your date field in the blueprint.

@jasonvarga in the docs that looks like its for how the date appears in the CP, NOT how it's saved?
CleanShot 2021-03-23 at 14 12 14@2x

Yeah the docs are wrong. See https://github.com/statamic/docs/issues/417

Ah, I see. I suppose this in the blueprint builder is wrong too then:

Screenshot 2021-03-24 at 8 27 35 AM

Also, the timestamps get saved as a string rather than an integer:

updated_at: 1616570745
created_at: '1616574300'

@aerni can you update the title to reflect the bug (saved as string)?

It works fine even if it's a string. Does it cause a problem somewhere?

I didn't encounter any problems except my OCD 馃お

@jasonvarga @aerni I've ran into it causing a problem as it happens.

I have a Bard field has a set that allows the selection of an entry from an events collection.

The event blueprint has a date field event_start_date and the is formatted to U.

When I try to select an event from my Bard set field, it hit this

Could not parse '1616716800': DateTime::__construct(): Failed to parse time string (1616716800) at position 8 (0): Unexpected character

and can't load any entries from the events collection as a result. Changing event_start_date: '1616716800' to event_start_date: 1616716800 and order is restored and I can select freely.

for the record, this worked fine in v2

I'm actually running into the same error now too. Not with a bard though. Just a regular field.

This is in my blueprint:

handle: created_at
  field:
    mode: single
    time_enabled: true
    time_required: true
    earliest_date: '2021-01-01'
    full_width: false
    inline: false
    columns: 1
    rows: 1
    display: 'Created at'
    type: date
    icon: date
    listable: true
    read_only: true
    format: U
    width: 50
    instructions: 'The time this order was created'

And this in the entry's data:

created_at: 1617117360

Loading the entry with this data works fine. But when I save the entry, it changes the timestamp to a string. Which then results in the error that @1stevengrant described.

that's like the opposite in this case though right, where strings are fine but the integer isn't ?

Looks like we'll have to make it save as an integer if you choose U as the format.

would it be possible to allow casting the value type?

to what?

@1stevengrant No, same issue. Integer works. String doesn't.

would it be possible to allow casting the value type?

Just wondering if there's instances you'd want it to be a string and other instances as into.

Not in this scenario mind you.

I think we could just make it an integer depending on the format.

If your format would result in a plain number, we'll save an integer.
If it would end up with a dash, space, etc, we'll save a string.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimblue picture jimblue  路  3Comments

sandervanh picture sandervanh  路  4Comments

robdekort picture robdekort  路  3Comments

sauerbraten picture sauerbraten  路  3Comments

ReneWeCode picture ReneWeCode  路  3Comments