How to check if a number is in between two numbers (or dates)? [duplicate]










0
















This question already has an answer here:



  • How to check if a date is in a given range?

    10 answers



I get two values from GET



$start = $_GET['start']; 
$end = $_GET['end'];


These are:



1-11-2018
30-11-2018


Then I remove the dash to create a whole number



$start = str_replace(["-", "–"], '', $start);
$end = str_replace(["-", "–"], '', $end);


Now we have:



1112018
30112018


Then we do a loop over our posts (we only have 2 posts) and we grab the value from a custom field:



$myDate = get_post_meta($id, 'usp-custom-80', TRUE);


Which gives us:



13-11-2017
26-11-2018


And then we do:



$myDate = str_replace(["-", "–"], '', $start);


And we have:



13112017
26112018


So now we can check if the value we're getting from the custom field is within or not the values we have from GET



if (($myDate >= $start) && ($myDate <= $end)) {
//content....


But I am getting the logic wrong, also because the dates from GET could have 1 without a zero at the beginning 01 and the number would be less even tho the actual date is correct to be considered within it.



Any idea how I can check if $myDate is in between $start and $end?



UPDATE



If I don't remove dash and I get:



Start date: 1-11-2018
End date: 30-11-2018
User date: 13-11-2017


And then I simply do:



if ( ( $myDate >= $start) && ( $myDate <= $end) ) {


I get ALL posts and not the filtered by range










share|improve this question















marked as duplicate by Prix, billynoah, Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 5:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Basically you need to check that your myDate is between the two date $start and $end right?

    – Always Sunny
    Nov 15 '18 at 5:34











  • @Curious_MInd exactly

    – rob.m
    Nov 15 '18 at 5:34






  • 2





    Possible duplicate of How to check if a date is in a given range?

    – Prix
    Nov 15 '18 at 5:35












  • why are you removing the dashes? call strtotime() on all your dates and compare results.

    – billynoah
    Nov 15 '18 at 5:41












  • @rob.m I've added answer for you then :)

    – Always Sunny
    Nov 15 '18 at 5:41















0
















This question already has an answer here:



  • How to check if a date is in a given range?

    10 answers



I get two values from GET



$start = $_GET['start']; 
$end = $_GET['end'];


These are:



1-11-2018
30-11-2018


Then I remove the dash to create a whole number



$start = str_replace(["-", "–"], '', $start);
$end = str_replace(["-", "–"], '', $end);


Now we have:



1112018
30112018


Then we do a loop over our posts (we only have 2 posts) and we grab the value from a custom field:



$myDate = get_post_meta($id, 'usp-custom-80', TRUE);


Which gives us:



13-11-2017
26-11-2018


And then we do:



$myDate = str_replace(["-", "–"], '', $start);


And we have:



13112017
26112018


So now we can check if the value we're getting from the custom field is within or not the values we have from GET



if (($myDate >= $start) && ($myDate <= $end)) {
//content....


But I am getting the logic wrong, also because the dates from GET could have 1 without a zero at the beginning 01 and the number would be less even tho the actual date is correct to be considered within it.



Any idea how I can check if $myDate is in between $start and $end?



UPDATE



If I don't remove dash and I get:



Start date: 1-11-2018
End date: 30-11-2018
User date: 13-11-2017


And then I simply do:



if ( ( $myDate >= $start) && ( $myDate <= $end) ) {


I get ALL posts and not the filtered by range










share|improve this question















marked as duplicate by Prix, billynoah, Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 5:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Basically you need to check that your myDate is between the two date $start and $end right?

    – Always Sunny
    Nov 15 '18 at 5:34











  • @Curious_MInd exactly

    – rob.m
    Nov 15 '18 at 5:34






  • 2





    Possible duplicate of How to check if a date is in a given range?

    – Prix
    Nov 15 '18 at 5:35












  • why are you removing the dashes? call strtotime() on all your dates and compare results.

    – billynoah
    Nov 15 '18 at 5:41












  • @rob.m I've added answer for you then :)

    – Always Sunny
    Nov 15 '18 at 5:41













0












0








0









This question already has an answer here:



  • How to check if a date is in a given range?

    10 answers



I get two values from GET



$start = $_GET['start']; 
$end = $_GET['end'];


These are:



1-11-2018
30-11-2018


Then I remove the dash to create a whole number



$start = str_replace(["-", "–"], '', $start);
$end = str_replace(["-", "–"], '', $end);


Now we have:



1112018
30112018


Then we do a loop over our posts (we only have 2 posts) and we grab the value from a custom field:



$myDate = get_post_meta($id, 'usp-custom-80', TRUE);


Which gives us:



13-11-2017
26-11-2018


And then we do:



$myDate = str_replace(["-", "–"], '', $start);


And we have:



13112017
26112018


So now we can check if the value we're getting from the custom field is within or not the values we have from GET



if (($myDate >= $start) && ($myDate <= $end)) {
//content....


But I am getting the logic wrong, also because the dates from GET could have 1 without a zero at the beginning 01 and the number would be less even tho the actual date is correct to be considered within it.



Any idea how I can check if $myDate is in between $start and $end?



UPDATE



If I don't remove dash and I get:



Start date: 1-11-2018
End date: 30-11-2018
User date: 13-11-2017


And then I simply do:



if ( ( $myDate >= $start) && ( $myDate <= $end) ) {


I get ALL posts and not the filtered by range










share|improve this question

















This question already has an answer here:



  • How to check if a date is in a given range?

    10 answers



I get two values from GET



$start = $_GET['start']; 
$end = $_GET['end'];


These are:



1-11-2018
30-11-2018


Then I remove the dash to create a whole number



$start = str_replace(["-", "–"], '', $start);
$end = str_replace(["-", "–"], '', $end);


Now we have:



1112018
30112018


Then we do a loop over our posts (we only have 2 posts) and we grab the value from a custom field:



$myDate = get_post_meta($id, 'usp-custom-80', TRUE);


Which gives us:



13-11-2017
26-11-2018


And then we do:



$myDate = str_replace(["-", "–"], '', $start);


And we have:



13112017
26112018


So now we can check if the value we're getting from the custom field is within or not the values we have from GET



if (($myDate >= $start) && ($myDate <= $end)) {
//content....


But I am getting the logic wrong, also because the dates from GET could have 1 without a zero at the beginning 01 and the number would be less even tho the actual date is correct to be considered within it.



Any idea how I can check if $myDate is in between $start and $end?



UPDATE



If I don't remove dash and I get:



Start date: 1-11-2018
End date: 30-11-2018
User date: 13-11-2017


And then I simply do:



if ( ( $myDate >= $start) && ( $myDate <= $end) ) {


I get ALL posts and not the filtered by range





This question already has an answer here:



  • How to check if a date is in a given range?

    10 answers







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:41







rob.m

















asked Nov 15 '18 at 5:31









rob.mrob.m

3,820113984




3,820113984




marked as duplicate by Prix, billynoah, Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 5:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Prix, billynoah, Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 5:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Basically you need to check that your myDate is between the two date $start and $end right?

    – Always Sunny
    Nov 15 '18 at 5:34











  • @Curious_MInd exactly

    – rob.m
    Nov 15 '18 at 5:34






  • 2





    Possible duplicate of How to check if a date is in a given range?

    – Prix
    Nov 15 '18 at 5:35












  • why are you removing the dashes? call strtotime() on all your dates and compare results.

    – billynoah
    Nov 15 '18 at 5:41












  • @rob.m I've added answer for you then :)

    – Always Sunny
    Nov 15 '18 at 5:41

















  • Basically you need to check that your myDate is between the two date $start and $end right?

    – Always Sunny
    Nov 15 '18 at 5:34











  • @Curious_MInd exactly

    – rob.m
    Nov 15 '18 at 5:34






  • 2





    Possible duplicate of How to check if a date is in a given range?

    – Prix
    Nov 15 '18 at 5:35












  • why are you removing the dashes? call strtotime() on all your dates and compare results.

    – billynoah
    Nov 15 '18 at 5:41












  • @rob.m I've added answer for you then :)

    – Always Sunny
    Nov 15 '18 at 5:41
















Basically you need to check that your myDate is between the two date $start and $end right?

– Always Sunny
Nov 15 '18 at 5:34





Basically you need to check that your myDate is between the two date $start and $end right?

– Always Sunny
Nov 15 '18 at 5:34













@Curious_MInd exactly

– rob.m
Nov 15 '18 at 5:34





@Curious_MInd exactly

– rob.m
Nov 15 '18 at 5:34




2




2





Possible duplicate of How to check if a date is in a given range?

– Prix
Nov 15 '18 at 5:35






Possible duplicate of How to check if a date is in a given range?

– Prix
Nov 15 '18 at 5:35














why are you removing the dashes? call strtotime() on all your dates and compare results.

– billynoah
Nov 15 '18 at 5:41






why are you removing the dashes? call strtotime() on all your dates and compare results.

– billynoah
Nov 15 '18 at 5:41














@rob.m I've added answer for you then :)

– Always Sunny
Nov 15 '18 at 5:41





@rob.m I've added answer for you then :)

– Always Sunny
Nov 15 '18 at 5:41












2 Answers
2






active

oldest

votes


















0














You can use strtotime() to parse date then compare dates like below-



 $start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) )
......
// your other code goes here






share|improve this answer

























  • brilliant, thanks this works

    – rob.m
    Nov 15 '18 at 5:51






  • 1





    @rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

    – fyrye
    Nov 15 '18 at 5:59


















1














You can simply do this way with DateTime class,



<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');

if ($myDate > $start && $myDate < $end )
echo "Date is between";
else
echo "Date is not between!";

?>


DEMO: https://3v4l.org/Grv1X






share|improve this answer

























  • No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

    – fyrye
    Nov 15 '18 at 5:42











  • @fyrye ohh my bad.yes you're 100% right thanks :)

    – Always Sunny
    Nov 15 '18 at 5:43











  • it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

    – rob.m
    Nov 15 '18 at 5:46











  • I also suggest using ::createFromFormat to ensure the date format is not mishandled

    – fyrye
    Nov 15 '18 at 5:46







  • 1





    I need to convert it into a string date value

    – rob.m
    Nov 15 '18 at 5:50

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use strtotime() to parse date then compare dates like below-



 $start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) )
......
// your other code goes here






share|improve this answer

























  • brilliant, thanks this works

    – rob.m
    Nov 15 '18 at 5:51






  • 1





    @rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

    – fyrye
    Nov 15 '18 at 5:59















0














You can use strtotime() to parse date then compare dates like below-



 $start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) )
......
// your other code goes here






share|improve this answer

























  • brilliant, thanks this works

    – rob.m
    Nov 15 '18 at 5:51






  • 1





    @rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

    – fyrye
    Nov 15 '18 at 5:59













0












0








0







You can use strtotime() to parse date then compare dates like below-



 $start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) )
