Page 1 of 1

Date conversion

Posted: Sat Aug 05, 2006 2:21 pm
by subfocus
Hey,

I'm not sure what I'm doing wrong with this, so I thought I'd post it up to see if you guys could try and give me a helping hand!

I have a date field (YYYY-MM-DD) in my database, the contents of that field in the record are put in to an editable text box, I used the following code to convert it to DD-MM-YYYY

Code: Select all

$cdate = date('d-m-Y',strtotime($cdate));
This appears to 'pull' the date out of the database and convert it to the required format fine.

The problem I have arrises when I submit the form, and I presume I have to convert the date back to the YYYY-MM-DD format for the field. Anyway when I check that the date field has been updated successfully, it just has random dates appear in the fields.

Prior to the date being updated by the MySQL query, I run the following code on the variable.

Code: Select all

$cdate = date('Y-m-d',strtotime($cdate));
If I submit 01-01-2000, the field has 23-06-2006 entered!

Anyway, I'm hoping my post made some sense, if anyone has any ideas that would be great.

Thanks alot.

Posted: Sat Aug 05, 2006 2:51 pm
by Ollie Saunders
When you insert date fields into any database they have to be as per ISO standard date format which is YYYY-MM-DD.
So yeah I'm not surprised you are getting random stuff. Make a couple of converter functions dateToBritishFromIso() and dateToIsoFromBritish().

Posted: Sat Aug 05, 2006 2:56 pm
by subfocus
Wouldn't the line of code I run before the query change it to an acceptible format? as 01-02-2000 becomes 2000-02-01 for example.

Do any kind of slashes, or dashes have any negative effects on what is entered in to the database?

Posted: Sat Aug 05, 2006 3:09 pm
by Ollie Saunders
Wouldn't the line of code I run before the query change it to an acceptible format?
Oh right sorry, yeah didn't see that.
Well echo the output from it, make sure its doing its job.

Posted: Sat Aug 05, 2006 3:29 pm
by subfocus
Seems I have found what I was looking for http://webdesign.ittoolbox.com/code/arc ... 6&a=s&i=10