0

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

rob.m
  • 9,843
  • 19
  • 73
  • 162

2 Answers2

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

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • No need to use `getTimestamp()` DateTimeInterface objects can be compared directly. https://3v4l.org/mQjBY – Will B. Nov 15 '18 at 05:42
  • @fyrye ohh my bad.yes you're 100% right thanks :) – A l w a y s S u n n y Nov 15 '18 at 05: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 05:46
  • I also suggest using [`::createFromFormat`](http://php.net/manual/en/datetime.createfromformat.php) to ensure the [date format](http://php.net/manual/en/datetime.formats.date.php) is not mishandled – Will B. Nov 15 '18 at 05:46
  • make sure the $start and $end is string date value. To be sure you can check it using `var_dump($start);` – A l w a y s S u n n y Nov 15 '18 at 05:49
  • 1
    I need to convert it into a string date value – rob.m Nov 15 '18 at 05:50
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
     }
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20
  • brilliant, thanks this works – rob.m Nov 15 '18 at 05:51
  • 1
    @rob.m be sure to read the notes on [`strtotime`](http://php.net/manual/en/function.strtotime.php#refsect1-function.strtotime-notes) to ensure your dates are always compliant. – Will B. Nov 15 '18 at 05:59