PHP calculate age
This works fine.
<?php
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = "12/17/1983";
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
? ((date("Y") - $birthDate[2]) - 1)
: (date("Y") - $birthDate[2]));
echo "Age is:" . $age;
?>
Calculate age based on date of birth
PHP >= 5.3.0
# object oriented
$from = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $from->diff($to)->y;
# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;
demo
functions: date_create()
, date_diff()
MySQL >= 5.0.0
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
demo
functions: TIMESTAMPDIFF()
, CURDATE()
PHP: How to calculate person age in months
Using PHP's DateInterval
(available from 5.3.0), that's pretty easy:
$birthday = new DateTime('1990-10-13');
$diff = $birthday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
Now $months
will contain the number of months I've lived.
Calculate age with PHP in YYYY MM DD format
Always be careful with date notation (ie. order of years, months and days).
This is working (original code adapted):
<?php
$birthDate = "1984-05-21";
$birthDate = explode("-", $birthDate);
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md") ? ((date("Y")-$birthDate[0])-1):(date("Y")-$birthDate[0]));
echo "Age is: ".$age;
?>
Calculate age in a list with Laravel
age calculation by using Laravel carbon.
use Carbon\Carbon;
$dateOfBirth = '1994-07-02';
$years = Carbon::parse($dateOfBirth)->age;
As per your code.
@if(!empty($pets))
@foreach($pets as $pet)
{{ Carbon::parse($pet->date_birth)->age ?? 0 }} years
@endforeach
@endif
PHP calculate age with DateTime::diff
than, use ajax instead.... this is really some quick and dirty solution...so you should adapt and improve this..
//getBirtday.php
<?php
$age = "";
$data = $_POST['birthday'];
if(!empty($data)){
$birthday = $data['year'].'-'.$data['month'].'-'.$data['date'];
$biday = new DateTime($birthday);
$today = new DateTime();
$diff = $today->diff($biday);
$age = $diff->y;
}
echo $age;
?>
//test.php (get sure your jquery is loaded....)
<script>
$(document).ready(function(){
$('#year').on("change", function(){
birthday["date"] = $("#day").val();
birthday["month"] = $("#month").val();
birthday["year"] = $("#year").val();
$.ajax({
url: 'getBirthday.php',
method: "POST",
data: {birthday:birthday},
error: function(error) {
alert("ERROR"+error);
},
success: function(data) {
$("#age").val(data);
}
});
});
});
</script>
</p> <p>AGE :</label> <input type="text" name="age" id="age" value="<?=$age?>" readonly></p>
<form name="form1" method="post" action="#">
BDAY:<?php
echo "<select name='date' id='day'>";
echo "<option selected='selected'>date</option>";
for($a=1; $a<=31; $a+=1)
{
echo"<option value=$a> $a </option>";
}
echo "</select>";
echo "<select name='month' id='month'>";
echo "<option selected='selected'>month</option>";
$month=array(1=> "Januari",2 => "Februari", 3=> "Maret", 4=> "April", 5=> "Mei", 6=> "Juni", 7=>"Juli", 8=> "Agustus", 9=> "September", 10=>"Oktober",11=>"November",12=>"Desember");
for($c=0; $c<count($month); $c++)
{
echo "<option value=$c> $month[$c] </option>";
}
echo "</select>";
$now=date('Y');
echo "<select name='year' id='year'>";
echo "<option selected='selected'>year</option>";
for ($a=1990;$a<=$now;$a++)
{
echo "<option value='$a'>$a</option>";
}
echo "</select>";
?>
</form>
How to calculate the age of a person using variables like: year, month, day in PHP
You can use this for age calculation:
$today = new DateTime("now");
$birthdate = new DateTime("$year-$month-$day");
$age = $today->diff($birthdate)->y;
Update:
So the whole code with some cleanup will be like:
<?php
$name = $_POST['name'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$today = new DateTime("now");
$birthdate = new DateTime("$year-$month-$day");
$age = $today->diff($birthdate)->y;
$nos = $_POST['nos'];
$gender = $_POST['gender'];
$loa = $_POST['loa'];
?>
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Name:`</b>
<?php echo $name; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Birth Date:`</b>
<?php echo $birthdate->format('Y-m-d'); ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Age:`</b> aprox.
<?php echo $age; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Name of School:`</b>
<?php echo $nos; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Gender:`</b>
<?php echo $gender; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Level of Academics:`</b>
<?php echo $loa; ?>
</div>;
Related Topics
Preserve Line Breaks from Textarea
How to Select and Upload Multiple Files With HTML and PHP, Using Http Post
Storing Files in Database VS File System
Having Issue With Matching Rows in the Database Using Pdo
PHP, How to Catch a Division by Zero
PHP Preg_Match to Find Multiple Occurrences
How to Echo a Variable With Single Quotes
I Need My PHP Page to Show My Blob Image from MySQL Database
Error With PHP Mail(): Multiple or Malformed Newlines Found in Additional_Header
How to Add/Remove Pkcs7 Padding from an Aes Encrypted String
Download File Through an Ajax Call PHP
How to Create an Array For Json Using PHP
How to Delete a Line from the File With PHP
Print Numeric Values to Two Decimal Places
What Is the Purpose of the Question Marks Before Type Declaration in PHP7 (String or Int)