2011-12-28 44 views
0

我的網址:重定向到本週

http://www.mysite.com/number/0

這裏是:

$date = new DateTime("2011-11-28"); 
$amountOfDays = ((int)$_GET['number']) * 7; 
echo $date->modify('+'.$amountOfDays.' day'); 

現在我想補充一個選項,星期一設置當前日期。 比如我有:

http://www.mysite.com/check

在這裏我想補充:

$datetime = new DateTime(); 
$number = ?; 
header('Location: http://www.mysite.com/number/' . $number); 

我的網站生成日期:

http://www.mysite.com/number/0 = 2011-11-28 
http://www.mysite.com/number/1 = 2011-12-05 
http://www.mysite.com/number/2 = 2011-12-12 
http://www.mysite.com/number/3 = 2011-12-19 

我想在http://www.mysite.com/check檢查當前日期和重定向t Ø充分http://www.mysite.com/number/x

例如,如果

$datetime = new DateTime(); 
$datime == 2011-12-08; //should be redirect to http://www.mysite.com/number/1 
$datime == 2011-12-05; //should be redirect to http://www.mysite.com/number/1 
$datime == 2011-12-04; //should be redirect to http://www.mysite.com/number/0 
$datime == 2011-12-18; //should be redirect to http://www.mysite.com/number/2 

有日期始終是星期一(在一週的第一天)

+1

我看到「你會喜歡」幾件事情,但我很少無法理解實際的問題。所以讓我回顧一下:你有一個腳本,它從用戶('$ _GET ['number']')獲得一個代表確切的一週的數字,用戶希望看到(或如此)。然後你「想添加一個選項,設置當天的日期」oO? 「與另一個人約會」是什麼意思?然而...你想添加下一個代碼 - 沒有問題,但有一個解析錯誤。然後(隨機)數字/鏈接等於日期..他們遵循哪個規則?其餘的我明白了。 – Matmarbon 2011-12-28 16:20:41

回答

0

這裏是我的方法根據我的建議...

$datetime = new DateTime(); 
$starttime = new DateTime('2011-11-28'); 
$daydifference = $starttime->diff($datetime)->days; 
$numberdifference = $daydifference - $daydifference % 7; 
header('HTTP/1.1 307 Temporary Redirect'); 
header(sprintf('Location: http://www.mysite.com/number/%s', $numberdifference)); 

PS:未測試

+0

其他日期無效 – matino 2011-12-28 15:49:44

+0

@matino哪些「其他日期」? – Matmarbon 2011-12-28 15:51:15

0

我想我明白你想要什麼。

$ref = strtotime('2011-11-28'); 

$today = strtotime('2011-12-19'); 
/* Your selected Monday. */ 

$week = 3600 * 24 * 7; /* Number of seconds in a week. */ 

$number = ($today-$ref)/$week; 
/* You could ceil() or floor() this, depending on how you want it to behave. */