Page 1 of 1

date is always todays!

Posted: Sat Jun 02, 2007 11:52 am
by bobo12
Even though a comment might have been added 2 months ago, my php code is output todays date!

Here's the code to insert:

Code: Select all

$curr_day = date('Y-m-d');
			$query = "INSERT INTO Messages (Name, Email, Comment, DATE)
			  	VALUES('$username','$usermail', '$comment', '$curr_day')";
Here's the output:

Code: Select all

date('D M Y', mktime($row['Date']));
I'm probably doing something completely stupid, but I have no idea what that is!

Posted: Sat Jun 02, 2007 12:13 pm
by Weirdan
echo $row['Date'] to start with

Posted: Sat Jun 02, 2007 12:15 pm
by bobo12
It seems like nothing at all is echoed :x

Posted: Sat Jun 02, 2007 12:19 pm
by superdezign
Does your database not support automatic dating?

Code: Select all

CREATE TABLE `table` (`date` DATE default CURRENT_DATE);

Posted: Sat Jun 02, 2007 1:23 pm
by pedrotuga
bobo12 wrote:It seems like nothing at all is echoed :x
ok... so the error is not in the code you just paste.
mktime is geting an empty parameter, that's why it returns the current time.

You are making some mistake somewhere back in your code when puting the value into $row['Date']

why don't you past the whole code?

Posted: Sat Jun 02, 2007 2:16 pm
by bobo12
What's weird is it shows up as 2007-05-22 00:00:00 in the databases and the code I gave is the whole thing with the exception of mysql_query($query,$connection) (which executes successfully). The rest of the stuff inserted is just post variables.

Posted: Sat Jun 02, 2007 2:19 pm
by Weirdan
$row['Date'] vs $row['DATE'] maybe?

Posted: Sat Jun 02, 2007 2:38 pm
by AKA Panama Jack
superdezign wrote:Does your database not support automatic dating?

Code: Select all

CREATE TABLE `table` (`date` DATE default CURRENT_DATE);
You should avoid doing this unless the database server is on the SAME server as PHP.

The reason is because I have found many hosting companies do not sync the dates between all of their servers properly. I have seen the database server be as much as a few days off compared to the PHP server. So you should always create the date using PHP and store it in the database. It will avoid a host of problems.

Re: date is always todays!

Posted: Sat Jun 02, 2007 2:41 pm
by AKA Panama Jack
bobo12 wrote: Here's the output:

Code: Select all

date('D M Y', mktime($row['Date']));
I'm probably doing something completely stupid, but I have no idea what that is!
Use this instead...

Code: Select all

date('D M Y', strtotime($row['Date']));

Posted: Sat Jun 02, 2007 3:15 pm
by bobo12
Weirdan wrote:$row['Date'] vs $row['DATE'] maybe?
:oops: Bingo. I've had this problem for a long time and looked right over this.
date('D M Y', strtotime($row['Date']));
Thanks, panama jack. I'm switching to this!


Thanks guys! This is resolved.

Posted: Sat Jun 02, 2007 3:41 pm
by superdezign
AKA Panama Jack wrote:I have seen the database server be as much as a few days off compared to the PHP server. So you should always create the date using PHP and store it in the database. It will avoid a host of problems.
Oh. Well, back to coding. :-p

Would you suggest I still use the datetime type, or just a varchar/text?

Posted: Sat Jun 02, 2007 4:26 pm
by AKA Panama Jack
Sure, it's best to use the datetime datatype because it will allow the database to perform date and time searches faster and easier. Just create the date and time using PHP based upon the clock PHP is using and store that information in the datetime field.