2014-09-10 116 views
-7

我有问题。 我想弄清楚这个问题的答案,但我似乎无法包围我的思想。 希望有人能帮助我。 现在的问题是:PHP日期问题

写一个函数,将某人的名字,姓氏,出生年份,月份和日期作为参数,并给出全名+年龄。确保应用程序/程序在2015年或以后也可以提供正确的结果。检查前两个参数是否是字符串,三分之一是否是整数。

由于我是PHP的新手,并没有约会日期(我GOOGLE了,但我无法让我的心灵缠绕它) 在此先感谢! :) (写了一些代码,自己去尝试,但它是无用的,所以我离开它了)

+0

分离 - 的担忧,使一个功能calc下岁,并留下弗里斯特/姓氏出来。 - http://stackoverflow.com/questions/3776682/php-calculate-age – Rufinus 2014-09-10 11:15:04

+2

请至少尝试编写一些代码之前寻求帮助的SO。 – 2014-09-10 11:15:10

+0

我写了一些代码,但它根本没有任何意义,所以我想最好不要发布它,以免造成混淆。 – Xereoth 2014-09-10 11:16:47

回答

0

下面是一个简单的函数来做到这一点

//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'); 
+0

很酷,这似乎做我想要的东西 我怎么不明白代码真的Date_create是一个PHP的构建功能和date_diff我也假设?(我会尽力谷歌了解更多信息,以更好地了解它!只是真的需要学习如何用代码^ _^ – Xereoth 2014-09-10 11:23:47

1
<?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; 
} 
?> 
+0

Thansk为你的答案,我怎么会猜测它从我的尝试。认为它太复杂了,我实际上需要:) – Xereoth 2014-09-10 11:25:55

+0

只要做得很快,显然有更好的方法,你也想检查变量,以确保它们是一个int而不是一个字符串等等 – 2014-09-10 11:29:49

0

使用此功能

<?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'; 
    ?> 
-1

为什么不创建一个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 
+1

“编写一个函数”,他们可能还没有得到面向对象的编程,所以如果你要做他的功课,那么做一个函数。 – 2014-09-10 11:28:22

+0

这是一个很好的练习案例来熟悉面向对象。但他没有要求。那是因为他可能不知道它存在。所以我们不要告诉他。或者让我们......我只是认为我会分享大多数程序员会解决这个问题的方式。 – ODaniel 2014-09-10 11:38:31

+0

大声笑,这不是我的家庭作业,它学习如何使用日期等练习,但我不明白任何它,所以我宁愿问帮助,然后拨弄它来弄清楚它是如何工作的:)但无论如何感谢 – Xereoth 2014-09-10 11:40:36

0

您可以使用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 

Live preview


检查第一2个参数是字符串和三分之二是整数。

我相信这对你不会有问题:)。

有用的资源:

+1

谢谢很多,就像你上面的这个,这工作得很好。它给了我一个很好的例子,并开始摆弄和调整一些其他的东西:)并尝试自己做出后续问题:) – Xereoth 2014-09-10 11:52:04