Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Age counter only counting years

Posted on 2010-09-05
3
Medium Priority
?
241 Views
Last Modified: 2013-12-13
Hi experts,

In my site I've got two age counters in a loop but they only count the years.

For example someone with birth date 2003-9-30 will be precented as a 7 years old person.
But he should be precented as a 6 year old. What should I change in my current code set this right?

Thanks

<?php
if(!isset($_POST['team'])){header("Location: samenstellen_keuze.php");} 
include('../includes/config.php'); 
access_control();

if(isset($_GET['moveto'])){$type = $_GET['moveto'];
	if ($type == "right"){
		if(isset($_POST['moveright'])){
			$moveright_array = $_POST['moveright'];
			foreach($moveright_array as $value1){
			  $sql="INSERT INTO functies (functietype, groepsID, persoonsID) VALUES ('41', '{$_POST['team']}', '$value1')";
			  $Result1 = mysql_query($sql,$con)or die(mysql_error());
			}
		unset($_POST['moveright']);
		}
	}
	if ($type == "left"){
		if(isset($_POST['moveleft'])){
			$moveleft_array = $_POST['moveleft'];
			foreach($moveleft_array as $value2){
	  		  	mysql_select_db($database, $con);
			  	$sql="delete from functies where functietype = '41' AND groepsID = '{$_POST['team']}' AND persoonsID = '$value2'";
			  	$Result1 = mysql_query($sql,$con)or die(mysql_error());
			}
		unset($_POST['moveleft']);  
		}
	}
}

$sql = "SELECT id, naam FROM groepen where id=". $_POST['team'];
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs)
?>
<html>
<head>
<link rel='stylesheet' href='../tree1.css'>
</head>
<body>
<form name="samenstellen" method="post" action="samenstellen.php?moveto=right">
<table align="center" border=0>
<tr><td colspan=3 align="center"><h3>Team indelingen</h3></td></tr>
<?
if(isset($_POST['leeftijdvan']) && $_POST['leeftijdtot'] != "") {
echo "<tr><td align=\"center\"><b>Leden database <font size=1>(gefilterd van ". $_POST['leeftijdvan']. " jaar tot ". $_POST['leeftijdtot']." jaar)<br>Minus de leden die al aan teams zijn toegewezen</font></b></td><td></td><td align=\"center\"><b>Team : ". $row['naam'] ."</b></td></tr>";
}else{
echo "<tr><td align=\"center\"><b>Leden database <font size=1>(Geen filter op leeftijd)<br>Minus de leden die al aan teams zijn toegewezen</font></b></td><td></td><td align=\"center\"><b>Team : ". $row['naam'] ."</b></td></tr>";
}
?>	
<tr>
<td> 
<select style="width: 300px; overflow:auto;" multiple="multiple" SIZE="40" name="moveright[]">
<?php

if(isset($_POST['leeftijdvan']) && $_POST['leeftijdtot'] != "") {
$sql="SELECT * FROM personen WHERE str_to_date(geboortedatum,'%Y-%m-%d') <= date_add(CURDATE(), INTERVAL -".$_POST['leeftijdvan']." YEAR) and str_to_date(geboortedatum,'%Y-%m-%d') > date_add(CURDATE(), INTERVAL -1 -".$_POST['leeftijdtot']." YEAR) AND ID not in (select persoonsID from functies where functietype = '41') order by achternaam";
}else{
	echo "geen filter";
$sql="SELECT * FROM personen WHERE ID not in (select persoonsID from functies where functietype = '41') order by achternaam";
}	
$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
	
	if (!($row['geboortedatum'] == "")){
		$arrDate = explode('-', $row['geboortedatum']); 
		//$timestamp = mktime( 0 , 0 , 0 , $arrDate[1], $arrDate[2], $arrDate[0]);
		$jaren= date( "Y" ) - $arrDate[0];
	  	echo "<option value=\"".$row['id']."\">".$row['achternaam'].", ". $row['voornaam']." ". $row['tussenvoegsel']. " (". $jaren. " Jaar) ".$row['geboortedatum']."\n  ";
	}else{
      	echo "<option value=\"".$row['id']."\">".$row['achternaam'].", ". $row['voornaam']." ". $row['tussenvoegsel']."\n  ";
	}	
}
?>
</select>
</td><td>
<input name="submit" type="submit" value="Voeg toe >" /><br>
<input type=hidden value="<? echo $_POST['team']; ?>" name="team" id="team">		        
<input type=hidden value="<? echo $_POST['leeftijdvan']; ?>" name="leeftijdvan" id="leeftijdvan">		        
<input type=hidden value="<? echo $_POST['leeftijdtot']; ?>" name="leeftijdtot" id="leeftijdtot">		        
</form>

