0

I am storing timestamps in my database as such:

04-16-2018 13:15:39

To create additional logic, it would be nice to alter a timestamp so it would have the year in the front of the string, like so:

2018-04-16 13:15:39

Is there a handy way of doing this?

Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • Did you try looking at the manual for `date()` or the DateTime class – RiggsFolly Apr 26 '18 at 10:59
  • What data type are you storing `04-16-2018 13:15:39` in on your database? – RiggsFolly Apr 26 '18 at 11:00
  • it hits my database as `VARCHAR` – Zach Smith Apr 26 '18 at 11:01
  • Well THAT is your first mistake. There is a DateTime datatype specifically designed for holding dates and times – RiggsFolly Apr 26 '18 at 11:02
  • Agreed with @RiggsFolly. – Alive to die - Anant Apr 26 '18 at 11:02
  • i think the point of how i am storing it is throwing you off. i retrieve the value from the database, then in my php code i need to alter it to move the year to the front of the string. sure i can change the datatype in the database, but that will not change how i need to then alter it when i want to use the data in my code to move the year around. for full disclosure i am walking down the path to accomplish this - https://stackoverflow.com/a/12447148/151438 - but the way my timestamp is stored won't work since it requires year in front of string to work with my example link – Zach Smith Apr 26 '18 at 11:05
  • There is a reason why date- / timestamps can be stored as such in your database. It allowes you to work with them as such. Right now, the date is considdered a string for your MySQL server. Not a date. So you don't have the MySQL DATE_FORMAT() function available to you. Which could've been used to solve your issue. – icecub Apr 26 '18 at 11:10
  • ah ok that makes sense. thanks for the clarification – Zach Smith Apr 26 '18 at 11:11
  • @HollerTrain hope this help you :- https://eval.in/995379 – Alive to die - Anant Apr 26 '18 at 11:17
  • if i change the way i am storing these in the db to timestamp, will that solve my issue at hand? could you answer this question as if i am currently storing them as timestamps? – Zach Smith Apr 26 '18 at 11:32
  • If you do that you will get the dates in the format you want automatically as that is how MySQL stores a date and time – RiggsFolly Apr 26 '18 at 11:43
  • You may need to do the date fiddling up front though to get user entered formats into the format you will then need for MySQL to put them into a DATETIME or TIMESTAMP data type – RiggsFolly Apr 26 '18 at 11:44

0 Answers0