开发者

Finding date range for current week, month and year

开发者 https://www.devze.com 2023-02-22 18:28 出处:网络
Suppose I have a date ava开发者_如何学Pythonilable with me: 2011-04-05 (i.e., 5th April, 2011) I want to find date range for Current Week, Month and Year

Suppose I have a date ava开发者_如何学Pythonilable 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?

Edit:

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

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

Any thoughts on this, guys?


 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()

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

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


you can use 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


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 =)

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


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


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,


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


Today is Tuesday, August 27th 2019

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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消