Get the Date of Next Monday, Tuesday, etc

Get the date of next monday, tuesday, etc

See strtotime()

strtotime('next tuesday');

You could probably find out if you have gone past that day by looking at the week number:

$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);

if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}

Easiest way to get next monday from current day?

okay use this,

$nextMonday = strtotime('next monday');

Get previous and next monday date by given date


echo "Next Monday:". date('Y-m-d', strtotime('next monday', strtotime($givenDate)));
echo "Previous Monday:". date('Y-m-d', strtotime('previous monday', strtotime($givenDate)));

SQL: get next relative day of week. (Next Monday, Tuesday, Wed.....)

1) Your solution uses a non-deterministic function: datepart(dw...) . Because of this aspect, changing DATEFIRST setting will gives different results. For example, you should try:

SET DATEFIRST 7;
your solution;

and then

SET DATEFIRST 1;
your solution;

2) Following solution is independent of DATEFIRST/LANGUAGE settings:

DECLARE @NextDayID INT  = 0 -- 0=Mon, 1=Tue, 2 = Wed, ..., 5=Sat, 6=Sun
SELECT DATEADD(DAY, (DATEDIFF(DAY, @NextDayID, GETDATE()) / 7) * 7 + 7, @NextDayID) AS NextDay

Result:

NextDay
-----------------------
2013-09-23 00:00:00.000

This solution is based on following property of DATETIME type:

  • Day 0 = 19000101 = Mon

  • Day 1 = 19000102 = Tue

  • Day 2 = 19000103 = Wed

...

  • Day 5 = 19000106 = Sat

  • Day 6 = 19000107 = Sun

So, converting INT value 0 to DATETIME gives 19000101.

If you want to find the next Wednesday then you should start from day 2 (19000103/Wed), compute days between day 2 and current day (20130921; 41534 days), divide by 7 (in order to get number of full weeks; 5933 weeks), multiple by 7 (41531 fays; in order to get the number of days - full weeks between the first Wednesday/19000103 and the last Wednesday) and then add 7 days (one week; 41538 days; in order to get following Wednesday). Add this number (41538 days) to the starting date: 19000103.

Note: my current date is 20130921.

Edit #1:

DECLARE @NextDayID INT;
SET @NextDayID = 1; -- Next Sunday
SELECT DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), GETDATE()) / 7) * 7 + 7, ((@NextDayID + 5) % 7)) AS NextDay

Result:

NextDay
-----------------------
2013-09-29 00:00:00.000

Note: my current date is 20130923.

Getting the date of next Monday

to fix if today is Monday then sun 7
This will do:

var d = new Date();
d.setDate(d.getDate() + (((1 + 7 - d.getDay()) % 7) || 7));
console.log(d);

How could I get the the Nth Monday, Tuesday, etc. of every month between two dates using JavaScript

To get the nth instance of a day in the month, create a date for the first of the month, move to the first instance of the required day, then add (n-1) weeks worth of days, e.g.





/* Get nth instance of a particular weekday in a month
*
* @param {number|string} nth - instance of day, 1 to 4
* @param {number|string} day - day of week, Sun 0, Mon 1, etc.
* @param {Date} month - any date in month to get day of
* @returns {Date} that is nth instance of day in month
*/
function getNthDayInMonth(nth, day, month) {
// Create new date for 1st of month
let d = new Date(month.getFullYear(), month.getMonth());
// Move to first instance of day in month and
// add (n - 1) weeks
d.setDate(1 + (7 - d.getDay() + day)%7 + (nth - 1)*7);
return d;
}

// Formater
let f = (d) => new Intl.DateTimeFormat('en-GB', {
weekday:'short',
day:'2-digit',
month:'short',
year: 'numeric'
}).format(d);

// Examples
[[1,1,new Date(2021,11),'1st Mon in Dec 2021'],
[3,3,new Date(2022, 0),'3rd Wed in Jan 2022'],
[4,1,new Date(2022, 1),'4th Mon in Feb 2022'],
[4,2,new Date(2022, 1),'4th Tue in Feb 2022'],
[4,3,new Date(2022, 1),'4th Wed in Feb 2022'],
[1,3,new Date(2022, 5),'1st Wed in Jun 2022'],
[5,5,new Date(2022,11),'5th Fri in Dec 2022']
].forEach(a => {
let [n,d,m,note] = a;
console.log(`${note}: ${f(getNthDayInMonth(n,d,m))}`);
});

How to get the next Monday using PHP strtotime?

You have an error here strftime('%B',time()), actualy it should be strftime('%B',strtotime("next monday")).

<?php

$next_monday_timestamp = strtotime("next monday");
echo "Next monday will be on " . strftime("%B", $next_monday_timestamp);

PHP Date&Time: Get next tuesday with 7 days between

You could use a minimum datetime instance to compare against.

$begin = new DateTime('next tuesday');
$min = new DateTime('+7 days');
$interval = new DateInterval( 'P1W' );

if ($begin < $min) {
$begin->add($interval);
}

However, if you always want two tuesdays from now, this simple change might work:

$begin = new DateTime('next tuesday +7 days');

Show date of next thursday

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;


Related Topics



Leave a reply



Submit