date is always todays!

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
bobo12
Forum Newbie
Posts: 18
Joined: Sun Mar 04, 2007 3:48 pm

date is always todays!

Post 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!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

echo $row['Date'] to start with
bobo12
Forum Newbie
Posts: 18
Joined: Sun Mar 04, 2007 3:48 pm

Post by bobo12 »

It seems like nothing at all is echoed :x
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Does your database not support automatic dating?

Code: Select all

CREATE TABLE `table` (`date` DATE default CURRENT_DATE);
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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?
bobo12
Forum Newbie
Posts: 18
Joined: Sun Mar 04, 2007 3:48 pm

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

$row['Date'] vs $row['DATE'] maybe?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
Last edited by AKA Panama Jack on Sat Jun 02, 2007 2:47 pm, edited 2 times in total.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: date is always todays!

Post 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']));
bobo12
Forum Newbie
Posts: 18
Joined: Sun Mar 04, 2007 3:48 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
Post Reply