如何计算给定出生日期的用户年龄?

时间:2018-08-23 10:58:52

标签: php

我需要从客户表中计算取决于生日的年龄。

这是我的代码:

File file =new File(mediaPath);
        if(file.exists()) {
            UploadTask uploadTask = riversRef.putFile(Uri.fromFile(file),metadata);
            Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    // Continue with the task to get the download URL
                    return riversRef.getDownloadUrl();
                }
            }).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    getUploadedMediaPath.getUploadedFileUrl(downloadUri.toString());
                }
            });
        }

在最后一行中,我需要回显根据从// customer age function calculate_customer_age($startDate,$endDate){ $endDate=date(m-d-y); $startDate= $customer['birthday']; $fromDate= new DateTime($startDate); $toDate= new DateTime($endDate); echo $fromDate->diff($toDate)->days; } 的客户表中获取的内容而计算出的年龄。

enter image description here

6 个答案:

答案 0 :(得分:0)

您可以在函数中使用以下代码,该代码将计算直到今天的年龄。您可以修改$ today以使用所需的结束日期和格式。

use stats_lib
use maths_lib

答案 1 :(得分:0)

您的calculate_customer_age函数中有几个错误,相反,您可以使用下面的代码来获取两个日期之间的天数差异:

function calculate_customer_age($startDate, $endDate)
{
    $startDate = DateTime::createFromFormat("m/d/Y", $startDate);
    $endDate = DateTime::createFromFormat("m/d/Y", $endDate);
    return $startDate->diff($endDate)->days;
}

echo calculate_customer_age("11/30/2008", "08/23/2018"); // 3553

文档:

答案 2 :(得分:0)

您的代码中有很多错误。您无需在函数中传递结束日期。您可以使用以下代码获取年龄。

function calculate_customer_age($startDate){
    $fromDate = new DateTime($startDate);
    $toDate       = new DateTime('today');
    return $fromDate->diff($toDate)->days;
       // or return $fromDate->diff($toDate)->y;
}

echo calculate_customer_age($customer['birthday']);

答案 3 :(得分:0)

PHP> = 5.3.0

面向对象

$from = new DateTime('1970-02-01');
$to   = new DateTime('today');
echo $from->date_diff($to)->y;

过程

echo date_diff(date_create('1970-02-01'), date_create('today'))->y;

MySQL> = 5.0.0

SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age

答案 4 :(得分:0)

如果要以天为单位计算客户的年龄,则应这样做:

function calculate_customer_age($startDate){
        $fromDate= new DateTime($startDate);
        $toDate= new DateTime();
        $difference = $fromDate->diff($toDate)->days;
        return $difference->format('%a');
}

答案 5 :(得分:0)

  

php代码

$startDate = '05-01-1988'; // sample start date or dob
$end_Date = '23-08-2018'; // smmple end date

echo calculate_customer_age( $startDate , $end_Date ); 
  

返回年龄的php函数-

function calculate_customer_age( $startDate , $endDate ) //function returns age   
{   
    $diff = date_diff( date_create( $startDate  ), date_create( $endDate ) );
    return $diff->format('%y Years, %m Months, %d Days');
}

示例输出:

  

30年7个月18天