Hướng dẫn php calculate age in years and months - php tính tuổi theo năm và tháng

Có thể trùng lặp: Làm thế nào tôi có thể tính độ tuổi của một người trong năm, tháng, ngày?
How can I calculate the age of a person in year, month, days?

Tôi muốn tính độ tuổi của một người được đưa ra ngày sinh và ngày hiện tại tính theo các năm, tháng và ngày so với ngày hiện tại.

Ví dụ:

>>> calculate_age(2008, 01, 01)
1 years, 0 months, 16 days

Hướng dẫn php calculate age in years and months - php tính tuổi theo năm và tháng

Khi được hỏi ngày 2 tháng 5 năm 2012 lúc 9:18May 2, 2012 at 9:18

2

Bạn có thể sử dụng Php

$interval = date_diff(date_create(), date_create('2008-01-01 10:30:00'));
echo $interval->format("You are  %Y Year, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");
1 http://php.net/manual/en/function.date-diff.php hoặc http://www.php.net/manual/en/datetime.diff.php để đạt được những gì bạn muốn và Bạn có thể xuất nó ở bất kỳ định dạng nào bạn muốn sử dụng định dạng ngày
$interval = date_diff(date_create(), date_create('2008-01-01 10:30:00'));
echo $interval->format("You are  %Y Year, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");
2

$interval = date_diff(date_create(), date_create('2008-01-01 10:30:00'));
echo $interval->format("You are  %Y Year, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");

tiếng vang

You are 04 Year, 04 Months, 1 Days, 00 Hours, 56 Minutes, 36 Seconds Old

Đã trả lời ngày 2 tháng 5 năm 2012 lúc 9:27May 2, 2012 at 9:27

Hướng dẫn php calculate age in years and months - php tính tuổi theo năm và tháng

BabababaBaba

92.9K28 Huy hiệu vàng164 Huy hiệu bạc215 Huy hiệu đồng28 gold badges164 silver badges215 bronze badges

3

<?php
date_default_timezone_set('Asia/Calcutta');

function findage($dob)
{
    $localtime = getdate();
    $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
    $dob_a = explode("-", $dob);
    $today_a = explode("-", $today);
    $dob_d = $dob_a[0];$dob_m = $dob_a[1];$dob_y = $dob_a[2];
    $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
    $years = $today_y - $dob_y;
    $months = $today_m - $dob_m;
    if ($today_m.$today_d < $dob_m.$dob_d) 
    {
        $years--;
        $months = 12 + $today_m - $dob_m;
    }

    if ($today_d < $dob_d) 
    {
        $months--;
    }

    $firstMonths=array(1,3,5,7,8,10,12);
    $secondMonths=array(4,6,9,11);
    $thirdMonths=array(2);

    if($today_m - $dob_m == 1) 
    {
        if(in_array($dob_m, $firstMonths)) 
        {
            array_push($firstMonths, 0);
        }
        elseif(in_array($dob_m, $secondMonths)) 
        {
            array_push($secondMonths, 0);
        }elseif(in_array($dob_m, $thirdMonths)) 
        {
            array_push($thirdMonths, 0);
        }
    }
    echo "<br><br> Age is $years years $months months.";
}

findage("21-04-1969"); //put date in the dd-mm-yyyy format
?>

Đã trả lời ngày 2 tháng 5 năm 2012 lúc 9:35May 2, 2012 at 9:35

Hướng dẫn php calculate age in years and months - php tính tuổi theo năm và tháng

Arunarunarun

3.6573 huy hiệu vàng28 Huy hiệu bạc54 Huy hiệu đồng3 gold badges28 silver badges54 bronze badges

1

<?php
$ageTime = mktime(0, 0, 0, 9, 9, 1919); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

echo 'You are ' . floor($ageYears) . ' years old.';
?>

Nguồn

Đã trả lời ngày 2 tháng 5 năm 2012 lúc 9:21May 2, 2012 at 9:21

