I need to know the first day-of-week in a given time period. The day of the week will change so it could be Sunday, Monday, Tuesday, Wednesday, Thursday, or Friday. For example: I need to find the first Monday between 2011-10-30 and 2011-12-11.
Just a little math with the date('N') (day of the week) value:
$timestamp = strtotime('2011-11-08');
$weekday = date('N', $timestamp);
if ($weekday > 1) {
$timestamp = strtotime('+' . abs(8 - $weekday) . ' days', $timestamp);
}
echo date('D Y-m-d', $timestamp);
Related
Say I wanted to get the week beginning and ending, for example:
Mon 29th June - week start
Sun 5th July - week end
and then tomorrow (Mon 6th July) it will say:
Mon 6th July - week start
Sun 12th July - week end
Is this the right way to do it?
$week_start = date('Y-m-d', strtotime('last monday'));
$week_end = date('Y-m-d', strtotime('this sunday'));
DateTime class has this nice method called setIsoDate():
$start = new DateTime();
$start->setIsoDate($start->format('o'), $start->format('W'));
$end = clone $start;
$end->modify('+6 day');
echo "From: " . $start->format('Y-m-d') . " to: " . $end->format('Y-m-d');
demo
That would not work properly if the current date is Monday. last monday would then translate to the Monday the previous week.
I would use this syntax instead:
$week_start = date('Y-m-d', strtotime('last monday', strtotime('tomorrow')));
$week_end = date('Y-m-d', strtotime('this sunday'));
You might also consider avoiding those "advanced" relative formats, and seek the correct dates based on the current weekday. Might be a bit more reliable, as those week formats don't always seem to behave as one might expect, and the logic behind them isn't readily available.
This solution uses simpler strtotime formats, and instead puts the seek logic in the code itself. As such, it's a lot more predictable, if a little less elegant.
$weekday = date("N"); // 1 = Monday, 7 = Sunday
$week_start = date("Y-m-d", strtotime("-" . ($weekday - 1) . " days"));
$week_end = date("Y-m-d", strtotime("+" . (7 - $weekday) . " days"));
Note that the date("N") flag is only available in PHP 5.1 and later. For older version you'd need to use date("w"), and move the Sunday value to the back.
$weekday = date("w"); // 0 = Sunday, 6 = Saturday
if ($weekday == 0) {
$weekday = 7;
}
Is there a short preset function to find the start (Monday) and end (Sunday) of the week by a given $date ?
I tried:
1)
date("Y-m-d", strtotime('sunday this week ' . $date));
date("Y-m-d", strtotime('monday this week ' . $date));
But this fails when $date is Sunday... it returns the Monday of Next week.
2) also this
date("Y-m-d", strtotime('last monday ' . $date));
date("Y-m-d", strtotime('next sunday ' . $date));
But again if $date is Monday or Sunday it gives the previous/next week.
I know it can be done with few condition .. but I m look for more out of the box solution.
You can use DateTime::format('N') to get the ISO-8601 day of the week (1 = Monday .. 7 = Sunday) and do some simple date arithmetic to get the Monday and the Sunday of the week that contains the specified date (I assume you want the week starting on Monday).
// Today (or any other day you like)
$today = new DateTime('now');
echo('Today: '.$today->format('Y-m-d (D)')."\n");
// Day of week (1 = Monday .. 7 = Sunday)
$dow = $today->format('N');
// Monday is ($dow-1) days in the past
$monday = clone $today;
$monday->sub(new DateInterval('P'.($dow-1).'D'));
echo('Monday: '.$monday->format('Y-m-d')."\n");
// Sunday is 6 days after Monday
$sunday = clone $monday;
$sunday->add(new DateInterval('P6D'));
echo('Sunday: '.$sunday->format('Y-m-d')."\n");
How can I calculate the day of month in PHP with giving month, year, day of week and number of week.
Like, if I have September 2013 and day of week is Friday and number of week is 2, I should get 6. (9/6/2013 is Friday on the 2nd week.)
One way to achieve this is using relative formats for strtotime().
Unfortunately, it's not as straightforward as:
strtotime('Friday of second week of September 2013');
In order for the weeks to work as you mentioned, you need to call strtotime() again with a relative timestamp.
$first_of_month_timestamp = strtotime('first day of September 2013');
$second_week_friday = strtotime('+1 week, Friday', $first_of_month_timestamp);
echo date('Y-m-d', $second_week_friday); // 2013-09-13
Note: Since the first day of the month starts on week one, I've decremented the week accordingly.
I was going to suggest to just use strtotime() in this fashion:
$ts = strtotime('2nd friday of september 2013');
echo date('Y-m-d', $ts), PHP_EOL;
// outputs: 2013-09-13
It seems that this is not how you want the calendar to behave? But it is following a (proper) standard :)
This way its a little longer and obvious but it works.
/* INPUT */
$month = "September";
$year = "2013";
$dayWeek= "Friday";
$week = 2;
$start = strtotime("{$year}/{$month}/1"); //get first day of that month
$result = false;
while(true) { //loop all days of month to find expected day
if(date("w", $start) == $week && date("l", $start) == $dayWeek) {
$result = date("d", $start);
break;
}
$start += 60 * 60 * 24;
}
var_dump($result); // string(2) "06"
i am making some statistics, i want to select the time from (last week only) and this week.
for this week its easy:
$start = strtotime('this week');
$finish = time();
for last week
$start = strtotime('last week');
$finish = ??????
This?
$start = strtotime('2 weeks ago');
$finish = strtotime('last week');
Edit: change credit to #Dominic Barnes's comment.
If the question is for statistical PHP script. Then all of the answers and basically the question is wrong.
Last week in statistics = One before currently running week from Sunday to Monday
This week in statistics = Currently running week from Sunday to Monday
(or Monday to Sunday, depending on which calendar you are used to, but in PHP that's one week)
This means, its not from today minus 7 days. That is not last or this week. So the selected answer currently, is in correct and counts 7 days back. Granted, its Sunday, at the time of testing, so it shows correct. But by editing the now date, you can see the problem:
// Selected answer:
$start = strtotime('2 weeks ago');
$finish = strtotime('last week');
// But if today isn't Sunday, you can see the code is wrong:
echo date("d.m.Y", strtotime("1 week ago", strtotime('yesterday')));
// Output: 15.08.2015 00:00:00 - 17.08.2015 00:00:00
You have to set the start of the week and the end of the week. strtotime() can support more stuff, so you can most likely make this answer better and more neat. However you get the working code and a good example of the logic from...
My proposed solution:
$today = strtotime('today 00:00:00');
$this_week_start = strtotime('-1 week monday 00:00:00');
$this_week_end = strtotime('sunday 23:59:59');
$last_week_start = strtotime('-2 week monday 00:00:00');
$last_week_end = strtotime('-1 week sunday 23:59:59');
echo date('d.m.Y H:i:s', $today) . ' - Today for example purposes<br />';
echo date('d.m.Y H:i:s', $this_week_start) . ' - ' . date('d.m.Y H:i:s', $this_week_end) . ' - Currently running week period<br />';
echo date('d.m.Y H:i:s', $last_week_start) . ' - ' . date('d.m.Y H:i:s', $last_week_end) . ' - Last week period<br />';
Above currently produces:
30.08.2015 00:00:00 - Today for example purposes
24.08.2015 00:00:00 - 30.08.2015 23:59:59 - Currently running week period
17.08.2015 00:00:00 - 23.08.2015 23:59:59 - Last week period
Because for statistics, it has to be accurate and if the end would be 00:00:00, then that date wont be counted in. And if the date would be one day later at 00:00:00, then the date is not correct. There for, this solution is the correct way to do this, for statistical purposes at least.
Is that what you want?
$start = strtotime('last week');
$finish = strtotime('this week');
Dominic also points out that time() === strtotime('this week') (CodePad).
If searching for the last week for statistical purposes, starting on Monday, ending on Sunday:
$last_week_start = strtotime("monday last week");
$last_week_end = strtotime("monday this week - 1 second");
"this week" is important, otherwise, if this weeks monday is already in the past (e.g. if it is tuesday already), it will give you the monday of next' week.
As for the months/years, I used the classy mktime approach:
last month
$last_month_start = mktime(0, 0, 0, date('m')-1, 01);
$last_month_end = mktime(23, 59, 59, date('m'), 0);
last year
$last_year_start = mktime(0, 0, 0, 1, 1, date('Y')-1);
$last_year_end = mktime(23, 59, 59, 1, 0, date('Y'));
If you are looking for "Last Week" instead of Last 7 days
$start = strtotime('Last Week'); // Will give you last Monday
$finish = strtotime('Last Sunday'); // Will give you last Sunday
Does PHP calculate weeks as being Sunday - Saturday? For a given date I am trying to determine the beginning and ending date of it's week as well as the beginning date of the next/previous weeks. Everything works fine unless I pass in a Sunday and it thinks the date is in a previous week.
$start = $_GET['start'];
$year = date('Y', strtotime($start));
$week = date('W', strtotime($start));
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
echo '<p>week: ' . $week . '</p>';
echo '<p>sunday: ' . date('Y-m-d', $sunday) . '</p>';
echo '<p>next:' . date('Y-m-d', $next) . '</p>';
echo '<p>prev: ' . date('Y-m-d', $prev) . '</p>';
Outcome:
2011-01-09 (Sunday)
Week: 01
WRONG
2011-01-10 (Monday)
Week: 02
RIGHT
2011-01-15 (Saturday)
Week: 02
RIGHT
PHP doesn't think about weeks at all, if you're getting the wrong results, it's because your math is off. :)
$date = strtotime('2011-1-14');
$startingSunday = strtotime('-' . date('w', $date) . ' days', $date);
$previousSaturday = strtotime('-1 day', $startingSunday);
$nextWeekSunday = strtotime('+7 days', $startingSunday);
As Dr.Molle point out, the information about "W" is correct. Your problem is here:
$sunday = strtotime($year.'W'.$week.'0');
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
Then you called strtotime on a Timestamp object (sorry, I don't know the exact term).
The wrong type of parameter (timestamp and string are used not correctly) is the cause of the problem. Here's my piece of code to determine the week and the beginning day of the week:
<?php
$date = '2011/09/09';
while (date('w', strtotime($date)) != 1) {
$tmp = strtotime('-1 day', strtotime($date));
$date = date('Y-m-d', $tmp);
}
$week = date('W', strtotime($date));
echo '<p>week: ' . $week . '</p>';
?>
To fully understand, you should take a look on date & strtotime manual.
As defined in ISO_8601, what date('W') refers to, a week starts with monday.
But be careful and read about the ISO-week: http://en.wikipedia.org/wiki/ISO_week_date
Maybe the result is not always like expected.
example:
date('W',mktime(0, 0, 0, 1, 1, 2011))
It will return 52 instead of 01, because the first ISO-week of a year is the first week with at least 4 days in the given year.
As 2011-1-1 was a saturday, there are only 2 days, so 2011-1-1 is in ISO in the last week of 2010(52) and not in the first week of 2011.
The function date('W') uses the ISO-8601 definition, therefore Monday is the first day of the week.
In place of date('W') use strftime('%U').
Example:
$date = strtotime('2011-01-09');
echo strftime('%U',$date);
Outcome:
02
The code:
$date = strtotime('2012-05-06');
$sunday = date('Y-m-d', strtotime(strftime("%Y-W%U-0", $date)));
$sturday = date('Y-m-d', strtotime(strftime("%Y-W%U-6", $date)));
echo $sunday . "\n";
echo $saturday;
Outcome:
2012-05-06
2012-05-12