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 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?