Hi,

I have converting date from d-m-Y format to Y-m-d format using date function but the date which is getting inserted is a junk value.

My code is as follows ::
The value which is inserted in FORM fields DOE & LDT is in d-M-Y format like 06-06-2010

Code ::

$DOE=$REQUEST['DOE'];
$DOE=date('Y-m-d', strtotime($DOE));
$LDT=$
REQUEST['LDT'];
$LDT=date('Y-m-d',strtotime($LDT));
$postid=$_REQUEST['id'];

UPDATE $tablename SET DOE='".$DOE."',LDT='".$LDT."' WHERE id = '".$postid."'";

The update is happening but the date which is update for values 06-06-2010 is 01-12-2011 ??? Y????

    Have you tried echoing $DOE or $LDT in your code? If the values you put in those vars are not what you want, you should probably reconsider how you are capturing data in your form. [man]strtotime[/man] may not be interpreting your form input like you want it to.

      dd-mm-yyyy isn't a very good format to use to begin with; for most purposes it's way too ambiguous (it's too easily confused with mm-dd-yyyy; on top of that, strtotime would interpret "06-06-10" as referring to June 10, 2006, so if the century is left out that's a third interpretation).

      As sneakyimp suggests, check the values of $DOE and $LDT; both before and after that bit of code, and also the SQL statement that's sent to the database, because date('Y-m-d', ...) would not produce "01-12-2011" .

        This is why it's a lot easier to use dropdown <SELECT> elements when users are to input dates, rather than just giving them a text box and hoping that they put the date in a sensible format (as Weedpacket suggests, dd-mm-yy isn't one of those 'sensible' formats to begin with).

        Otherwise, if you know what format the date is going to be in, it might be easier to use [man]sscanf/man and [man]mktime/man rather than hoping [man]strtotime/man picks the right format.

          Thanks all of you.

          I have used a trick. I am splitting the date into array and then showing it in the form.

          and while updating it again concatinating. Don't know whether its the best way but its working.

            It's at least a little better than giving a vanilla "date" textbox. The less control you give to the user as far as the format goes, however, the better off you'll be - that's why I prefer to use <select> dropdown boxes.

              Write a Reply...