views:

241

answers:

1

Ok so I have managed to get the format of the date presented in HTML (upon display) to how I want (dd/mm/yyy)...However, the user also can change the date via a form.

So this is how it's setup as present. Firstly, the conversion from YYYY-MM-DD to DD/MM/YYYY and then display in HTML:

$timestamp = strtotime($duedate); echo date('d/m/Y', $timestamp);

Now when the user selects to update the due date, the value already stored value is drawn up and presented in my text field (using exactly the same code as above).. All good so far.

But when my user runs the update script (clicks submit), the new due date is not being stored, and in my DB its seen as '0000-00-00'. I know I need to convert it back to the correct format that MySQL wants to have it in, in order for it to be saved, but I'm not sure on how to do this. This is what I have so far in my update script (for the due date):

$duedate = $_POST['duedate'];

$timestamp = strtotime($duedate);
    date('Y/m/d', $timestamp);

$query = "UPDATE films SET filmname= '".$filmname."', duedate= '".$duedate;

Can somebody please point me in the right direction to have the new date, when processed, converted back to the accepted format by MySQL. I appreciate any help given! Thanks.

+1  A: 

This should help. Try doing the same conversion, but back to the format MySQL needs. Look at the date() function for formats.

 $duedate = date('Y-m-d', strtotime($_POST['duedate']));
 // Alternate, more accurate version.
 $date_part = explode("/", $_POST['duedate']);
 $duedate = date('Y-m-d', mktime(0, 0, 0, $date_part[1], $date_part[0], $date_part[2]));
St. John Johnson
Thanks St. John, I'll give it a go!
Jess
may I ask wy you have put Y-m'd' ? Is this is typo?
Jess
That definitely is a typo. It should be Y-m-d.
John Conde
@Jess that is definitely a typo. It should be 'Y-m-d'
Steven Oxley
Ok, just to point out, this does 'seem' to work at first, but the actual date displayed after the update, switches the month and day around!!? E.g. the value in the form is set as '02/01/2011'. I did an echo $query; and got: duedate= '1970-02-01'. This obviously changes th value of the date when displayed back in HTML. Is there any reason for this? I have check to make sure all my date formatting is correct?
Jess
strtotime() attempts to figure out what the date is, but it's not always perfect. You could do mktime(). I edited my post to include a version including that.
St. John Johnson
This worked perfectly thanks alot for your help :) ...just out of interest though, I take that the strtotime() couldn't calculate the difference in '-' and '/' between the dates?? I just tried leaving it like your original post solution, and set the displayed dates as d-m-Y, going back to Y-m-d and it seemed to figure it out ok!
Jess
Well, in some countries dates are M/D/Y and others are D/M/Y. So strtotime() has trouble figuring out which is used. Just so you know, after editing, I lose the accepted answer. So if it worked, check me off again.
St. John Johnson
Ok thanks, I have checked you off again! Very greatful :)
Jess