dounan4479 2015-12-01 08:40
浏览 135
已采纳

从MySQL数据库获取的年龄计算

I am trying to use this age calculator to calculate users' ages, which is stored in a MySQL database. I thought this would work, but it doesn't seem to.

My problem is that I don't know how to get the date, from a users table from MySQL.

<?php
require_once 'core/init.php';

$user = new User();

if(!$user->isLoggedIn()) {
    Redirect::to('index.php');
}

  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "<?php escape($user->data()->birthday); ?>";        //"08/13/2000";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is: " . $age;
?>
  • 写回答

1条回答 默认 最新

  • dongxiaoke2018 2015-12-01 08:57
    关注

    I just wrote this simple class that uses the DateTime class and associated methods - should be easy enough to adapt your date string to be in the correct format.

        class userage{
    
            private $dob;
            private $options;
    
            public function __construct( $dob=false, $options=array() ){
                $this->dob=$dob;
                $this->options=(object)array_merge( array(
                    'timezone'  =>  'Europe/London',
                    'format'    =>  'Y-m-d H:i:s'
                ),$options );
            }
    
            public function calculate(){
                $opts=$this->options;
                $timezone=new DateTimeZone( $opts->timezone );
                $dob=new DateTime( date( $opts->format, strtotime( $this->dob ) ), $timezone );
                $now=new DateTime( date( $opts->format, strtotime( 'now' ) ), $timezone );
                $age = $now->diff( $dob );
    
                $result=(object)array(
                    'years'     =>  $age->format('%y'),
                    'months'    =>  $age->format('%m'),
                    'days'      =>  $age->format('%d'),
                    'hours'     =>  $age->format('%h'),
                    'mins'      =>  $age->format('%i'),
                    'secs'      =>  $age->format('%s')
                );
                $dob=$now=$age=null;
                return $result;
            }
        }
    
        $ua=new userage('1970-09-05');
        $age=$ua->calculate();
    
        echo '<pre>',print_r($age,true),'</pre>';
    
    /*
        outputs
        -------
        stdClass Object
        (
            [years] => 45
            [months] => 2
            [days] => 26
            [hours] => 8
            [mins] => 53
            [secs] => 30
        )
    
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作