Create A Simple Timestamp in PHP, MySQL

Well, here's the screenshot 'coz I'm too lazy to explain it.
Here's the code for reference:

index.php

<!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>Time Ago</title>
<style>
#stamp{
background-color:#CCCCCC;
}
</style>
</head>
<body>
<p>Create a Database by clicking the button below then sign-up the form.</p>
<form method="post" id="createdb" action="createdbandtables.php">
   <input type="submit" class="submitdb" value="Create DB" /><br/>
</form> 
<form action="reguser.php" method="post" name="userreg">
  <table>
  <tr><td>&nbsp;</td><td>
   <?php
   $con = mysql_connect("localhost","root","");
   if (!$con){
      die('Could not connect: ' . mysql_error());}

   mysql_select_db("dbproj", $con);
   $result = mysql_query("SELECT MAX(userid) as userid FROM user")or die("Query Error:".mysql_error());
   while($row = mysql_fetch_array($result)){
    $id = $row['userid'];
   }
   $newid= $id + 1;
   echo"<input name='uid' type='hidden'  value='$newid'/>";
   mysql_close($con);
   ?> 
  
  </td></tr>
  <tr><td>First Name:</td><td><input type="text" maxlength="20" align="middle" name="fname"/></td></tr>
  <tr><td>Middle Name:</td><td><input type="text" maxlength="20" align="middle" name="mname"/></td></tr>
  <tr><td>Last Name:</td><td><input type="text" maxlength="20" align="middle" name="lname"/></td></tr>
  <tr><td>Contact Nos.:</td><td><input type="text" maxlength="20" align="middle" name="contactnos"/></td></tr>
  <tr><td>Gender:</td><td><select name="gender"><option value="Male">Male</option><option value="Female">Female</option></select></td></tr>
  <tr><td>Birthdate:</td><td><input type="text" maxlength="20" align="middle" name="bdate" placeholder="mm/dd/yyyy"/></td></tr>
  <tr><td>Username:</td><td><input type="text" maxlength="20" align="middle" name="username"/></td></tr>
  <tr><td>Password:</td><td><input type="password" maxlength="20" align="middle" name="password"/></td></tr>
  <tr><td>&nbsp;</td><td><input type="hidden" maxlength="20" align="middle" name="status" value="UC"/></td></tr>
  <tr><td><input type="reset"/></td><td><input type="submit" value="Sign Up" onclick="return confirm('Are you sure the details are correct?');"></td></tr>
  </table>
 </form>
<?php
error_reporting(0);
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";
}

$con = mysql_connect("localhost","root","");
   if (!$con){
      die('Could not connect: ' . mysql_error());
    }
   mysql_select_db("dbtimeago", $con);

$results3 = mysql_query("SELECT firstname,userid,regdate FROM user where status='UC'");
   
   //functtion time
   
   echo "<table id='stamp'>";
   while($row3 = mysql_fetch_array($results3)){
   $userid =  $row3['userid'];
   date_default_timezone_set('Asia/Kuala_Lumpur');
   $date = date($row3['regdate']);
   $result = nicetime($date);
   echo "<tr>";
     echo "<td>". $row3['firstname'] ." was registered  $result. </td>";
   echo "</tr>";
   }
   echo "</table>";
   
?>
</body>
</html>


createdbandtable.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE dbtimeago",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("dbtimeago", $con);
$sqluser = "CREATE TABLE user
(
userid int,
firstName varchar(25),
middleName varchar(25),
lastName varchar(25),
contact_Nos int,
gender varchar (15),
birthdate varchar (25), 
username varchar (20),
password varchar (20),
regdate varchar (25),
status varchar (20)
)";
mysql_query($sqluser,$con);


header("Location: http://localhost/time");
mysql_close($con);
?> 



reguser.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtimeago", $con);
date_default_timezone_set('Asia/Kuala_Lumpur');
$datenow= date("Y/m/d H:i:s");

$sql="INSERT INTO user (userid,FirstName, MiddleName, LastName, Contact_nos, gender, birthdate, username, password, regdate, status)
VALUES
('$_POST[uid]','$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[contactnos]','$_POST[gender]','$_POST[bdate]','$_POST[username]','$_POST[password]','$datenow','$_POST[status]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Succesfully Added!";
echo "<p><a href='http://localhost/time'>Back</a>";


mysql_close($con)
?>

That's it!

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Add/Subtract Single Digit Number in Assembly Language