Jump to content

I am confused with the php time() and date() function and mysql now()? Need some


kee2ka4

Recommended Posts

Hi,

 

I have a site that stores user comments and the date/time of when the comments was placed. At the moment I insert comments and store date/time using the Now() as shown below:

sprintf("INSERT INTO comments 
                  SET
		  body = '%s',
		  created_at = now(),
		  user_id = '%s'
                  ", 
		  safe_output(mysql_real_escape_string($params['body'])),
		  safe_output(mysql_real_escape_string($params['user_id'])));

The problem with the above is that the now() stores the date/time according to the hosting server location. I want the site to display and store date/time set to the timezone in London. Hence I have used this php function: date_default_timezone_set('Europe/London'); and used the time() in the insert query rather then the mysql now(), as shown below:

sprintf("INSERT INTO comments 
             SET 
	     body = '%s',
	     created_at =" .time(). ",
	     user_id = '%s'
             ", 
	     safe_output(mysql_real_escape_string($params['body'])),
	     safe_output(mysql_real_escape_string($params['user_id'])));

However, I get 00 for the date value and 00:00 for time value. I think I get these null values because I have the created_at field as type "datetime" in mysql and also may be because time() function is a timestamp? Is that correct!

 

If I want to use the time() and date() php functions do I have to change the create_at field as type "string" rather then "datetime"? and if I save the timestamp using time() as a string, can I then display the date the comment was left using the date() and passing the timestamp as a parameter?

I am new to php and need some guidance in correct way to using the php functions time() and date() rather then using the mysql now(). Is there any guide's or tutorial that can make things clear for me.

 

Thanks,

Ket

Link to comment
Share on other sites

You can create a timestamp field in the database, and when a new record is inserted, it will insert the current time that corresponding record was added. All done automatically. It's in the form of a unix timestamp though, I.e. 01-01-2008 12:45

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.