Date conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
subfocus
Forum Newbie
Posts: 5
Joined: Fri Jul 28, 2006 6:42 am

Date conversion

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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().
subfocus
Forum Newbie
Posts: 5
Joined: Fri Jul 28, 2006 6:42 am

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
subfocus
Forum Newbie
Posts: 5
Joined: Fri Jul 28, 2006 6:42 am

Post by subfocus »

Seems I have found what I was looking for http://webdesign.ittoolbox.com/code/arc ... 6&a=s&i=10
Post Reply