使用 PHP 根据 JSON 数组中的出生日期信息计算一个人的年龄

发布于 2024-10-25 13:16:14 字数 695 浏览 2 评论 0原文

我想知道如何使用 PHP 来计算一个人的年龄,给定一个带有 JSON 数组值的日期字符串:

{  
    "DOB":{ // should I use integers or strings for these values? I'm assuming strings for now.  
        "year": "1970",  
        "month": "01",  
        "day": "01"  
    }  
}

然后我将如何使用 PHP 来计算年龄?
此外,假设所有这些信息都是员工目录之类的一小部分,格式为 { "People":{ "user1":{ label: value },"user2":{ label: value} } }。如何使用 PHP 在 value

  • 标签内为 label 创建 标签(
  • value
  • ) 在 div 内每个用户的无序列表中每个用户?
    告诉我这是否令人困惑;而年龄的计算是我现在最优先考虑的事情。

    I was wondering how I could use PHP to calculate one's age given a date string with values from a JSON array:

    {  
        "DOB":{ // should I use integers or strings for these values? I'm assuming strings for now.  
            "year": "1970",  
            "month": "01",  
            "day": "01"  
        }  
    }
    

    How would I then use PHP to calculate the age?
    Additionally, say all of this information is a small part of something like a staff directory, with the format of { "People":{ "user1":{ label: value },"user2":{ label: value} } }. How could I use PHP to create <label> tags for label within <li> tags for value (<li><label>label</label>value</li>) within unordered lists for each user within divs for each user?
    Please tell me if this is confusing; and the calculation of the age is my highest priority right now.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    随梦而飞# 2024-11-01 13:16:14

    我假设你要使用这个:

    http://php.net/manual/ en/function.json-decode.php

    将其转换为 php 可以使用的数组。

    <?php
    
    $arr = json_decode('{"DOB":{"year": "1970", "month": "01", "day": "01"}}');
    $year = $arr->DOB->year;
    $month = $arr->DOB->month;
    $day = $arr->DOB->day;
    
    $today = date("Y-m-d"); 
    $bday = date("Y-m-d",mktime(0, 0, 0, $month, $day, $year));
    
    $today = new DateTime($today);
    $bday = new DateTime($bday);
    $age = $today->diff($bday)->y;
    
    echo $age;
    
    
    ?>
    

    我相信这应该在 PHP 5.3+ 中工作。

    Im assuming you are going to use this:

    http://php.net/manual/en/function.json-decode.php

    convert it to an array php can use.

    <?php
    
    $arr = json_decode('{"DOB":{"year": "1970", "month": "01", "day": "01"}}');
    $year = $arr->DOB->year;
    $month = $arr->DOB->month;
    $day = $arr->DOB->day;
    
    $today = date("Y-m-d"); 
    $bday = date("Y-m-d",mktime(0, 0, 0, $month, $day, $year));
    
    $today = new DateTime($today);
    $bday = new DateTime($bday);
    $age = $today->diff($bday)->y;
    
    echo $age;
    
    
    ?>
    

    This should work in PHP 5.3+ i believe.

    烟沫凡尘 2024-11-01 13:16:14

    不确定我是否正确理解了您要对用户执行的操作,因此如果我弄错了,请告诉我:

    <?php
        $people = json_decode($data);
    ?>
    <html>
        <head>
            <title>Users List</title>
        </head>
    <body>
        <?php foreach ($people as $userName => $userInfo): ?>
        <div>
            <p><?php echo $userName; ?>
            <ul>
            <?php foreach ($userInfo as $label => $value): ?>
                <li><label><?php echo $label; ?></label> <?php echo $value; ?></li>
            <?php endforeach; ?>
            </ul>
        </div>
        <?php endforeach; ?>
    </body>
    </html>
    

    Not sure if I correctly understood what you're trying to do with the user so let me know if i was mistaken:

    <?php
        $people = json_decode($data);
    ?>
    <html>
        <head>
            <title>Users List</title>
        </head>
    <body>
        <?php foreach ($people as $userName => $userInfo): ?>
        <div>
            <p><?php echo $userName; ?>
            <ul>
            <?php foreach ($userInfo as $label => $value): ?>
                <li><label><?php echo $label; ?></label> <?php echo $value; ?></li>
            <?php endforeach; ?>
            </ul>
        </div>
        <?php endforeach; ?>
    </body>
    </html>
    
    无语# 2024-11-01 13:16:14

    像这样的事情:

    <?php
    $str = '{"DOB":{"year": "1970","month": "01","day": "01"}}';
    $obj = json_decode($str);
    $obj->DOB->year = intval($obj->DOB->year);
    $obj->DOB->month = intval($obj->DOB->month);
    $obj->DOB->day = intval($obj->DOB->day);
    $yearD  = date("Y") - $obj->DOB->year;
    $monthD = date("m") - $obj->DOB->month;
    $dayD   = date("d") - $obj->DOB->day;
    if ($dayD < 0 || $monthD < 0)
        $yearD--;
    echo "<pre>";
    echo $yearD;
    echo "</pre>";
    ?>
    

    没有考虑闰年。只要算一下就没有了。闰年,并添加尽可能多的天数。

    关于你的第二个问题,你可以访问这个:Convert JSON to HTML Tree

    Something like this:

    <?php
    $str = '{"DOB":{"year": "1970","month": "01","day": "01"}}';
    $obj = json_decode($str);
    $obj->DOB->year = intval($obj->DOB->year);
    $obj->DOB->month = intval($obj->DOB->month);
    $obj->DOB->day = intval($obj->DOB->day);
    $yearD  = date("Y") - $obj->DOB->year;
    $monthD = date("m") - $obj->DOB->month;
    $dayD   = date("d") - $obj->DOB->day;
    if ($dayD < 0 || $monthD < 0)
        $yearD--;
    echo "<pre>";
    echo $yearD;
    echo "</pre>";
    ?>
    

    Didnt take leap years into account. Just calculate no. of leap years, and add as many days.

    And about your second question, you could visit this: Convert JSON to HTML Tree

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文