在 PHP 4 中获取本周星期一的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2958327/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get date of Monday in current week in PHP 4
提问by Ben Jones
I need to find the date of Monday in the current week. How can I do this in PHP 4?
我需要在本周中找到星期一的日期。我怎样才能在 PHP 4 中做到这一点?
回答by Davidcologne
Easisest way:
最简单的方法:
$time = strtotime('monday this week');
回答by nickf
Try this:
尝试这个:
return strtotime('last monday', strtotime('next sunday'));
回答by apis17
echo date('Y-m-d',time()+( 1 - date('w'))*24*3600);
For next week:
下周:
echo date('Y-m-d',time()+( 8 - date('w'))*24*3600);
1 for Monday, 2 Tuesday, 3 Wednesday and so on. Have a try.
1 代表星期一,2 星期二,3 星期三等等。试试。
回答by mr.b
echo date('Y-m-d', strtotime('previous monday'));
Just one note, though. You want to make sure that it's not monday today, otherwise you will get date of previousmonday, just like it says. You could do it as follows
不过,只有一个注释。你要确保今天不是星期一,否则你会得到上一个星期一的日期,就像它说的那样。你可以这样做
if (date('w') == 1)
{
    // today is monday
}
else
{
    // find last monday
}
回答by Rob
$thisMonday = date('l, F d, Y', time() - ((date('w')-1) * 86400) );
$thisMonday = date('l, F d, Y', time() - ((date('w')-1) * 86400) );
Edit: explanation
编辑:解释
- date('w')is a numeric representation of the day of the week (0=sunday, 6=saturday)
- there are 86400 seconds in a day
- we take the current time, and subtract (one day * (day of the week - 1))
- date('w')是星期几的数字表示(0=星期日,6=星期六)
- 一天有 86400 秒
- 我们取当前时间,然后减去 (一天 * (星期几 - 1))
So, if it is currently wednesday (day 3), monday is two days ago:
因此,如果当前是星期三(第 3 天),则星期一是两天前:
time() - (86400 * (3 - 1))= time() - 86400 * 2
time() - (86400 * (3 - 1))= time() - 86400 * 2
If it is monday (day 1), we get:
如果是星期一(第 1 天),我们得到:
time() - (86400 * (1 - 1))= time() - 86400 * 0= time()
time() - (86400 * (1 - 1))= time() - 86400 * 0=time()
If it is sunday (day 0), monday is tomorrow.
如果是星期日(第 0 天),则星期一是明天。
time() - (86400 * (0 - 1))= time() - -86400= time() + 86400
time() - (86400 * (0 - 1))= time() - -86400=time() + 86400
回答by mr.b
Try this.
尝试这个。
echo date('Y-m-d', strtotime('last monday', strtotime('next monday')));
It will return current date if today is monday, and will return last monday otherwise. At least it does on my PHP 5.2.4 under en_US locale.
如果今天是星期一,它将返回当前日期,否则将返回上一个星期一。至少在我的 PHP 5.2.4 en_US 语言环境下是这样。
回答by Okochea
Try this
尝试这个
$day_of_week = date("N") - 1; 
$monday_time = strtotime("-$day_of_week days");
回答by Greg Reynolds
My attempt:
我的尝试:
<?php
$weekday = date("w") - 1;
if ($weekday < 0)
{
    $weekday += 7;
}
echo "Monday this week : ", date("Y-m-d",time() - $weekday * 86400) , "\n";
?>

