我有問題。 我想弄清楚這個問題的答案,但我似乎無法包圍我的思想。 希望有人能幫助我。 現在的問題是:PHP日期問題
寫一個函數,將某人的名字,姓氏,出生年份,月份和日期作爲參數,並給出全名+年齡。確保應用程序/程序在2015年或以後也可以提供正確的結果。檢查前兩個參數是否是字符串,三分之一是否是整數。
由於我是PHP的新手,並沒有約會日期(我GOOGLE了,但我無法讓我的心靈纏繞它) 在此先感謝! :) (寫了一些代碼,自己去嘗試,但它是無用的,所以我離開它了)
我有問題。 我想弄清楚這個問題的答案,但我似乎無法包圍我的思想。 希望有人能幫助我。 現在的問題是:PHP日期問題
寫一個函數,將某人的名字,姓氏,出生年份,月份和日期作爲參數,並給出全名+年齡。確保應用程序/程序在2015年或以後也可以提供正確的結果。檢查前兩個參數是否是字符串,三分之一是否是整數。
由於我是PHP的新手,並沒有約會日期(我GOOGLE了,但我無法讓我的心靈纏繞它) 在此先感謝! :) (寫了一些代碼,自己去嘗試,但它是無用的,所以我離開它了)
下面是一個簡單的函數來做到這一點
//birthday YYYY-MM-DD
function userInfo($fname, $lname, $birthday) {
//Calc age
$age = date_diff(date_create($birthday), date_create('today'))->y;
return $fname ." ". $lname . " ".$age;
}
現在
稱之爲
echo userInfo('John', 'Doe', '1986-02-01');
很酷,這似乎做我想要的東西 我怎麼不明白代碼真的Date_create是一個PHP的構建功能和date_diff我也假設?(我會盡力谷歌瞭解更多信息,以更好地瞭解它!只是真的需要學習如何用代碼^ _^ – Xereoth 2014-09-10 11:23:47
<?php
function getAge($f_name, $l_name, $b_day, $b_month, $b_year) {
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = $b_month . "/" . $b_day . "/" . $b_year;
//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]));
return $f_name ." ". $l_name ." is: " . $age;
}
?>
Thansk爲你的答案,我怎麼會猜測它從我的嘗試。認爲它太複雜了,我實際上需要:) – Xereoth 2014-09-10 11:25:55
只要做得很快,顯然有更好的方法,你也想檢查變量,以確保它們是一個int而不是一個字符串等等 – 2014-09-10 11:29:49
使用此功能
<?php
function get_the_user($fname, $lname,$m ,$d,$y)
$birthDate =$m.'/'.$d.'/'.$y;
//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;
return $fname.' '.$lname.'is '.$age.' old';
?>
爲什麼不創建一個Person對象並創建一些方法來處理邏輯和格式。像:
class Person {
public $firstName, $lastName, $dob;
public function __construct($firstName, $lastName, $dob) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->dob = new DateTime($dob);
}
public function getFullname() {
return $firstName . ' ' . $lastName;
}
public function getAge($onDate=null) {
if ($onDate === null) $onDate = date('Y-m-d');
$onDate = new DateTime($onDate);
$ageOnDate = $this->dob->diff($onDate)->y;
return $ageOnDate;
}
}
$dude = new Person('John', 'Doe', '1980-01-15');
echo $dude->getFullName() . "\n";
echo $dude->getAge() . "\n"; // get his age today
echo $dude->getAge('2015-06-01'); // get his age on June 1st, 2015
您可以使用DateTime
類對日期時間進行簡單操作。
function getAge($name, $surname, $b_year, $b_month, $b_date)
{
$today = new DateTime();
$birthdate = new DateTime($b_year . '-' . $b_month . '-' . $b_date);
$diff = $today->diff($birthdate);
return $name . ' ' . $surname . ' is ' . $diff->format('%y years') . ' old';
}
echo getAge('Mr.', 'Smith', 1990, 12, 12);
輸出:
Mr. Smith is 23 years old
檢查第一2個參數是字符串和三分之二是整數。
我相信這對你不會有問題:)。
謝謝很多,就像你上面的這個,這工作得很好。它給了我一個很好的例子,並開始擺弄和調整一些其他的東西:)並嘗試自己做出後續問題:) – Xereoth 2014-09-10 11:52:04
分離 - 的擔憂,使一個功能calc下歲,並留下弗里斯特/姓氏出來。 - http://stackoverflow.com/questions/3776682/php-calculate-age – Rufinus 2014-09-10 11:15:04
請至少嘗試編寫一些代碼之前尋求幫助的SO。 – 2014-09-10 11:15:10
我寫了一些代碼,但它根本沒有任何意義,所以我想最好不要發佈它,以免造成混淆。 – Xereoth 2014-09-10 11:16:47