Page 1 of 1

Date Time question

Posted: Mon Mar 21, 2011 8:15 pm
by slannesh
Hi all,

I'm new to php and I'm having problems with date and datetime. My code works but the database of the hosting site i use is three hours advanced. I tried to set the time as a variable at -3 hours but that didn't work. Am I going about it the wrong way?

Any help will be appreciated.

Here is a snippet of my code:

Code: Select all

	header("Content-type: image/png");
	$im = imagecreatefrompng("test.png");
        imagecolortransparent ( $im,imagecolorallocate($im, 255, 255, 255));
	$hostname=gethostbyaddr($_SERVER['REMOTE_ADDR']);
	$os=find_os();
	$browser=$_SERVER['HTTP_USER_AGENT'];  //find_browser();
	imagepng($im);
	imagedestroy($im);

include 'config.php';
include 'opendb.php';
$query = 'CREATE TABLE hitlog( '.
		 'cid INT NOT NULL AUTO_INCREMENT, '.
         'hostname TEXT, '.
         'ip TEXT, '.
		 'os TEXT, '.
		 'browser TEXT, '.
		 'city TEXT, '.
		 'referer TEXT, '.
		 'date DATETIME, '.
		 'PRIMARY KEY(cid))';

$result = mysql_query($query);
$query = "INSERT INTO hitlog(hostname, ip, os, browser, city, referer, date) VALUES ('".
	$hostname . "', '".
	$_SERVER['REMOTE_ADDR'] . "', '".
	$os . "', '".
	$browser. "', '".
	whois_info() . "', '".
	$_SERVER['HTTP_REFERER']. "', ".
        " now() )";

$result = mysql_query($query);
include 'closedb.php';

Re: Date Time question

Posted: Mon Mar 21, 2011 8:40 pm
by Jonah Bron
That probably means it's in a different timezone. Let it store it like it wants to, and make up for the timezone difference when you output the time.

Re: Date Time question

Posted: Tue Mar 22, 2011 10:11 am
by akuji36
Hello

Take a look at the following tutorial at:

http://www.youtube.com/watch?v=X8OQMwhdI5M

Re: Date Time question

Posted: Tue Mar 22, 2011 4:29 pm
by slannesh
Thanks for the help. I tried to make the changes by using strtotime and offsetting it by - 3 hours. No error message but nothing is now being added to the database. I'm still looking at how to display the results at - 3 hours.

The changes I made are in Bold.

header("Content-type: image/png");
$im = imagecreatefrompng("test.png");
imagecolortransparent ( $im,imagecolorallocate($im, 255, 255, 255));
$hostname=gethostbyaddr($_SERVER['REMOTE_ADDR']);
$os=find_os();

$offset = strtotime("-3 hours");
$localdate= date("Y-m-d H:i:s", $offset);


$browser=$_SERVER['HTTP_USER_AGENT']; //find_browser();
imagepng($im);
imagedestroy($im);

include 'config.php';
include 'opendb.php';
$query = 'CREATE TABLE hitlog( '.
'cid INT NOT NULL AUTO_INCREMENT, '.
'hostname TEXT, '.
'ip TEXT, '.
'os TEXT, '.
'browser TEXT, '.
'city TEXT, '.
'referer TEXT, '.
'date DATETIME, '.
'PRIMARY KEY(cid))';

$result = mysql_query($query);
$query = "INSERT INTO hitlog(hostname, ip, os, browser, city, referer, date) VALUES ('".
$hostname . "', '".
$_SERVER['REMOTE_ADDR'] . "', '".
$os . "', '".
$browser. "', '".
whois_info() . "', '".
$_SERVER['HTTP_REFERER']. "', ".
" $localdate )";

$result = mysql_query($query);
include 'closedb.php';

Re: Date Time question

Posted: Tue Mar 22, 2011 9:09 pm
by Jonah Bron
Nothing? Does that mean there's no new row, or the date cell is empty?

Re: Date Time question

Posted: Tue Mar 22, 2011 9:16 pm
by slannesh
Sorry forgot to specify that. There is no new row added to the database.

Re: Date Time question

Posted: Wed Mar 23, 2011 3:02 pm
by slannesh
WOOT finally got it. I didn't know that now() was an SQL function ratheren then a function in php. I managed to redo the sql insert with the stringtotime offset and its working.

Thanks for the help everyone.