PHP ~ Check if a date occurs during the next (proper) week
I've searched for this one, and I have a solution that gets dates for Monday and Friday (only looking for working week) of next week, and then checks if my date falls within that range. It works.
<?php
$check_count = 0 ;
$check_date = 20181127 ;
if ( $check_date >= date ( 'Ymd' , strtotime ( 'monday next week' ) ) &&
$check_date <= date ( 'Ymd' , strtotime ( 'friday next week' ) ) ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
But I would like to know if there is a one-liner of sorts, which can take a date ( 'Ymd' ) and see if it occurs "next week."
Any help will be much appreciated.
Thanks,
furi0usBee
php date time compare
|
show 1 more comment
I've searched for this one, and I have a solution that gets dates for Monday and Friday (only looking for working week) of next week, and then checks if my date falls within that range. It works.
<?php
$check_count = 0 ;
$check_date = 20181127 ;
if ( $check_date >= date ( 'Ymd' , strtotime ( 'monday next week' ) ) &&
$check_date <= date ( 'Ymd' , strtotime ( 'friday next week' ) ) ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
But I would like to know if there is a one-liner of sorts, which can take a date ( 'Ymd' ) and see if it occurs "next week."
Any help will be much appreciated.
Thanks,
furi0usBee
php date time compare
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please use theedit
function, stackoverflow.com/posts/53460560/edit.
– user3783243
Nov 24 '18 at 17:25
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31
|
show 1 more comment
I've searched for this one, and I have a solution that gets dates for Monday and Friday (only looking for working week) of next week, and then checks if my date falls within that range. It works.
<?php
$check_count = 0 ;
$check_date = 20181127 ;
if ( $check_date >= date ( 'Ymd' , strtotime ( 'monday next week' ) ) &&
$check_date <= date ( 'Ymd' , strtotime ( 'friday next week' ) ) ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
But I would like to know if there is a one-liner of sorts, which can take a date ( 'Ymd' ) and see if it occurs "next week."
Any help will be much appreciated.
Thanks,
furi0usBee
php date time compare
I've searched for this one, and I have a solution that gets dates for Monday and Friday (only looking for working week) of next week, and then checks if my date falls within that range. It works.
<?php
$check_count = 0 ;
$check_date = 20181127 ;
if ( $check_date >= date ( 'Ymd' , strtotime ( 'monday next week' ) ) &&
$check_date <= date ( 'Ymd' , strtotime ( 'friday next week' ) ) ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
But I would like to know if there is a one-liner of sorts, which can take a date ( 'Ymd' ) and see if it occurs "next week."
Any help will be much appreciated.
Thanks,
furi0usBee
php date time compare
php date time compare
edited Nov 24 '18 at 17:34
furi0usBee
asked Nov 24 '18 at 17:13
furi0usBeefuri0usBee
62
62
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please use theedit
function, stackoverflow.com/posts/53460560/edit.
– user3783243
Nov 24 '18 at 17:25
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31
|
show 1 more comment
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please use theedit
function, stackoverflow.com/posts/53460560/edit.
– user3783243
Nov 24 '18 at 17:25
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please use the
edit
function, stackoverflow.com/posts/53460560/edit.– user3783243
Nov 24 '18 at 17:25
Please use the
edit
function, stackoverflow.com/posts/53460560/edit.– user3783243
Nov 24 '18 at 17:25
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
Hi furi0usBee and welcome to stackoverflow,
you could shorten your if-statement a little by using the date_format
function to check for the week number ('W') and the ISO day of the week ('N').
To use the date_format
function you have to create a DateTime object from your Ymd date:
<?php
$check_count = 0 ;
$check_date = 20181127 ;
$objDate = new DateTime($check_date);
if ( date('W', strtotime('monday next week')) == date_format($objDate, 'W')
&& date_format($objDate, 'N') < 6 ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
The code looks a bit cleaner with this.
add a comment |
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/3.0/"u003ecc by-sa 3.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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460560%2fphp-check-if-a-date-occurs-during-the-next-proper-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hi furi0usBee and welcome to stackoverflow,
you could shorten your if-statement a little by using the date_format
function to check for the week number ('W') and the ISO day of the week ('N').
To use the date_format
function you have to create a DateTime object from your Ymd date:
<?php
$check_count = 0 ;
$check_date = 20181127 ;
$objDate = new DateTime($check_date);
if ( date('W', strtotime('monday next week')) == date_format($objDate, 'W')
&& date_format($objDate, 'N') < 6 ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
The code looks a bit cleaner with this.
add a comment |
Hi furi0usBee and welcome to stackoverflow,
you could shorten your if-statement a little by using the date_format
function to check for the week number ('W') and the ISO day of the week ('N').
To use the date_format
function you have to create a DateTime object from your Ymd date:
<?php
$check_count = 0 ;
$check_date = 20181127 ;
$objDate = new DateTime($check_date);
if ( date('W', strtotime('monday next week')) == date_format($objDate, 'W')
&& date_format($objDate, 'N') < 6 ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
The code looks a bit cleaner with this.
add a comment |
Hi furi0usBee and welcome to stackoverflow,
you could shorten your if-statement a little by using the date_format
function to check for the week number ('W') and the ISO day of the week ('N').
To use the date_format
function you have to create a DateTime object from your Ymd date:
<?php
$check_count = 0 ;
$check_date = 20181127 ;
$objDate = new DateTime($check_date);
if ( date('W', strtotime('monday next week')) == date_format($objDate, 'W')
&& date_format($objDate, 'N') < 6 ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
The code looks a bit cleaner with this.
Hi furi0usBee and welcome to stackoverflow,
you could shorten your if-statement a little by using the date_format
function to check for the week number ('W') and the ISO day of the week ('N').
To use the date_format
function you have to create a DateTime object from your Ymd date:
<?php
$check_count = 0 ;
$check_date = 20181127 ;
$objDate = new DateTime($check_date);
if ( date('W', strtotime('monday next week')) == date_format($objDate, 'W')
&& date_format($objDate, 'N') < 6 ) {
// DATE IS NEXT WEEK
echo 'Next week!' ;
$check_count++ ;
}
?>
The code looks a bit cleaner with this.
answered Nov 24 '18 at 22:53
digijaydigijay
5861715
5861715
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460560%2fphp-check-if-a-date-occurs-during-the-next-proper-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please add your current code.
– user3783243
Nov 24 '18 at 17:19
Please use the
edit
function, stackoverflow.com/posts/53460560/edit.– user3783243
Nov 24 '18 at 17:25
Isn't that already a one liner? Do you mean a single function?
– user3783243
Nov 24 '18 at 17:27
I'm actually looking to check a date that falls within Monday - Friday of the next week, not through Sunday, per the code. Sorry about that. Rough start to my account here LOL.
– furi0usBee
Nov 24 '18 at 17:29
Rather than having to get a start/finish date for next week, I though maybe there was just a single check, such as date_interval ( "+1 weeks") or something to that effect.
– furi0usBee
Nov 24 '18 at 17:31