Date Time question

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
slannesh
Forum Newbie
Posts: 4
Joined: Fri Mar 18, 2011 12:06 pm

Date Time question

Post 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';
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date Time question

Post 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.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Date Time question

Post by akuji36 »

Hello

Take a look at the following tutorial at:

http://www.youtube.com/watch?v=X8OQMwhdI5M
slannesh
Forum Newbie
Posts: 4
Joined: Fri Mar 18, 2011 12:06 pm

Re: Date Time question

Post 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';
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date Time question

Post by Jonah Bron »

Nothing? Does that mean there's no new row, or the date cell is empty?
slannesh
Forum Newbie
Posts: 4
Joined: Fri Mar 18, 2011 12:06 pm

Re: Date Time question

Post by slannesh »

Sorry forgot to specify that. There is no new row added to the database.
slannesh
Forum Newbie
Posts: 4
Joined: Fri Mar 18, 2011 12:06 pm

Re: Date Time question

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