doumi5223 2018-10-18 19:12 采纳率: 100%
浏览 14
已采纳

计算日期差异

I'm calculating the age of a user by subtracting his age from today's date. The problem I have is that it show their age as whole number. But rather than showing it as a whole number, I'd like to display it as decimal like 25.5 or 27.3. I tried using round and number_format but neither worked.

$bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
$now = date("Y/m/d");
$diff = ($now) - date($bday);
echo number_format(round($diff, 2), 2); 
  • 写回答

1条回答 默认 最新

  • douliexu5623 2018-10-18 19:43
    关注

    you can try this:

    $bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
    echo  number_format(round((strtotime(date('Y-m-d')) - strtotime($bday))/(60*60*24*365),2),2);
    

    However, above calculation is very rough. if you want get value more precisely you should use date_diff for that:

    $bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
    $date1=date_create($bday); //create date object
    $date2=date_create(date("Y-m-d")); //create date object
    $curyearmaxdays=date("z", mktime(23,59,59,12,31,date("y")))+1; //count of days in the current year
    $diff = date_diff($date1, $date2); //difference between two date objects
    $realdiff=$diff->y + $diff->m/12+$diff->d/$curyearmaxdays;
    echo  number_format(round($realdiff,2),2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理