Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySQL数据库进行年龄计算_Php_Mysql - Fatal编程技术网

Php 从MySQL数据库进行年龄计算

Php 从MySQL数据库进行年龄计算,php,mysql,Php,Mysql,我正在尝试使用年龄计算器来计算用户的年龄,它存储在MySQL数据库中。我原以为这样行得通,但似乎不行 我的问题是,我不知道如何从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

我正在尝试使用年龄计算器来计算用户的年龄,它存储在MySQL数据库中。我原以为这样行得通,但似乎不行

我的问题是,我不知道如何从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;
?>

我刚刚编写了这个简单的类,它使用了
DateTime
类和相关方法,应该很容易将日期字符串调整为正确的格式

    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
    )

*/
class用户年龄{
私人股本$dob;
私人美元期权;
公共函数构造($dob=false,$options=array()){
$this->dob=$dob;
$this->options=(对象)数组\合并(数组)(
“时区”=>“欧洲/伦敦”,
'格式'=>'Y-m-d H:i:s'
)美元期权);
}
公共功能计算(){
$opts=$this->options;
$timezone=newdatetimezone($opts->timezone);
$dob=新日期时间(日期($opts->format,STROTOTIME($this->dob)),$TIMEONE);
$now=新日期时间(日期($opts->format,STROTTIME('now')),$timezone);
$age=$now->diff($dob);
$result=(对象)数组(
“年”=>$age->格式(“%y”),
“月数”=>$age->格式(“%m”),
“天”=>$age->格式(“%d”),
“小时数”=>$age->格式(“%h”),
“分钟”=>$age->格式(“%i”),
“秒”=>$age->格式(“%s”)
);
$dob=$now=$age=null;
返回$result;
}
}
$ua=新用户年龄('1970-09-05');
$age=$ua->calculate();
echo“”,print_r($age,true),“”;
/*
输出
-------
stdClass对象
(
[年]=>45
[月]=>2
[天]=>26天
[小时]=>8小时
[分钟]=>53
[秒]=>30秒
)
*/

什么对您不起作用?从MySQL用户表中获取日期。将
更改为
转义($user->data()->生日)。不需要把它放在一个字符串中,我很确定这是否起作用。仍然是错误,今年是回音。。。my MySQL中的日期格式:“2000-01-03”。那么,运行
var\u dump($birthDate)
并检查您得到了什么。做一些基本的调试。这并不能解决我的问题。脚本本身工作得非常好,我唯一的问题是从MySQL用户表中获取日期。我看到3分钟前你补充说问题本身只是从数据库获取数据。这应该是一个相当简单的sql查询