0

I'm trying to exclude those playername which is 15 younger not to show in <option></option> is my approach okay? I don't want to put age in database. or is possible to convert datebirth into yearsold now from sql query?

I have 3 data in player which is
Name | Gender | Age
A    | Male   | 15
B    | Male   | 13
C    | Male   | 16


<?php
$sql = "SELECT * FROM player WHERE Playergender='Male'"; 
$result = mysqli_query($con,$sql);
echo "<span>Select Player:</span><select>";
echo "<option>--Select Player--</option>";
while($row = mysqli_fetch_array($result))
  {
$Playername=$row['playername'];
$Datebirth=$row['Datebirth'];
$from = new DateTime($Datebirth);
$to   = new DateTime('today');
$yearsold=$from->diff($to)->y;
if(yearsold<15)
{
echo "<option value=ID>" . $Playername . "<option>";
else
echo "<option value=ID>" . $Playername . "</option>;
?>

2 Answers2

0

MySQL query for fetching users above 15 years old will be:

$sql = "SELECT * FROM player WHERE Playergender='Male' AND Datebirth < (CURDATE() - INTERVAL 15 YEAR)"; 
Qarib Haider
  • 4,796
  • 5
  • 27
  • 38
  • thanks man it works :) I change the ">" into "<" because it didn't get the 15 years above if i use ">" it do the opposite thing. – ImNoobSarry Aug 03 '14 at 11:01
0

It's possible to get age using MySQL: How to get the difference in years from two different dates?

So in your case this would be

SELECT *,YEAR(CURRENT_TIMESTAMP) - YEAR(Datebirth) - (DATE_FORMAT(CURRENT_TIMESTAMP, '%m%d') < DATE_FORMAT(Datebirth, '%m%d')) as age FROM player WHERE Playergender='Male' WHERE age<15

If you don't need data for another players, this approach will be better performance-wise.

Community
  • 1
  • 1
Konstantin Pereiaslov
  • 1,786
  • 1
  • 18
  • 26
  • thanks this seem more complicated if I use this code i get the query error unknown column of age the conversion of datebirth from datenow should be = age I tried but it failed. anyway thank you for this. I'll look into the thread and try working how to do this. – ImNoobSarry Aug 03 '14 at 11:05