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

时间:2022-08-25 10:22:49

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日至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?

对此有何看法,伙计们?

6 个解决方案

#1


12  

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

#2


25  

 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 Warning if default time zone is not set in php.ini.

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

#3


8  

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>";

#4


5  

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...

希望能帮助到你...

#5


4  

Check out the getdate function, 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函数,有一些如何在我链接的手册页上使用它的示例。我想它会归还你想要的一切,

#6


3  

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 ";
?>

#1


12  

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

#2


25  

 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 Warning if default time zone is not set in php.ini.

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

#3


8  

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>";

#4


5  

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...

希望能帮助到你...

#5


4  

Check out the getdate function, 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函数,有一些如何在我链接的手册页上使用它的示例。我想它会归还你想要的一切,

#6


3  

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 ";
?>