admin管理员组

文章数量:1122832

Is it possible to save a column as ISO8601 format that is not a string type?

Basically, I want to save the date and time as shown below:

2024-11-21T19:30:00.

I tried to use timestamp, so

$table->timestamp('start');

but it saves the data as 2024-11-21 19:30:00, so not as I want.

Is there a way to save it directly or do I have to use string, so $table->string('start') ?

Is it possible to save a column as ISO8601 format that is not a string type?

Basically, I want to save the date and time as shown below:

2024-11-21T19:30:00.

I tried to use timestamp, so

$table->timestamp('start');

but it saves the data as 2024-11-21 19:30:00, so not as I want.

Is there a way to save it directly or do I have to use string, so $table->string('start') ?

Share Improve this question asked Nov 21, 2024 at 17:16 ghost_of_the_codeghost_of_the_code 3351 silver badge8 bronze badges 3
  • 3 I'd recommend leaving them as timestamps, as it's easier to search and compare. You can format them with Eloquent's date casting – aynber Commented Nov 21, 2024 at 18:32
  • So to have the format YYYY-MM-DDTHH:MM:SS what do you recommend I use? – ghost_of_the_code Commented Nov 21, 2024 at 18:38
  • 2 The value you're saving to the database should be a timestamp, like you have it now. If you wanted to store the ISO8601 value alongside it as a string, you could do that, but not sure what benefit you'd get out of that. When you actually use this value in your Laravel app, with a bit of configuration, you can simply do $model->start->toIso8601String() to display that value while keeping the correct value in the database. – Tim Lewis Commented Nov 21, 2024 at 18:51
Add a comment  | 

1 Answer 1

Reset to default 0

One way to go about it is to treat it as a string and call the Carbon object to parse it to ISO8601 format when you need it.

本文标签: