Closest day of week to specific dateGet the closest date of a specific week dayHow to print a date in a regular format?Convert HTML + CSS to PDF with PHP?Compare two dates with JavaScriptAdd days to JavaScript DateDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript dateHow do I check if a string contains a specific word?Get first and last date of current month with JavaScript or jQuery

Permutations in Disguise

How to give my students a straightedge instead of a ruler

What does "boys rule, girls drool" mean?

What explanation do proponents of a Scotland-NI bridge give for it breaking Brexit impasse?

Output a Super Mario Image

Is there any benefit to riders on the front of a paceline?

Ambiguity in notation resolved by +

How can I discourage sharing internal API keys within a company?

Why is the Digital 0 not 0V in computer systems?

What exactly is a marshrutka (маршрутка)?

What is the name of this Allen-head furniture fastener?

Is は a particle in こんにちは and こんばんは?

Can I see Harvest moon in India?

Some Prime Peerage

What's the benefit of prohibiting the use of techniques/language constructs that have not been taught?

Are there any “Third Order” acronyms used in space exploration?

Why is the year in this ISO timestamp not 2019?

Can I fix my boots by gluing the soles back on?

Reading double values from a text file

How do I say "quirky" in German without sounding derogatory?

Bash awk command with quotes

Why is my fire extinguisher emptied after one use?

If I want an interpretable model, are there methods other than Linear Regression?

How do certain apps show new notifications when internet access is restricted to them?



Closest day of week to specific date


Get the closest date of a specific week dayHow to print a date in a regular format?Convert HTML + CSS to PDF with PHP?Compare two dates with JavaScriptAdd days to JavaScript DateDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript dateHow do I check if a string contains a specific word?Get first and last date of current month with JavaScript or jQuery






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I'm trying to calculate National Sovereignty Day of Argentina which is celebrated on the Monday closest to 20 November.



I tried to find PHP solutions here but just found a Ruby solution: Get the closest date of a specific week day



How can I implement this for PHP?










share|improve this question


























  • Did you start coding anything in PHP? If so show us

    – RiggsFolly
    Mar 28 at 11:11











  • @RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

    – mitkosoft
    Mar 28 at 11:12











  • It's trivial to do that with Remind dianne.skoll.ca/projects/remind

    – Dougie
    Mar 28 at 11:13











  • “the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

    – 04FS
    Mar 28 at 11:14


















-1















I'm trying to calculate National Sovereignty Day of Argentina which is celebrated on the Monday closest to 20 November.



I tried to find PHP solutions here but just found a Ruby solution: Get the closest date of a specific week day



How can I implement this for PHP?










share|improve this question


























  • Did you start coding anything in PHP? If so show us

    – RiggsFolly
    Mar 28 at 11:11











  • @RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

    – mitkosoft
    Mar 28 at 11:12











  • It's trivial to do that with Remind dianne.skoll.ca/projects/remind

    – Dougie
    Mar 28 at 11:13











  • “the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

    – 04FS
    Mar 28 at 11:14














-1












-1








-1








I'm trying to calculate National Sovereignty Day of Argentina which is celebrated on the Monday closest to 20 November.



I tried to find PHP solutions here but just found a Ruby solution: Get the closest date of a specific week day



How can I implement this for PHP?










share|improve this question
















I'm trying to calculate National Sovereignty Day of Argentina which is celebrated on the Monday closest to 20 November.



I tried to find PHP solutions here but just found a Ruby solution: Get the closest date of a specific week day



How can I implement this for PHP?







php date strtotime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 11:35









Qirel

17.4k7 gold badges28 silver badges46 bronze badges




17.4k7 gold badges28 silver badges46 bronze badges










asked Mar 28 at 11:10









mitkosoftmitkosoft

3,7051 gold badge9 silver badges28 bronze badges




3,7051 gold badge9 silver badges28 bronze badges















  • Did you start coding anything in PHP? If so show us

    – RiggsFolly
    Mar 28 at 11:11











  • @RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

    – mitkosoft
    Mar 28 at 11:12











  • It's trivial to do that with Remind dianne.skoll.ca/projects/remind

    – Dougie
    Mar 28 at 11:13











  • “the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

    – 04FS
    Mar 28 at 11:14


















  • Did you start coding anything in PHP? If so show us

    – RiggsFolly
    Mar 28 at 11:11











  • @RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

    – mitkosoft
    Mar 28 at 11:12











  • It's trivial to do that with Remind dianne.skoll.ca/projects/remind

    – Dougie
    Mar 28 at 11:13











  • “the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

    – 04FS
    Mar 28 at 11:14

















Did you start coding anything in PHP? If so show us

– RiggsFolly
Mar 28 at 11:11





Did you start coding anything in PHP? If so show us

– RiggsFolly
Mar 28 at 11:11













@RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

– mitkosoft
Mar 28 at 11:12





@RiggsFolly, nope, I didn't as I cannot figure out what kind of logic to apply here.

– mitkosoft
Mar 28 at 11:12













It's trivial to do that with Remind dianne.skoll.ca/projects/remind

– Dougie
Mar 28 at 11:13





It's trivial to do that with Remind dianne.skoll.ca/projects/remind

– Dougie
Mar 28 at 11:13













“the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

– 04FS
Mar 28 at 11:14






“the Monday closest to 20 November” - so that would be the previous Monday if the 20th was anything up to and including Thursday, and the next one if it was Friday or “later” in the week. (When we assume the week starts on Monday, which PHP’s N date format character gives.)

– 04FS
Mar 28 at 11:14













2 Answers
2






active

oldest

votes


















2
















Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.



If $date->format("N") === 1, it's the date provided, and we don't need any modifications.



