重定向到当前周

时间:2011-12-28 15:12:17

标签: php datetime

我有网址:

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中检查当前日期并重定向到足够的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

日期总是星期一(星期一天)

2 个答案:

答案 0 :(得分: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:未经测试

答案 1 :(得分: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. */