<form name="samenstellen" method="post" action="samenstellen.php?moveto=left">
<input name="submit" type="submit" value="< Verwijder" /></td>
<td>
<select style="width: 300px; overflow:auto;" multiple="multiple" SIZE="40" name="moveleft[]">
<?php
$sql = "SELECT personen.id, voornaam, tussenvoegsel, achternaam, geboortedatum FROM personen INNER JOIN functies ON personen.id = functies.persoonsID WHERE functies.groepsID ='".$_POST['team']."' AND functies.functietype = '41'  ORDER BY achternaam";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
	if (!($row['geboortedatum'] == "")){
		$arrDate = explode('-', $row['geboortedatum']); 
		//$timestamp = mktime( 0 , 0 , 0 , $arrDate[1], $arrDate[2], $arrDate[0]);
		$jaren= date( "Y" ) - $arrDate[0];
	  	echo "<option value=\"".$row['id']."\">".$row['achternaam'].", ". $row['voornaam']." ". $row['tussenvoegsel']. " (". $jaren. " Jaar) ".$row['geboortedatum']."\n";
	}else{
      	echo "<option value=\"".$row['id']."\">".$row['achternaam'].", ". $row['voornaam']." ". $row['tussenvoegsel']."\n";
	}	
}
?>
</select>
<input type=hidden value="<? echo $_POST['team']; ?>" name="team" id="team">		        
<input type=hidden value="<? echo $_POST['leeftijdvan']; ?>" name="leeftijdvan" id="leeftijdvan">		        
<input type=hidden value="<? echo $_POST['leeftijdtot']; ?>" name="leeftijdtot" id="leeftijdtot">			        

</td></tr>
<tr><td align="center" colspan=3><INPUT type="button" value="Terug" onClick="location.href='../../beheer/verkeersplein.php'"><INPUT type="button" value="Een ander team kiezen" onClick="location.href='samenstellen_keuze.php'"></td></tr>
</table>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Steynsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 33608076
Can't quite follow your code, but here is a teaching example of how to get the age on a given date.

HTH, ~Ray
<?php // RAY_age_on_date.php
error_reporting(E_ALL);

// A FUNCTION TO RETURN THE AGE ON A GIVEN DATE
function age_group($birth_date, $test_date='Today')
{
    // IF THE DATE IS NOT USABLE
    if (!$timestamp = strtotime($birth_date)) return FALSE;
    if (!$teststamp = strtotime($test_date))  return FALSE;

    // GET THE YEAR AND SUBTRACT FROM THIS YEAR
    $year_birth = date('Y', $timestamp);
    $year_now   = date('Y', $teststamp);
    $years_elapsed = $year_now - $year_birth - 1;

    // GET THE MONTH AND DAY IN THE STYLE OF ISO8601 DATES
    $moday_birth = date('m-d', $timestamp);
    $moday_cut   = date('m-d', $teststamp);

    // IF THE BIRTHDAY IS BEFORE CUTOFF ADD ONE YEAR
    if ($moday_birth <= $moday_cut) $years_elapsed++;

    return $years_elapsed;
}


// TEST DATA
echo "<pre>";

// A BIRTHDAY
$dt = date('Y-m-d', strtotime('tomorrow'));

// COUNT BACKWARDS FOR EACH BIRTHDAY
while ($dt > '1990-05-01')
{
    echo "\nA CHILD BORN ON " . $dt; echo " WILL BE AGE " . age_group($dt) . " TODAY";
    $dt = date('Y-m-d', strtotime($dt . ' - 1 day'));
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Steynsk
ID: 33608149
Thanks a lot, exactly the function I needed
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33608169
You're welcome, it's a great question -- thanks for the points! ~Ray
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question