Really need some help on that one

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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Really need some help on that one

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

how will i explode that time?

s
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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]));
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Post Reply