1

Need help understanding why this doesn't work. I have a text that's only visible on a given start date and time and gets removed on ended date and time.

This is the code I am using that works just fine. The text is visible for a month without problems:

<?php 
     if ( date('Y/m/d H:i') >= "2017/04/06 08:00" && 
          date('Y/m/d H:i') <= "2017/05/06 20:00" ) { 
         echo "$text_1"; 
     } else { 
         echo "$text_2"; 
     }
?>

This is how I would like to be able write the date but it doesn't work as it supposed to:

<?php 
     if ( date('d/m/Y H:i') >= "06/04/2017 08:00" && 
          date('d/m/Y H:i') <= "06/05/2017 20:00" ) { 
         echo "$text_1"; 
     } else { 
         echo "$text_2"; 
     }
?>

It doesn't take into account the month or year. Why is that? This would only show the text for one day and not for a month.

arghtype
  • 4,376
  • 11
  • 45
  • 60
Alex Welander
  • 89
  • 1
  • 7
  • No wonder that you don't understand it, if you don't format it properly. – Aloso Apr 06 '17 at 22:53
  • feel free to elaborate what I am missing please – Alex Welander Apr 06 '17 at 22:56
  • Is this a locale issue? `"06/04/2017 08:00"` is being interpreted as Jun-4 (American mm/dd/yy), not Apr-6 (European dd/mm/yy) as you are hoping? I'd just stick with year-month-day if it works. There are lots of date comparison examples in other answers, such as [How to compare two dates in PHP](http://stackoverflow.com/questions/8722806/how-to-compare-two-dates-in-php) – domwrap Apr 06 '17 at 22:58
  • Yeas that right it should be 6 of Aprle 2017 d = day / m = Month / Y = year or is this all wrong? – Alex Welander Apr 06 '17 at 23:01

2 Answers2

0

Change date into timestamp and then compare. Format will not matter

if ( strtotime(date("Y-m-d H:i")) >= strtotime("06/04/2017 08:00") && 
    strtotime(date("Y-m-d H:i")) <= strtotime("06/05/2017 20:00" )) { 
    echo "$text_1"; 
} 
else { 
    echo "$text_2"; 
}

Its does not matter the formate now whether "Y-m-d H:i" or d/m/Y H:i. You can do any formate.

You can also do as follows

if ( strtotime(date("d/m/Y H:i")) >= strtotime("06/04/2017 08:00") && 
    strtotime(date("d/m/Y H:i")) <= strtotime("06/05/2017 20:00" )) { 
    echo "aaa"; 
} 
else { 
    echo "bbb"; 
}

The date format really does not matter, when ever you try to compare dates, Convert that in seconds and then compare .
Just take a lot at strtotime() method and do examples and see.

BetaDev
  • 4,516
  • 3
  • 21
  • 47
  • 1
    `strtotime(date(...))` is way too complicated. Why generate a date, formatted as string, just to parse it into a date again? See `time()` – Aloso Apr 06 '17 at 23:32
  • yeah your are right @Aloso but when I was the post, this solution clicked my mind. You are right better to use time(); – BetaDev Apr 06 '17 at 23:34
0

The problem is that you compare strings using >= and <= which is not possible in most cases.

Here's what PHP does when executing the comparison date('Y/m/d H:i') >= "2017/04/06 08:00":

First, the date() function is executed, which returns a string like "2017/04/23 12:51"

Since >= can only compare numbers, both strings are then implicitly casted into double. For this, PHP uses the strtod function. This function parses a string from left to right. If the first thing it finds (except for whitespace) is a number/infinity/NAN, this number is returned. As soon as strtod finds a non-numeric character, the rest of the string is ignored. If strtod does not find a number, it returns 0.

In this case, strtod returns only the year because it does not understand what a / means, and therefore ignores everything after the year.


If you want to compare dates properly, you have to use timestamps. To get the current timestamp, PHP offers the time() function.


Aloso
  • 5,123
  • 4
  • 24
  • 41