我需要计算到用户下一个生日的时间


I need to calculate the time until a users next birthday

我是一名目前正在学习PHP课程的学生,我们的老师告诉我们要创建代码来接受用户的生日,然后计算从今天起多少天是他们的下一个生日。我完全理解如何计算两个日期之间的时间,但由于我只想要用户生日之前的天数,这就变得棘手了。我一直在寻找这个问题,但所有的答案似乎都只是计算年份之间的时间。

例子:
输入:11/14/1985
今天的日期是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";
}



?>

事先感谢您的协助蒂芙尼(Tiffany)

您可以使用日期时间。

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');

如果您使用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";
}

有一个PHP函数叫做date_diff。下面是一个直接来自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');
?>