Jump to content

MySQL field type datetime stores value as 0000-00-00 00:00:00 for some reason


kee2ka4

Recommended Posts

Hi everyone,

 

I have a field called created_at of type datetime in mysql, and I am passing in a variable $time that holds the time in format 2008-07-09 18:14:54 but for some strange reason when I insert the $time value into create_at field it saves it as 0000-00-00 00:00:00

 

Here is my php code:

date_default_timezone_set('Asia/Calcutta');
function uk_time()
{
$time = time();

return date('Y-m-d G:i:s', $time);
}  

$time = uk_time();
echo $time;

$post_validations = array('body'  => '/^[[:alnum:][:punct:][:space:]]{1,2000}$/');

/**
 * creates a post
 * @param array $params
 * @return bool
 */
function create_post($params)
{
  $connection = db_connect();

  $query = sprintf("insert into posts 
                       set 
										 body = '%s',
										 user_id = '%s',
										 created_at ='%s'
                     ", 
										 safe_output(mysql_real_escape_string($params['body'])),
										 safe_output(mysql_real_escape_string($params['user_id'])),
										 $time
										 );

	$result = mysql_query($query);

 

I am using the uk_time() function and saving the value to variable $time which is then saved to created_at field in the database. The reason I use uk_time function is because I need the indian timezone and my hosting company has a different timezone settings, hence I can't use the mysql now() function.

 

Can someone please tell me why the correct value is not sored in the database.

 

Thanks,

Ket

Link to comment
Share on other sites

the function doesn't have access to that variable...replace $time with your function call:

 

date_default_timezone_set('Asia/Calcutta');
function uk_time()
{
$time = time();

return date('Y-m-d G:i:s', $time);
}  

$time = uk_time();
echo $time;

$post_validations = array('body'  => '/^[[:alnum:][:punct:][:space:]]{1,2000}$/');

/**
 * creates a post
 * @param array $params
 * @return bool
 */
function create_post($params)
{
  $connection = db_connect();

  $query = sprintf("insert into posts 
                       set 
										 body = '%s',
										 user_id = '%s',
										 created_at ='%s'
                     ", 
										 safe_output(mysql_real_escape_string($params['body'])),
										 safe_output(mysql_real_escape_string($params['user_id'])),
										 uk_time() //I changed this here
										 );

	$result = mysql_query($query);

Link to comment
Share on other sites

Hi everyone,

 

I have a field called created_at of type datetime in mysql, and I am passing in a variable $time that holds the time in format 2008-07-09 18:14:54 but for some strange reason when I insert the $time value into create_at field it saves it as 0000-00-00 00:00:00

 

Here is my php code:

date_default_timezone_set('Asia/Calcutta');
function uk_time()
{
$time = time();

return date('Y-m-d G:i:s', $time);
}  

$time = uk_time();
echo $time;

$post_validations = array('body'  => '/^[[:alnum:][:punct:][:space:]]{1,2000}$/');

/**
 * creates a post
 * @param array $params
 * @return bool
 */
function create_post($params)
{
  $connection = db_connect();

  $query = sprintf("insert into posts 
                       set 
										 body = '%s',
										 user_id = '%s',
										 created_at ='%s'
                     ", 
										 safe_output(mysql_real_escape_string($params['body'])),
										 safe_output(mysql_real_escape_string($params['user_id'])),
										 $time
										 );

	$result = mysql_query($query);

 

I am using the uk_time() function and saving the value to variable $time which is then saved to created_at field in the database. The reason I use uk_time function is because I need the indian timezone and my hosting company has a different timezone settings, hence I can't use the mysql now() function.

 

Can someone please tell me why the correct value is not sored in the database.

 

Thanks,

Ket

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.