......
// your other code goes here






share|improve this answer















You can use strtotime() to parse date then compare dates like below-



 $start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) )
......
// your other code goes here







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 14 '18 at 16:09









Always Sunny

16.5k32848




16.5k32848










answered Nov 15 '18 at 5:42









suresh bambhaniyasuresh bambhaniya

945315




945315












  • brilliant, thanks this works

    – rob.m
    Nov 15 '18 at 5:51






  • 1





    @rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

    – fyrye
    Nov 15 '18 at 5:59

















  • brilliant, thanks this works

    – rob.m
    Nov 15 '18 at 5:51






  • 1





    @rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

    – fyrye
    Nov 15 '18 at 5:59
















brilliant, thanks this works

– rob.m
Nov 15 '18 at 5:51





brilliant, thanks this works

– rob.m
Nov 15 '18 at 5:51




1




1





@rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

– fyrye
Nov 15 '18 at 5:59





@rob.m be sure to read the notes on strtotime to ensure your dates are always compliant.

– fyrye
Nov 15 '18 at 5:59













1














You can simply do this way with DateTime class,



<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');

if ($myDate > $start && $myDate < $end )
echo "Date is between";
else
echo "Date is not between!";

?>


DEMO: https://3v4l.org/Grv1X






share|improve this answer

























  • No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

    – fyrye
    Nov 15 '18 at 5:42











  • @fyrye ohh my bad.yes you're 100% right thanks :)

    – Always Sunny
    Nov 15 '18 at 5:43











  • it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

    – rob.m
    Nov 15 '18 at 5:46











  • I also suggest using ::createFromFormat to ensure the date format is not mishandled

    – fyrye
    Nov 15 '18 at 5:46







  • 1





    I need to convert it into a string date value

    – rob.m
    Nov 15 '18 at 5:50















