我想用this年齡計算器來計算用戶的年齡,這是存儲在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 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;
?>
什麼不適合你? – drmonkeyninja
從MySQL用戶表獲取日期。 –
更改'「<?php轉義($ user-> data() - > birthday);?>''轉義'($ user-> data() - > birthday);'。沒有必要把它放在一個字符串中,我很確定這是行不通的。 –