【问题标题】:PHP or Javascript/Jquery - Calculate age for newborns/adultsPHP 或 Javascript/Jquery - 计算新生儿/成人的年龄
【发布时间】:2015-08-17 23:50:37
【问题描述】:

我见过的所有函数都得到与出生日期相差的年份,我想得到几个月甚至几天的年龄。

php 示例:

$date = "2015-05-23";
/* whatever */
echo "Has 2 months and 8 days";

$date = "2012-10-30";
/* whatever */
echo "Has 2 years and 2 months";

不管它是在 javascript/jquery 还是 php 中。

谢谢大家!

【问题讨论】:

  • stackoverflow.com/questions/1968167/… 你要做的就是使用现在而不是第二个日期,然后根据需要格式化输出。
  • 由于长度可变,做月比较困难,通过使用 setDate 或 setUTCDate 到 0 你可以找出天数在上个月,所以你可以找到任何一个月的天数,所以如果你有负数的月差,你可以找到要添加到这个值的数字(并从月差中减去一个)
  • 我强烈建议使用库来解决这个问题-moment.js 会浮现在脑海中...... JS 中的日期很笨拙,PHP 也没有那么好

标签: javascript php jquery date


【解决方案1】:

您可以在 PHP 中使用此代码来获取日期与当前日期或其他日期之间的日、月和年的差异

<?php
$date = "2015-07-17";

$difference = strtotime(date("Y-m-d")) - strtotime($date);

$days = abs(round($difference / 86400,0));
$years = abs(round($days /365,0));
$months = abs(round($years/12,0));
echo "Difference in Days: $days| Months: $months| Years: $years";

如果你想在字符串中输入一个数字,你可以使用 stackoverflow 中的这个答案...

Convert number to string

【讨论】:

    【解决方案2】:

    在php中你可以试试这个:

    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo ($diff->format("%a"));
    

    它会返回天数的差异,也可以转换成月和年。

    【讨论】:

      【解决方案3】:

      我修改了一些代码以获得我想要的。除了所有例外情况(如果某些年/月/日为 0,则不显示),此功能完美运行。

      如果有人也需要的话,我会与你分享我的 PHP 代码。

      echo getAge("10-02-2014");
      
      function getAge($fecha) {
      /* $fecha => d-m-Y (in this format!!) */
      $dob = strtotime($fecha);
      $current_time = time();
      $age_years = date('Y',$current_time) - date('Y',$dob);
      $age_months = date('m',$current_time) - date('m',$dob);
      $age_days = date('d',$current_time) - date('d',$dob);
      
      if ($age_days<0) {
          $days_in_month = date('t',$current_time);
          $age_months--;
          $age_days= $days_in_month+$age_days;
      }
      if ($age_months<0) {
          $age_years--;
          $age_months = 12+$age_months;
      }
      $todayString = date('d',$current_time).'-'.date('m',$current_time).'-'.date('Y',$current_time);
      /* keep in mind $today is always superior than $fecha */
      if($fecha == $todayString){
         return "Today"; 
      }  else if(date('Y',$dob) == date('Y',$current_time) && $age_months == "0") {
          return $age_days . " day".plural($age_days);
      } else if(date('Y',$dob) == date('Y',$current_time) && $age_days != "0") {
          return $age_months ." month".plural($age_months)." and ".$age_days ." day".plural($age_days);
      } else if(date('Y',$dob) == date('Y',$current_time) && $age_days == "0") {
          return $age_months . " month".plural($age_months);
      } else if($age_years != "0" &&  $age_months != "0" && $age_days == "0") {
          return $age_years . " year".plural($age_years)." and ".$age_months." month".plural($age_months);
      }  else if($age_years != "0" &&  $age_months == "0" && $age_days != "0") {
          return $age_years . " year".plural($age_years)." and ".$age_days." day".plural($age_days);
      } else if($age_years != "0" &&  $age_months == "0" && $age_days == "0") {
          return $age_years . " year".plural($age_years);
      } else { /* if you want to show the days always, add ----> ." and ".$age_days." day".plural($age_days) <---- below */
          return $age_years. " year".plural($age_years)." and ".$age_months." month".plural($age_months);
      } }
      
      function plural($num) {
      if($num!="1"){
          return "s";
      } }
      

      啊,记住,也许你需要在之前设置时区。

      date_default_timezone_set("Europe/Madrid");
      

      【讨论】:

        猜你喜欢
        • 2011-03-23
        • 1970-01-01
        • 2017-11-21
        • 2016-03-20
        • 2020-12-28
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        相关资源
        最近更新 更多