2016-11-09 41 views
1

我是一名當前註冊PHP課程的學生,我們的教師已經告訴我們要創建可以接受用戶生日的代碼,然後計算從今天起有多少天是他們下一個生日。我完全理解如何計算兩個日期之間的時間,但由於我只需要用戶生日之前的天數,所以它變得棘手。我已經搜索了這個問題,但所有的答案似乎只是計算兩年之間的時間。我需要計算直到用戶下一個生日的時間

例如:
輸入:1985年11月14日
今天的日期是2016年11月9日
輸出(應該是): 「你有5天,直到你的下一個生日」

THIS IS MY編輯的代碼:

<?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

有一個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'); 
?> 
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"; 
} 
+0

所以我編輯爲你指示的代碼,但是當我運行此程序計算的天數是有點過。所以如果生日真的是30天以後,該計劃可能會讓我有33天的時間。 – Veronica

+0

@TiffanyIwinyoulose檢查我的編輯,我猜你錯過了第二個'switch'語句中的'$ jake_bday' :) –

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'); 
相關問題