1














You can simply do this way with DateTime class,



<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');

if ($myDate > $start && $myDate < $end )
echo "Date is between";
else
echo "Date is not between!";

?>


DEMO: https://3v4l.org/Grv1X






share|improve this answer

























  • No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

    – fyrye
    Nov 15 '18 at 5:42











  • @fyrye ohh my bad.yes you're 100% right thanks :)

    – Always Sunny
    Nov 15 '18 at 5:43











  • it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

    – rob.m
    Nov 15 '18 at 5:46











  • I also suggest using ::createFromFormat to ensure the date format is not mishandled

    – fyrye
    Nov 15 '18 at 5:46







  • 1





    I need to convert it into a string date value

    – rob.m
    Nov 15 '18 at 5:50













1












1








1







You can simply do this way with DateTime class,



<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');

if ($myDate > $start && $myDate < $end )
echo "Date is between";
else
echo "Date is not between!";

?>


DEMO: https://3v4l.org/Grv1X






share|improve this answer















You can simply do this way with DateTime class,



<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');

if ($myDate > $start && $myDate < $end )
echo "Date is between";
else
echo "Date is not between!";

?>


DEMO: https://3v4l.org/Grv1X







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 5:44

























answered Nov 15 '18 at 5:39









Always SunnyAlways Sunny

