1

I usually keep the date as timestamp in sql.

But now Im doing a project that I want that the date appears like "Wednesday, 30 April, 2014".

And Im using datepicker to format the date like I want and its working fine.

$('.datepicker').datepicker({
    dateFormat: 'DD, d MM, yy',         
});

But now I am in doubt, if it is appropriate to save date as varchar, but also dont seems possible to save "Wednesday, 30 April, 2014" as timestamp or datetime or someting else date option.

What do you think its better?

OzzC
  • 815
  • 4
  • 20
  • 36

1 Answers1

1

Use DateTime or TimeStamp but do not use a VarChar.

What do you think its better?

This type of question are problematic on StackOverflow, but it has been asked before.

If you ever store it as a VARCHAR it will make your life very hard. Because doing calculation on dates will just be a real hassle.

Community
  • 1
  • 1
meda
  • 45,103
  • 14
  • 92
  • 122
  • I thought it may be better, because there isn´t a type in phpmyadmin to this date format: "Wednesday, April 30, 2014,". And as I'll want to show the date like "Wednesday, April 30,2014", I thought it was better to save now as varchar instead of writing as datetime and then do the conversion. – OzzC Apr 30 '14 at 01:00
  • 1
    @OzzC no, saving as `VARCHAR` will be the worst idea. If you ever have to retrieve that data you will need to worry about parsing it back to a date, format and all the pain.If you instead choose a date field it will be much more powerful as you will be able to directly do calculation in the query or perhaps Order your records by date. – meda Apr 30 '14 at 01:03
  • 1
    @OzzC just because you want to display it as X does not mean you have to store it as X. you may display on\off or true\false but would store the integer 1\0. its the same with dates. its trivial to convert with either php or mysql's own DATE_FORMAT() –  Apr 30 '14 at 01:06
  • Ok, thank you for your explanation! But do you know how I can do this? Im trying with strtotime but its not working. My date is "Wednesday, April, 2014", Im trying to conver to datetime like this: echo date('Y-m-d',strtotime($f['date'])); My result is this:"1970-01-01". – OzzC Apr 30 '14 at 01:09
  • And I will need to do some calculation with date, so Im seeing that varchar is really to forget! – OzzC Apr 30 '14 at 01:23