AhatiusahatiusAhatius

4.59711 Huy hiệu vàng48 Huy hiệu bạc77 Huy hiệu đồng11 gold badges48 silver badges77 bronze badges

3

Đã trả lời ngày 2 tháng 5 năm 2012 lúc 9:28May 2, 2012 at 9:28

Trong bài đăng này, bạn sẽ học cách tính độ tuổi hiện tại theo năm, tháng và ngày sử dụng ngôn ngữ lập trình PHP. Đôi khi chúng ta cần tính độ tuổi của người dùng trong các ứng dụng web. Tính toán tuổi là một quá trình rất đơn giản. Chúng ta sẽ phải tìm sự khác biệt giữa năm hiện tại và năm sinh. PHP cung cấp các cách tiếp cận khác nhau để tính độ tuổi. Ở đây, chúng tôi đã đề cập đến hầu hết trong số họ.PHP programming language. Sometimes we need to calculate the age of the user in web applications. Calculating age is a very simple process. We will have to find the difference between the current year and the birth year. PHP provides various approaches to calculate the age. Here, we have mentioned most of them.

Php tính toán tuổi bằng phương thức DateTime ()

Trong ví dụ đã cho, chúng tôi đang tính toán tuổi bằng cách sử dụng hàm php diff () của DateTime. Độ tuổi hiện tại được tìm thấy bằng cách có được sự khác biệt giữa hai ngày.diff() function of DateTime. The current age is found by getting the difference between two dates.

<?php 
// PHP program to calculate age in years
// Define the date of birth
$dateOfBirth = '15-03-1987';
 
// Create a datetime object using date of birth
$dob = new DateTime($dateOfBirth);
 
// Get current date
$now = new DateTime();
 
// Calculate the time difference between the two dates
$diff = $now->diff($dob);
 
// Get the age in years, months and days
echo "Your current age is ".$diff->y." years.
"; // Get the age in years, months and days echo "Your current age is ".$diff->y." years ".$diff->m." months ".$diff->d." days."; ?>

Đầu ra mẫu của chương trình trên:

Your current age is 34 years.
Your current age is 34 years 5 months 0 days.

Php tính tuổi bằng phương thức Date_diff ()

Ở đây, chúng tôi đã sử dụng hàm date_diff () để tính độ tuổi hiện tại theo năm, tháng và ngày.date_diff() function to calculate the current age in years, months, and days.

<?php
// PHP program to calculate age in years
// Define the date of birth
$dateOfBirth = '20-04-1988';
 
// Get today's date
$now = date("Y-m-d");
 
// Calculate the time difference between the two dates
$diff = date_diff(date_create($dateOfBirth), date_create($now));
 
// Get the age in years, months and days
echo "your current age is ".$diff->format('%y')." Years ".$diff->format('%m')." months ".$diff->format('%d')." days";
?>

Đầu ra mẫu của chương trình trên:

Your current age is 32 Years 4 months 21 days.

Php tính tuổi bằng phương thức Date_diff ()

Ở đây, chúng tôi đã sử dụng hàm date_diff () để tính độ tuổi hiện tại theo năm, tháng và ngày.

function calculate_age( $date ) { 
    $age = date('Y') - $date; 
   if (date('md') < date('md', strtotime($date))) { 
       return $age - 1; 
   } 
   return $age; 
} 

echo calculate_age('1987')." years old.";

Đầu ra mẫu của chương trình trên:

$interval = date_diff(date_create(), date_create('2008-01-01 10:30:00'));
echo $interval->format("You are  %Y Year, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");
0

Php tính tuổi bằng phương thức Date_diff ()

Ở đây, chúng tôi đã sử dụng hàm date_diff () để tính độ tuổi hiện tại theo năm, tháng và ngày.
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP calculate percentage of total
PHP sanitize input for MySQL
Display PDF using an AJAX call
How to fetch data from database in php and display in pdf
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Sending HTML form data to an email address