我是一名目前正在注册PHP课程的学生,我们的讲师告诉我们创建接受用户生日的代码,然后计算从今天开始的下一个生日的天数。我完全理解如何计算两个日期之间的时间,但由于我只想要用户生日之前的天数,所以它变得棘手。我已经搜索过这个问题,但所有的答案似乎只计算了几年之间的时间。
示例:
输入:1987年11月14日
今天的日期是11/9/2016
输出(应该是):“你有5天的时间直到你的下一个生日”
这是我的编辑代码:
<?php
$johnsBirthday = $_GET ['JohnBday'];
$jakesBirthday = $_GET ['JakeBday'];
$john_bday = new DateTime($_GET['JohnBday']);
$jake_bday = new DateTime($_GET['JakeBday']);
$today_date = new DateTime();
switch (true) {
case ($john_bday < $today_date) :
$today_date->setDate($john_bday->format('Y'), $today_date- >format('m'), $today_date->format('d'));
break;
case ($today_date < $john_bday) :
$john_bday->setDate($today_date->format('Y'), $john_bday->format('m'), $john_bday->format('d'));
break;
}
switch (true) {
case ($today_date < $jake_bday) :
$jake_bday->setDate($today_date->format('Y'), $jake_bday->format('m'), $jake_bday->format('d'));
break;
case ($jake_bday < $today_date) :
$jake_bday->setDate($today_date->format('Y'), $jake_bday->format('m'), $jake_bday->format('d'));
break;
}
$john_interval = $john_bday->diff($today_date);
$john_diff = $john_interval->format('%a');
echo "John you have $john_diff days until your next Birthday</br>";
$jake_interval = $jake_bday->diff($today_date);
$jake_diff = $jake_interval->format('%a');
echo "Jake you have $jake_diff days until your next Birthday</br>";
if ($johnsBirthday < $jakesBirthday)
{
echo "John is older than Jake</br>";
}
elseif ($johnsBirthday > $jakesBirthday)
{
echo "Jake is older than John</br>";
}
else
{
echo "Jake and John are twins";
}
?>
感谢您提前协助, 蒂芙尼
答案 0 :(得分:1)
如果您使用PHP 5.3 >
,这应该适用于您的方案,这是计算差异的最准确方法之一。我已正确使用变量名称以便您清楚地理解。
$input_date = new DateTime('1985-11-14');
$today_date = new DateTime();
switch (true) {
case ($input_date < $today_date) :
$today_date->setDate($input_date->format('Y'), $today_date->format('m'), $today_date->format('d'));
break;
case ($today_date < $input_date) :
$input_date->setDate($today_date->format('Y'), $input_date->format('m'), $input_date->format('d'));
break;
}
$interval = $input_date->diff($today_date);
$diff = $interval->format('%a');
$output = "You have $diff days until your next Birthday";
echo $output;
修改:这应该是您的代码
$johnsBirthday = '1985-11-15';
$jakesBirthday = '2986-11-30';
$john_bday = new DateTime($johnsBirthday);
$jake_bday = new DateTime($jakesBirthday);
$today_date = new DateTime();
switch (true) {
case ($john_bday < $today_date) :
$today_date->setDate($john_bday->format('Y'), $today_date->format('m'), $today_date->format('d'));
break;
case ($today_date < $john_bday) :
$john_bday->setDate($today_date->format('Y'), $john_bday->format('m'), $john_bday->format('d'));
break;
}
switch (true) {
case ($today_date < $jake_bday) :
$jake_bday->setDate($today_date->format('Y'), $jake_bday->format('m'),$jake_bday->format('d'));
break;
case ($jake_bday < $today_date) :
$today_date->setDate($jake_bday->format('Y'), $jake_bday->format('m'), $jake_bday->format('d'));
break;
}
$john_interval = $john_bday->diff($today_date);
$john_diff = $john_interval->format('%a');
echo "John you have $john_diff days until your next Birthday</br>";
$jake_interval = $jake_bday->diff($today_date);
$jake_diff = $jake_interval->format('%a');
echo "Jake you have $jake_diff days until your next Birthday</br>";
if ($johnsBirthday < $jakesBirthday)
{
echo "John is older than Jake</br>";
}
elseif ($johnsBirthday > $jakesBirthday)
{
echo "Jake is older than John</br>";
}
else
{
echo "Jake and John are twins";
}
答案 1 :(得分:1)
您可以使用日期时间执行此操作。
http://be2.php.net/manual/en/class.datetime.php
// create the birthday and a copy to add a year to for the next
$datetime1 = new DateTime($_GET['JohnBday']);
$datetime2 = new DateTime($_GET['JohnBday']);
date_add($datetime2, date_interval_create_from_date_string('1 year'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
答案 2 :(得分:0)
有一个名为date_diff的PHP函数。以下是PHP手册中的一个示例..
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>