How to calculate AGE from your birth-date in PHP
Here I will Provide Code for How to calculate AGE from your birth-date
all data will be dynamically this is no static script
PAGE LIST:
1.Register Page2.View Page3.Database query
features:
1.Here i provide advance register Page Code with implode explode function2.count age from birthdate is fully dynamically without any function
CODE:
-----------------------------------------
Create database db_harsh
---------------------------------------
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 25, 2016 at 07:22 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `db_harsh`
--
-- --------------------------------------------------------
--
-- Table structure for table `country`
--
CREATE TABLE IF NOT EXISTS `country` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(20) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `country`
--
INSERT INTO `country` (`cid`, `cname`) VALUES
(1, 'india'),
(2, 'australia');
-- --------------------------------------------------------
--
-- Table structure for table `reg`
--
CREATE TABLE IF NOT EXISTS `reg` (
`rid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(20) NOT NULL,
`pass` varchar(20) NOT NULL,
`gender` varchar(10) NOT NULL,
`hobby` varchar(25) NOT NULL,
`cid` int(11) NOT NULL,
`sid` int(11) NOT NULL,
`dob` date NOT NULL,
PRIMARY KEY (`rid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
--
-- Dumping data for table `reg`
--
INSERT INTO `reg` (`rid`, `uname`, `pass`, `gender`, `hobby`, `cid`, `sid`, `dob`) VALUES
(17, 'harsh', 'suthar7576', 'male', 'music', 1, 1, '1994-01-31'),
(19, 'harsh', 'suthar7576', 'male', 'music', 2, 1, '1994-02-06'),
(20, 'harsh', 'suthar7576', 'male', 'travel,game', 2, 1, '1994-06-03'),
(21, 'harshawaerstdfyj', 'suthar7576', '', '', 0, 0, '2000-02-29'),
(22, 'harshewretertrytry', 'suthar7576', 'male', 'music', 1, 3, '2000-02-29');
-- --------------------------------------------------------
--
-- Table structure for table `state`
--
CREATE TABLE IF NOT EXISTS `state` (
`cid` int(11) NOT NULL,
`sid` int(11) NOT NULL AUTO_INCREMENT,
`sname` varchar(20) NOT NULL,
PRIMARY KEY (`sid`),
KEY `cid` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `state`
--
INSERT INTO `state` (`cid`, `sid`, `sname`) VALUES
(1, 1, 'Gujarat'),
(1, 2, 'Goa'),
(1, 3, 'Mumbai'),
(2, 4, 'Melbourn'),
(2, 5, 'Sydney');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `state`
--
ALTER TABLE `state`
ADD CONSTRAINT `state_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `country` (`cid`) ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
--------------------------------
Save this code as conection.php
-------------------------------
<?php
$con= new mysqli("localhost","root","","db_harsh"); //( server name ,user name , password, database name )
?>
--------------------------------
Save this code as view.php
-------------------------------
<?php
include_once("conection.php");
$sel="select * from reg";
$sql=$con->query($sel);
?>
<form method="post">
<table border="1">
<tr>
<th></th>
<th scope="col">RID</th>
<th scope="col">USERNAME</th>
<th scope="col">PASSWORD</th>
<th scope="col">GENDER</th>
<th scope="col">HOBBY</th>
<th scope="col">COUNTRY</th>
<th scope="col">STATE</th>
<th scope="col">DATE OF BIRTH </th>
<th scope="col">Age</th>
</tr>
<?php
while($fet=$sql->fetch_object())
{
$birth=$fet->dob;
$dob = new DateTime($birth);
$interval = $dob->diff(new DateTime);
?>
<tr>
<td><?php echo $fet->rid; ?> </td>
<td><?php echo $fet->uname; ?> </td>
<td><?php echo $fet->pass; ?> </td>
<td><?php echo $fet->gender; ?> </td>
<td><?php echo $fet->hobby; ?> </td>
<td><?php echo $fet->cid; ?> </td>
<td><?php echo $fet->sid; ?> </td>
<td><?php echo $fet->dob; ?> </td>
<td><?php echo "Your Age: ".$interval->y; ?> </td>
</tr>
<?php
}
?>
</table>
</form>
---------------------------------------
save this code as reg.php
----------------------------------------
<?php
include_once('conection.php');
//fetching country
$sel = "select * from country";
$count = $con->query($sel); // country table connect to databse
//fetching state
$sels = "select * from state";
$state = $con->query($sels);
if(isset($_POST["insert"]))
{
$uname=$_POST["uname"];
$pass=$_POST["pass"];
$gender=$_POST["gender"];
$hobby=$_POST["chk"];
$hby=implode(",",$hobby); // convert array to string
$coun=$_POST["country"];
$sta=$_POST["state"];
$dob=$_POST["dob"];
$dob=implode(",",$dob); // convert array to string
$ins = "INSERT INTO reg(`uname`, `pass`, `gender`, `hobby`, `cid`, `sid`, `dob`) VALUES ('$uname','$pass','$gender','$hby','$coun','$sta','$dob')";
$con->query($ins);
}
?>
<h3>Register Form</h3>
<form method="post">
<table width="400" border="1" align="center" cellspacing="0" background="freebg1.jpg" bordercolor="#E87400">
<tr>
<th scope="row">Username</th>
<td><input type="text" name="uname" required="required"/></td>
</tr>
<tr>
<th scope="row">Password</th>
<td><input type="password" name="pass" required="required"/></td>
</tr>
<tr>
<th scope="row">Gender</th>
<td><input type="radio" name="gender" value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
</td>
</tr>
<tr>
<th scope="row">Hobby</th>
<td><input type="checkbox" name="chk[]" value="music" />Music
<input type="checkbox" name="chk[]" value="travel" />Travel
<input type="checkbox" name="chk[]" value="game" />Game</td>
</tr>
<tr>
<th scope="row">Country</th>
<td> <select name="country" required="required">
<option value="0">------select-----</option>
<?php
while($coun=$count->fetch_object())
{
?>
<option value="<?php echo $coun->cid; ?>" > <?php echo $coun->cname; ?> </option>
<?php
}
?>
</select></td>
</tr>
<tr>
<th scope="row">State</th>
<td><select name="state" required="required">
<option value="0">------select-----</option>
<?php
while ($sta=$state->fetch_object())
{
?>
<option value="<?php echo $sta->sid;?>"> <?php echo $sta->sname;?> </option>
<?php
}
?>
</select></td>
</tr>
<tr>
<th scope="row">Dob</th>
<td>
<select name="dob[]" value="year" required="required">
<option>Year</option>
<?php
for($i=1994;$i<=2016;$i++)
{
?>
<option value="<?php echo $i; ?>"> <?php echo $i;?> </option>
<?php
}
?>
</select>
<select name="dob[]" value="month" required="required">
<option>Month</option>
<?php
for($m=01;$m<=12;$m++)
{
?>
<option value="<?php echo $m; ?>"> <?php echo $m;?> </option>
<?php
}
?>
</select>
<select name="dob[]" value="date" required="required">
<option>Date</option>
<?php
for($d=01;$d<=31;$d++)
{
?> <option value="<?php echo $d; ?>" > <?php echo $d;?> </option>
<?php
}
?>
</select>
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="insert" value="Register" class="btn btn-success"/></td>
<a href="view.php" >View All Record</a>
</tr>
</table>
</form>
</body>
</html>
0 comments:
Post a Comment