Page 1 of 1

Really need some help on that one

Posted: Wed Dec 22, 2004 4:30 am
by snicolas
Sorry to bother you with this again, but I am blocked for the last 2 days.
PHP and SQL.

I display my date from SQL in a form for user to update like that

<input type="text" name="date" size="20" value="<?php echo date("d/m/Y",strtotime($date));?>">

When user post this value I get it back that way
$date = $_POST["date"];
$date = date('Y-m-d h:i:s',strtotime($date));

now if in my form i enter a date like 15/12/1999, this updated properly
BUT
if i enter a date liuke 15/12/2004, the DB is update with the value
12/03/2005 12:00:00

something i am missing there???

thanks in advance
s

Posted: Wed Dec 22, 2004 4:40 am
by onion2k
strtotime() is assuming the date is in mm/dd/yyyy format.

Err..

Though that doesn't explain why it works with the 1999 date obviously. This is why I never use strtotime. Explode() it, feed the bits into mktime() instead.

Posted: Wed Dec 22, 2004 4:49 am
by markl999
Yeah, the 15/12/1999 can't be working ok as date(.., strtotime()) on that will return 2000-03-12 12:00:00

Posted: Wed Dec 22, 2004 4:54 am
by snicolas
how will i explode that time?

s

Posted: Wed Dec 22, 2004 5:13 am
by markl999
You could do something like:

Code: Select all

$dparts = explode('/', $_POST['date']);
$date = date('Y-m-d h:i:s', strtotime($dparts[1].'/'.$dparts[0].'/'.$dparts[2]));

Posted: Wed Dec 22, 2004 5:23 am
by snicolas
That worked perfectly mark!
Thanks for your input.

I repeat the code below for people that needs it:

Display date in a input ready for update:
<input type="text" name="date" size="20" value="<?php echo date("d/m/Y",strtotime($date));?>">

User submit the form with whatever value and I get the info like that
$date = $_POST["date"];
$dparts = explode('/', $date);
$date = date('Y-m-d h:i:s', strtotime($dparts[1].'/'.$dparts[0].'/'.$dparts[2]));

date is displayed properly...

Last little question...
Any good function to check that the date is in correct format?

thanks again

s

Posted: Wed Dec 22, 2004 5:25 am
by markl999