16.5k32848




16.5k32848












  • No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

    – fyrye
    Nov 15 '18 at 5:42











  • @fyrye ohh my bad.yes you're 100% right thanks :)

    – Always Sunny
    Nov 15 '18 at 5:43











  • it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

    – rob.m
    Nov 15 '18 at 5:46











  • I also suggest using ::createFromFormat to ensure the date format is not mishandled

    – fyrye
    Nov 15 '18 at 5:46







  • 1





    I need to convert it into a string date value

    – rob.m
    Nov 15 '18 at 5:50

















  • No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

    – fyrye
    Nov 15 '18 at 5:42











  • @fyrye ohh my bad.yes you're 100% right thanks :)

    – Always Sunny
    Nov 15 '18 at 5:43











  • it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

    – rob.m
    Nov 15 '18 at 5:46











  • I also suggest using ::createFromFormat to ensure the date format is not mishandled

    – fyrye
    Nov 15 '18 at 5:46







  • 1





    I need to convert it into a string date value

    – rob.m
    Nov 15 '18 at 5:50
















No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

– fyrye
Nov 15 '18 at 5:42





No need to use getTimestamp() DateTimeInterface objects can be compared directly. 3v4l.org/mQjBY

– fyrye
Nov 15 '18 at 5:42













@fyrye ohh my bad.yes you're 100% right thanks :)

– Always Sunny
Nov 15 '18 at 5:43





@fyrye ohh my bad.yes you're 100% right thanks :)

– Always Sunny
Nov 15 '18 at 5:43













it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

– rob.m
Nov 15 '18 at 5:46





it gives me an error Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, the values of start/end are from GET $start = $_GET['start'];

– rob.m
Nov 15 '18 at 5:46













I also suggest using ::createFromFormat to ensure the date format is not mishandled

– fyrye
Nov 15 '18 at 5:46






I also suggest using ::createFromFormat to ensure the date format is not mishandled

– fyrye
Nov 15 '18 at 5:46





1




1





I need to convert it into a string date value

– rob.m
Nov 15 '18 at 5:50





I need to convert it into a string date value

– rob.m
Nov 15 '18 at 5:50



這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3