Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

php date/time help


ghost's Avatar
0 0

I want to be able to insert a datetime value to my database table, And then check to see if the current date and time is greater than the time in the database. I've tried a few things now and can't get any of them to work :(

any help?


ghost's Avatar
0 0
<?php
$now = time();
$query = "INSERT INTO `table` (`time`) VALUES ('$now')";
mysql_query($query);
sleep(2);
$later = time();
$query = "SELECT * FROM `table`";
$res = mysql_query($query);
$data = mysql_fetch_array($res);
if( $data['time'] < $later )
{
do_whatever();
}
else
{
do_something_different();
}
?>

PM me if you have any questions


SySTeM's Avatar
-=[TheOutlaw]=-
20 0

TAB AND SPACE YOUR DAMN CODING! :@


ghost's Avatar
0 0

Heh, I never tab my coding. I'll just space it out slightly to make it look nice and neat. :D


ghost's Avatar
0 0

system_meltdown wrote: TAB AND SPACE YOUR DAMN CODING! :@

HAHAHAHA, I knew you'd say that :D

I would have done, but I'm on IRC as well, so I didn't have time to bother with that.


ghost's Avatar
0 0

What's wrong with the way it looks anyway? Seems organized to me..:|


ghost's Avatar
0 0

thanks il check it out now :)

edit how do i pass the "name" variable into the url for get_info.php to work?

<form id="search_form" name="search_form" method="post" action="<?php echo $GLOBALS['file_path'];?>get_info.php?">
	<div align="right">
	<span class="Forms">Name</span>
	<input name="name" type="text" id="name" />
			
              <input name="search_btn" type="submit" class="Default" id="btn_search" value="search" />
</div>
</form>```

get_info.php
```markup
<?php
	$req_name = trim($_GET['name']);
	$req_name = urldecode($req_name);

ghost's Avatar
0 0

@xJorxDyx (Sorry if I did yer nick wrong), for your form action, set it as method="get" not "post"


ghost's Avatar
0 0

thanks that worked :)

@mastergamer:

The code did not work right :angry: when i submitted the query to update the datetime field in the database it did not work. It only works when the field is a "time" field. How to i insert into a "datatime" field?


ghost's Avatar
0 0

xJorDyx wrote: thanks that worked :)

@mastergamer:

The code did not work right :angry: when i submitted the query to update the datetime field in the database it did not work. It only works when the field is a "time" field. How to i insert into a "datatime" field?

I only used those MySQL table and column names as an example, just change them to match what you have. You might want to make a new table to play with this stuff, in case you accidently break something ;)


ghost's Avatar
0 0

no i'm not that stupid lol i have got it all set up for my table but yet it still doesnt work it just inputs the default value for the datetime field which i think was 0000-00-00 00:00:00


ghost's Avatar
0 0

Don't use the DATETIME or TIMESTAMP column types, set them to INT and make the length 20.