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:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Time Ago</title>
<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 action="reguser.php" method="post" name="userreg">
   $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'/>";
  <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>
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 = date($row3['regdate']);
   $result = nicetime($date);
   echo "<tr>";
     echo "<td>". $row3['firstname'] ." was registered  $result. </td>";
   echo "</tr>";
   echo "</table>";

$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";
  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)

header("Location: http://localhost/time");

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

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

$sql="INSERT INTO user (userid,FirstName, MiddleName, LastName, Contact_nos, gender, birthdate, username, password, regdate, status)

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


That's it!

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Echo of Soul: Huntress Quickdraw Build

GeoDjango + GeoServer + Openlayers Combo!