php 查找当前周、月和年的日期范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5552862/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:47:27  来源:igfitidea点击:

Finding date range for current week, month and year

php

提问by I-M-JM

Suppose I have a date available with me:

假设我有一个可用的约会对象:

2011-04-05 (i.e., 5th April, 2011)

I want to find date range for Current Week, Month and Year

我想查找当前周、月和年的日期范围

Current Week:  3rd April to 9th April
Current Month: 1st April to 30 April
Current Year:  1st Jan to 31 Dec

I do understand that current year would be always 1st Jan to 31Dec, but what about current month and week, How can I find it?

我知道今年总是 1 月 1 日到 12 月 31 日,但是当月和当前周呢,我怎么能找到呢?

Edit:

编辑:

How can I find date, which is 10 days earlier or later from a given date. Example:

如何找到比给定日期早或晚 10 天的日期。例子:

Suppose today's date is 6th April, 2011 10 day's earlier: 28 March, 2011 10 day's later: 15 April, 2011

假设今天的日期是 2011 年 4 月 6 日 10 天前:2011 年 3 月 28 日 10 天后:2011 年 4 月 15 日

Any thoughts on this, guys?

对此,各位有什么想法吗?

采纳答案by Tufan Bar?? Y?ld?r?m

you can use strtotime

你可以使用strtotime

example :

例子 :

    date('d.m.Y',strtotime('last day of this month')) 
date('d.m.Y',strtotime('last monday'))  // for first day of this week

回答by Wh1T3h4Ck5

 function rangeMonth ($datestr) {
   date_default_timezone_set (date_default_timezone_get());
   $dt = strtotime ($datestr);
   return array (
     "start" => date ('Y-m-d', strtotime ('first day of this month', $dt)),
     "end" => date ('Y-m-d', strtotime ('last day of this month', $dt))
   );
 }

 function rangeWeek ($datestr) {
   date_default_timezone_set (date_default_timezone_get());
   $dt = strtotime ($datestr);
   return array (
     "start" => date ('N', $dt) == 1 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('last monday', $dt)),
     "end" => date('N', $dt) == 7 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('next sunday', $dt))
   );
 }

 print_r (rangeMonth('2011-4-5')); // format: YYYY-M-D
 print_r (rangeWeek('2011-4-5'));

output for rangeMonth()

输出 rangeMonth()

Array
(
    [start] => 2011-04-01
    [end] => 2011-04-30
)

output for rangeWeek()

输出 rangeWeek()

Array
(
    [start] => 2011-04-04
    [end] => 2011-04-08
)

Notice: functions like getdate(), date(), etc. throw Warningif default time zone is not set in php.ini.

注意:getdate()、date() 等函数如果在 php.ini 中没有设置默认时区,则会抛出警告

回答by Dr Yunke

This solution is simple, and it takes in consideration when the current day is Monday or Sunday.

这个解决方案很简单,它考虑了当前日期是星期一还是星期日。

NOTE: Using strtotime('last monday') may work if the day is other than Monday, otherwise it will return the previous Monday. Because of that we should use strtotime('last monday', strtotime('tomorrow')) and it will work for any day of the current week =)

注意:如果当天不是星期一,则使用 strtotime('last monday') 可能会起作用,否则它将返回上一个星期一。因此,我们应该使用 strtotime('last monday', strtotime('tomorrow')) 并且它适用于本周的任何一天 =)

Solution:

解决方案:

$monday = strtotime('last monday', strtotime('tomorrow'));
$sunday = strtotime('+6 days', $monday);
echo "<P>". date('d-M-Y', $monday) . " to " . date('d-M-Y', $sunday) . "</P>";

回答by enam

The following code will give you the start and last date of a week:

以下代码将为您提供一周的开始日期和最后日期:

   $today = getdate();
   print_r($today);
   echo "<br/>";

   $weekStartDate = $today['mday'] - $today['wday'];
   $weekEndDate = $today['mday'] - $today['wday']+6;
   echo "<br/>";
   echo "<br/>";
   echo "week start date:".$weekStartDate;
   echo "<br/>";
   echo "week end date:".$weekEndDate;

Hope it helps...

希望能帮助到你...

回答by Erik

Check out the getdatefunction, there are a few examples of how to use it on the manual page I linked. I think it will return everything you're looking for,

查看getdate函数,在我链接的手册页上有一些如何使用它的示例。我认为它会返回你正在寻找的一切,

回答by Kavvson Empcraft

Working example it properly handles the Monday issue

工作示例它正确处理星期一问题

<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");

$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);

echo "Current week range from $this_week_sd to $this_week_ed ";
?>

回答by Ruth

Today is Tuesday, August 27th 2019

今天是 2019 年 8 月 27 日,星期二

This week is from Monday, August 26th 2019 to Sunday, September 1st 2019

本周为2019年8月26日星期一至2019年9月1日星期日

php > $date = new DateTime('Sunday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-01 00:00:00

php > $date = new DateTime('Tuesday');
php > echo $date->format('Y-m-d H:i:s');
2019-08-27 00:00:00

php > $date = new DateTime('Monday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-02 00:00:00

php > $date = new DateTime('Monday this week');
php > echo $date->format('Y-m-d H:i:s');
2019-08-26 00:00:00