$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1)
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');

echo $date->format("Y-m-d");


  • Live demo





share|improve this answer



























  • It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

    – mitkosoft
    Mar 28 at 11:30







  • 1





    Seems you're correct! I'll update the answer accordingly.

    – Qirel
    Mar 28 at 11:34


















1
















Could try something like this:



<?php

function getSovDay($year)

$date = new DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)

return $date;
elseif ($weekDay <= 4)
return $date->modify("previous monday");
else
return $date->modify("next monday");



for($i = 2000; $i <= 2200; $i++)

echo getSovDay($i)->format("Y-m-d")."n";






share|improve this answer



























  • What if $date->format("N") == 1, what happens then?

    – Qirel
    Mar 28 at 11:27











  • Good point, edited the answer.

    – ccKep
    Mar 28 at 11:53













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55396126%2fclosest-day-of-week-to-specific-date%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.



If $date->format("N") === 1, it's the date provided, and we don't need any modifications.



$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1)
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');

echo $date->format("Y-m-d");


  • Live demo





share|improve this answer



























  • It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

    – mitkosoft
    Mar 28 at 11:30







  • 1





    Seems you're correct! I'll update the answer accordingly.

    – Qirel
    Mar 28 at 11:34















2
















Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.



If $date->format("N") === 1, it's the date provided, and we don't need any modifications.



$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1)
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');

echo $date->format("Y-m-d");


  • Live demo





share|improve this answer



























  • It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

    – mitkosoft
    Mar 28 at 11:30







  • 1





    Seems you're correct! I'll update the answer accordingly.

    – Qirel
    Mar 28 at 11:34













2














2










2









Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.



If $date->format("N") === 1, it's the date provided, and we don't need any modifications.



$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1)
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');

echo $date->format("Y-m-d");


  • Live demo





share|improve this answer















Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.



If $date->format("N") === 1, it's the date provided, and we don't need any modifications.



$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1)
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');

echo $date->format("Y-m-d");


  • Live demo






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 11:34

























answered Mar 28 at 11:23









QirelQirel

17.4k7 gold badges28 silver badges46 bronze badges




17.4k7 gold badges28 silver badges46 bronze badges















  • It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

    – mitkosoft
    Mar 28 at 11:30







  • 1





    Seems you're correct! I'll update the answer accordingly.

    – Qirel
    Mar 28 at 11:34

















  • It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

    – mitkosoft
    Mar 28 at 11:30







  • 1





    Seems you're correct! I'll update the answer accordingly.

    – Qirel
    Mar 28 at 11:34
















It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

– mitkosoft
Mar 28 at 11:30






It doesn't work for 2020 as it returns 2020-11-16 instead of the correct closest Monday on 2020-11-23. However the logic is fine (it should be > 4 I believe), so I'll accept this approach! Thank you!

– mitkosoft
Mar 28 at 11:30





1




1





Seems you're correct! I'll update the answer accordingly.

– Qirel
Mar 28 at 11:34





Seems you're correct! I'll update the answer accordingly.

– Qirel
Mar 28 at 11:34













1
















Could try something like this:



<?php

function getSovDay($year)

$date = new DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)

return $date;
elseif ($weekDay <= 4)
return $date->modify("previous monday");
else
return $date->modify("next monday");



for($i = 2000; $i <= 2200; $i++)

echo getSovDay($i)->format("Y-m-d")."n";






share|improve this answer



























  • What if $date->format("N") == 1, what happens then?

    – Qirel
    Mar 28 at 11:27











  • Good point, edited the answer.

    – ccKep
    Mar 28 at 11:53















1
















Could try something like this:



<?php

function getSovDay($year)

$date = new DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)

return $date;
elseif ($weekDay <= 4)
return $date->modify("previous monday");
else
return $date->modify("next monday");



for($i = 2000; $i <= 2200; $i++)

echo getSovDay($i)->format("Y-m-d")."n";






share|improve this answer



























  • What if $date->format("N") == 1, what happens then?

    – Qirel
    Mar 28 at 11:27











  • Good point, edited the answer.

    – ccKep
    Mar 28 at 11:53













1














1










1









Could try something like this:



<?php

function getSovDay($year)

$date = new DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)

return $date;
elseif ($weekDay <= 4)
return $date->modify("previous monday");
else
return $date->modify("next monday");



for($i = 2000; $i <= 2200; $i++)

echo getSovDay($i)->format("Y-m-d")."n";






share|improve this answer















Could try something like this:



<?php

function getSovDay($year)

$date = new DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)

return $date;
elseif ($weekDay <= 4)
return $date->modify("previous monday");
else
return $date->modify("next monday");



for($i = 2000; $i <= 2200; $i++)

echo getSovDay($i)->format("Y-m-d")."n";







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 11:53

























answered Mar 28 at 11:21









ccKepccKep

5,08613 silver badges26 bronze badges




5,08613 silver badges26 bronze badges















  • What if $date->format("N") == 1, what happens then?

    – Qirel
    Mar 28 at 11:27











  • Good point, edited the answer.

    – ccKep
    Mar 28 at 11:53

















  • What if $date->format("N") == 1, what happens then?

    – Qirel
    Mar 28 at 11:27











  • Good point, edited the answer.

    – ccKep
    Mar 28 at 11:53
















What if $date->format("N") == 1, what happens then?

– Qirel
Mar 28 at 11:27





What if $date->format("N") == 1, what happens then?

– Qirel
Mar 28 at 11:27













Good point, edited the answer.

– ccKep
Mar 28 at 11:53





Good point, edited the answer.

– ccKep
Mar 28 at 11:53


















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55396126%2fclosest-day-of-week-to-specific-date%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript