在给定日期的MM-dd-yyyy
格式下,有人能帮我获取本周第一天吗?
这里是我正在使用的代码...
$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));
$day 包含一个数字,范围从0到6,代表一周中的某一天(星期日=0,星期一=1等)。
$week_start 包含本周星期日的日期,格式为mm-dd-yyyy。
$week_end 包含本周星期六的日期,格式为mm-dd-yyyy。
date("N")
也可以使用。根据https://www.php.net/manual/en/datetime.format.php,`N`是“星期几的ISO 8601数字表示”。只需要将其递减后即可在此代码中使用。 - Cave Johnson使用strtotime
函数非常简单:
echo date("Y-m-d", strtotime('monday this week')), "\n";
echo date("Y-m-d", strtotime('sunday this week')), "\n";
这在不同的PHP版本中有所不同 :
2015-03-16
2015-03-22
2015-03-23
2015-03-22
2015-03-30
2015-03-29
本周等相对描述词有其特定的上下文。以下是在周一或周日时,对本周周一和周日的输出展示:
$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
但是它在不同的PHP版本中有所不同:
2015-03-16
2015-03-22
2015-03-23
2015-03-29
2015-03-16
2015-03-22
2015-03-23
2015-03-22
2015-03-23
2015-03-22
2015-03-23
2015-03-29
2015-03-23
2015-03-29
2015-03-30
2015-03-29
strtotime('this week', time());
替换time()。当当前日期是周日/周一时,下一个周日/上一个周一的方法将无法使用。
$monday = strtotime('last monday', strtotime('tomorrow'));
可以翻译为:将“明天”这个时间戳作为参数,计算“上周一”的时间戳,并将结果赋值给变量 $monday
。 - Lewis Buckleytime()
是可选的。 - Ifnotstrtotime('last Monday tomorrow',$intTimestamp)
。 - Eugene C<?php
$dateTime = new \DateTime('2020-04-01');
$monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dateTime->modify('Sunday this week');
?>
来源:PHP手册
注意:由于某些用户的评论,$dateTime的值将被修改。
DateTime
类,这意味着即使当前命名空间中有另一个叫做 DateTime
的类,代码也能正常工作。 - Spudley$givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy));
这将给出所提供日期本身的星期几,其中0
表示星期日,6
表示星期六。从那里,您可以简单地向后计算到您想要的日期。
这个问题需要一个好的DateTime答案:
function firstDayOfWeek($date)
{
$day = DateTime::createFromFormat('m-d-Y', $date);
$day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1);
return $day->format('m-d-Y');
}
var_dump(firstDayOfWeek('06-13-2013'));
输出:-string '06-10-2013' (length=10)
这将处理年份边界和闰年。
编辑:以下链接不再在所述版本的PHP上运行。它正在运行PHP 5.6,改进了strtotime的可靠性,但并不完美!表格中的结果是来自PHP 5.6的实时结果。
值得一提的是,以下是strtotime确定一致参考帧时的不稳定行为的详细说明:
http://gamereplays.org/reference/strtotime.php
基本上,只有这些字符串可以可靠地给出相同的日期,无论您在调用它们时所处的星期几:
strtotime("next monday");
strtotime("this sunday");
strtotime("last sunday");
假设一周的第一天为星期一,则此代码可行:
echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));
以下代码适用于任何自定义日期,只需使用所需的日期格式即可。
$custom_date = strtotime( date('d-m-Y', strtotime('31-07-2012')) );
$week_start = date('d-m-Y', strtotime('this week last monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week next sunday', $custom_date));
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;
我用 PHP 5.2.17 测试了这段代码,结果如下:
Start: 30-07-2012
End: 05-08-2012
$first_day_of_week = date('m-d-Y', strtotime('Last Monday', time()));
$last_day_of_week = date('m-d-Y', strtotime('Next Sunday', time()));
DateTime
解决方案,而大多数回答并未提供它。 =) - lin