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

andreasb's avatar

Amazon thinks "12/2015" is a good way to return a date field. How to save to DB now?

Good evening,

my beloved Amazon (I really like them!! :-)) provide monthly reports. And for each month, the provide a date if you list - say - the last 12 months. And they seriously provide the month in "mm/yyyy" format, e.g. "12/2015".

Obviously, I dont want to store this as a string in the DB but as a date field.

Now I was thinking about the best ways how to tackle this.

  • 1)The obvious solution: A model mutator. Well, long story short - I am insertin the data via
DB::table($tableName)->insert($requestStatus->parsedResponse);

because I am talking potentially thousands of rows and also I am using composite keys which Laravel unfortunately does not support...

  • 2)Store as string (arg) and use a mutator when RETRIEVING the model. Not nice but that could work
  • 3)Manipulate the value before it is inserted into the DB. I dont like this one because I have several reports which I am processing with a handful of fully report-agnostic methods and I wouldnt like to start changing this.
  • 4)...any other options? Like changing the date format of MySQL? :-)

Your insights are - as always - very welcome :-)

Thanks Andreas

0 likes
3 replies
Demers94's avatar

It seems like you're looking at complicated solutions to a very simple problem : why don't you just format the date as you need in your PHP script, and store it as a proper DATETIME or DATE in the database?

Assuming that it's always MM/YYYY, you can juste split it up to get the individual components, and order/format them any way you want.

You can also use the Carbon library which is already used in Laravel.

andreasb's avatar

Hi you two

thanks for the reply. But as I said, I would prefer a solution where I do not have to tinker with my report agnostic methods.

Does anyone has another idea?

Is there no way to tell MySQL to treat this as a date input?

Andreas

Please or to participate in this conversation.