Jump to content

[Solved] Date and Time


Trium918

Recommended Posts

Ok, I finally got the the user's register date into the datebase, but now I am trying to
write a program that will calculate the user's last login by updating reg_date columm in
the user table.

This is just a simple html code that just enter password and username

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basic Authentication Script</title>
</head>

<body bgcolor="#FFCOCB" text="#000000">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<form name="form1" method="post" action="last_visit.php">
<table width="500" border="5" align="center" cellspacing="2" cellpadding="10">
<tr bgcolor="#EEEEEE">
<td height="1">Password
<input type="password" name="password"/><br />
Username<input type="text" name="user"/>
<input type="submit" name="Submit" value="Login" />
</td>
</tr>
<tr>
<td bgcolor="#DDDDDD">
<div align="center"><b>Enter the password to see the next page.</b></div>
</td>
</tr>
</table>

</body>
</html>
[/code]

This is the php code that checks enter the usename and time of registeration into the database

[code=php:0]
<? 
$host="localhost";
$database="Date";
$dusername="root";
$x=mysql_connect($host,$database,$dusername);
$x1=mysql_select_db($database)
or die("Could not select Date database !");

$time = time();

$query = "insert into user_data
(reg_date,user)
values
($time, '$user')";


$result=mysql_query($query);
if (!($result)) { die("User Register date could not be stored"); }

if (empty($password))
{
die("You did not specify the password!");
}
if (!($password == "pass"))
{
die("You did not specify the valid password.");
}

?>
[/code]
Link to comment
Share on other sites

PHP Also has a time function called Date(); You can use this with a number of parameters.

An example would be, if you want to get the Day, Month and Year. You would use Date("d/m/Y");

You can check this function up on php.net or this link : http://www.w3schools.com/php/php_date.asp

Cheers, Daniel
Link to comment
Share on other sites

For starters, Date is a reserved word in mysql, so shouldn't be used as a database or field name.

[code]
<?php
  $host="localhost";
  $database = "DateDB"; // Ive changed your db name.
  $dusername = "root" ;
  $dbpassword = "";
  mysql_connect($host,$database,$dusername,$dpassword) ;
  mysql_select_db($database) or die( "Could not select DateDB database !" ) ;
  $query = "INSERT INTO DateDB useradded VALUES ('NOW()')"; // Ive changed your field name.
  if (mysql_query($query))
    echo "success";
  } else {
    echo mysql_error();
  }
?>
[/code]

PS: The board has its own syntax highlighting, just wrap all your code in either [ php ][ /php ] or [ code ][ /code ] tags without the spaces.
Link to comment
Share on other sites

You should keep in mind that the server is not always in the same time zone as you and sometimes the mysql server is not the same machine as your apache server.  For instance on the old web host we used at work, if I used date() from within PHP I got a time on the east coast of the United States.  If I used NOW() in MySQL I got a time a few hours ahead of that.

What I recommend is using a statement like:
[code]SELECT DATE_FORMAT(NOW(), 'H:i:s') FROM table WHERE 1 LIMIT 1[/code]

That will give you the time of the MySQL server.  Let's say that time is 3 hours ahead of where you are, then you can add a line:
[code=php:0]define('DB_NOW', '(NOW() - 3 * 60 * 60)');[/code]
to your general DB.php file.

Now whenever you need to insert the current DATETIME into a table you can just do:
[code=php:0]$sql = "INSERT INTO table (fld1, fld2, the_date) VALUES (val1, 'val2', '" . DB_NOW . "')";[/code]

If you ever move servers, you only need to adjust a single line of code to compensate for the new server's possible timezone difference than the old one.
Link to comment
Share on other sites

Just use the date() function its easier.

You just have to use this code...

[CODE]

<?php

// Using some of thorpes code...

  $host="localhost";
  $database = "DateDB"; // Ive changed your db name.
  $dusername = "root" ;
  $dbpassword = "";

  mysql_connect($host,$database,$dusername,$dpassword) ;

  mysql_select_db($database) or die( "Could not select DateDB database !" ) ;

  $date = Date("d/m/Y");

  $query = "INSERT INTO DateDB useradded VALUES ('$date')"; // Ive changed your field name.

  if (mysql_query($query))

    echo "success";

  } else {

    echo mysql_error();

  }

?>

[/CODE]

Use that code.

Thanks, Daniel
Link to comment
Share on other sites

Ok, this code will only allow me to enter the user register date once.
Afterward, the code [code=php:0]die("User Register date could not be stored"); [/code]
is displayed in the browser. Could someone please tell me what am I doing wrong
,and how to fix it?

[code=php:0]$host="localhost";
$database="Date"; //The name of the database
$dusername="root";
//$dpassword="";,$dpassword
$x=mysql_connect($host,$database,$dusername);
$x1=mysql_select_db($database)
or die("Could not select Date database !");
$date = date('n/j/Y');
$query="INSERT into user_data $reg_date Values('$date')";
$result=mysql_query($query);
if (!($result)) { die("User Register date could not be stored"); }
[/code]
Link to comment
Share on other sites

This is how I do it, and I've never had a problem on my site

[code]
$ip = $REMOTE_ADDR;
$query="update users set last_login=NOW(),last_ip='$ip' WHERE username='$username'";

[/code]

Puts the last time the user logs in and what ip they logged in from
Link to comment
Share on other sites

I tried to reply to your personal mesage, but it says I am not allowed to send personal messages, go figure.

Anyhow, to answer your question, to insert the date during registration, it would be the same as updating the Last Login:

I'm just running a simple Photo hosting site, so my database is small, as well as my registration.
Here is the sql for what my site uses:
[code]
CREATE TABLE `users` (
  `user_id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL default '',
  `password` varchar(255) NOT NULL default '',
  `email` varchar(255) NOT NULL default '',
  `signup_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `ip` varchar(255) NOT NULL default '',
  `last_login` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `last_ip` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
[/code]

And then my script to insert the data

[code]
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry there the username $username is already taken.<br>";
echo "<a href=register1.php>Try again</a>";
exit;
}
//check if email was used
// check if the username is taken
$check = "select id from $table where email = '".$_POST['email']."';";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, that email has already been used ($email).<br>";
echo "<a href=register1.php>Try again</a>";
exit;
}else {
// insert the data
$ip = $REMOTE_ADDR;
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."',NOW(),'$ip',NOW(),'".$_POST['username']."')")
or die("Could not insert data because ".mysql_error());

//make directory
mkdir("/your/direct path/$_POST[username]");
$result = mkdir($_POST[username],0777);
  if(!$result) {
      echo "Error: Couldn't create the directory for $_POST[username]!<BR>";

  }
  {
copy ("/your/direct path/index.php", "/your/direct path/$_POST[username]/index.php")
        or die("Could not copy index file");
}

//mail the user
    $subject = "Account Confirmation!";
    $message = "
Hi $_POST[username] -- Thanks for joining!

Here's your account info for logging in:

E-mail: $_POST[username].
Password: $_POST[password].

Keep it secret. Keep it safe.
===================================

NOTE: This email is never sent unsolicited. If you believe you received this notification in error, please send an email to


-------------------------

<!-- __$email__ -->
   
    This is an automated response, please do not reply!";
   
    mail($_POST[email], $subject, $message,
        "From: Welcome to My Website<register@website.com>\n");
// print a success message
echo "Your user account has been created! for $username<br>";
echo "Now you can login to your new address: <a href=http://www.website.com/$_POST[username]>http://www.website.com/$_POST[username]</a>";
}
[/code]
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.