start and end of the week by given date - php

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

Related

PHP: how to get a day in last week?

Is there an easy way to get last week's, say, Monday? If today is Tuesday, I do not want yesterday's Monday. Rather, I want the Monday 8 days ago (last week's Monday). Then I want that Monday's proceeding Sunday. Basically, I'm trying to get the date range for last week, Monday to Sunday.
This doesn't always work right:
date('Y-m-d', strtotime('last Monday')
Suggestions?
You can use “this week” format:
$monday = strtotime( 'this week', strtotime( '7 days ago' ) );
$sunday = strtotime( '+ 6 days', $monday );
3v4l.org demo
“this_week” returns monday of previous week, then — adding 6 days — you obtain the monday of relative week.
The strtotime function accepts the current date as a parameter.
http://php.net/manual/en/function.strtotime.php
Just pass in strtotime('last Sunday') as the parameter to get a weekday of the last full week.
$beginning_of_week = strtotime('last Sunday');
$result = date('Y-m-d', strtotime('last Monday', $beginning_of_week));
echo $result;

Convert Day to Next Available Date in PHP

how can I convert the day to the near future date in php?
Like if I enter Monday and if today is Friday 22/08/2014 it should convert Monday to 25/08/2014 ?
use the php function strtotime and the key next week monday to get the next date
http://de2.php.net/manual/de/function.strtotime.php
$theDay = 'Monday'; // eg. Monday
$time = strtotime('next week ' . $theDay);
echo date('d.m.y', $time); // get next monday

PHP Find the first day-of-week in a given time period

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

last week, this week (php)

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

Finding First day of week via php [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Get first day of week in PHP?
Hi,
I want to find first and last date of current week and last week.
Similarly I want to find first and last date of current month and last month.
This has to be done in PHP. Please help.
strtotime is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Here's a function for the first and last day of the week:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
It can probably be adapted to do month as well, but I wanted to get my answer in! :)

Categories