This question already has answers here:
Get the date of next monday, tuesday, etc
(10 answers)
Closed 11 months ago.
How we can get previous week monday date and next week monday date by provided date.Eample: if $date = '2015-04-08' (y-m-d format).
then function returns, previous monday date = 2015-03-30
and next monday date = 2015-04-13
echo "Next Monday:". date('Y-m-d', strtotime('next monday', strtotime($givenDate)));
echo "Previous Monday:". date('Y-m-d', strtotime('previous monday', strtotime($givenDate)));
you can use
date('Y-m-d', strtotime('last week Monday'));
date('Y-m-d', strtotime('next week Monday'));
or whatever you want http://php.net/manual/en/datetime.formats.relative.php
You can use this:
$nextMonday = new \DateTime('2015-04-08 next Monday');
$previousMonday = new \DateTime('2015-04-08 Monday ago');
Related
This question already has answers here:
how to get the date of last week's (tuesday or any other day) in php?
(8 answers)
Closed 10 months ago.
I am trying to work out how to use php dates to calculate and echo the following:
-Get the date and day today ($dateToday = date("Y-m-d"); $dayToday = date("D");)
-Use the date and day today to calculate what the date was last Friday and Saturday.
For example:
If today is Tuesday the 3rd of May, we should echo out Saturday 30th of April and Friday 29th of April.
If today is Wednesday the 11th of May, we should echo out Saturday 7th of May and Friday the 6th of May.
Thanks in advance!
You can get last Friday/Saturday by using:
date('Y-m-d', strtotime("last friday"));
date('Y-m-d', strtotime("last satruday"));
It will always return the last friday/saturday compared to today's date.
Solution:
echo date('Y-m-d', strtotime("last friday")) . "," . date("D", strtotime("last friday"));
echo "<br>";
echo date('Y-m-d', strtotime("last saturday")) . "," . date("D", strtotime("last saturday"));
This question already has answers here:
Get the date of next monday, tuesday, etc
(10 answers)
Closed 11 months ago.
I use the following code to display the date of every next thursday.
Now I got the following problem: today is thursday, but I don't want to show the date of the next week until the current day is over. How is that possible?
Every thursday we have duty from 7pm to 10pm. Therefore I need to display todays date for example. After 10pm I can display the date of the next thursday (for example 30.11.)
Current code:
$timestmp = strtotime('next thursday');
setlocale(LC_TIME, "de_DE");
$next = strftime('%A, %d.%m.%Y', $timestmp);
You can have a simple if condition to check if today is Thursday, print today's date. Else, print next Thursday
setlocale(LC_TIME, "de_DE");
if (date('l') == 'Thursday') {
$thu = strftime('%A, %d.%m.%Y');
} else {
$timestmp = strtotime('next thursday');
$thu = strftime('%A, %d.%m.%Y', $timestmp);
}
echo $thu;
if(date('l') == 'Thursday') {
echo 'Today is Thursday! Whola!'
}
else {
$timestmp = strtotime('next thursday');
setlocale(LC_TIME, "de_DE");
$next = strftime('%A, %d.%m.%Y', $timestmp);
}
So you want show today this thursday?
Then pass to strtotime, yesterday's timestamp. Like that
$timestmp = strtotime('next thursday', strtotime('-1 day'));
Then it will seek for next thursday from yesterday, which is today.
The following code grabs the next thursday if its sunday, monday, tuesday or wednesday. It grabs the last thursday if it's firday or saturday. And it will grab the current day if it's thursday today.
$now = new DateTime();
if ($now->format('w') < 4) {
$now->modify('next thursday')
} elseif ($now->format('w') > 4) {
$now->modify('last thursday')
}
echo $now->format('Y-m-d');
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;
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");
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! :)