Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Dates and If not function not working as is should

Jam

Coder
Without the boring entry stuff - the function is pretty straight forward. Just don't understand why it ain't working.

PHP:
$sydatumopteken // this variable I get from the database. The value is 08-10-2020


$sydatumopteken = substr($sydatumopteken,0,2); //the value is 08

$sydatumopteken = (int)$sydatumopteken; //the value is 8

$didagvandag = date("d"); //the value is 08

$didagvandag = (int)$sydatumopteken; //the value is 8

$dag2 = $sydatumopteken + 1; //the value is 9

$dag3 = $sydatumopteken + 2; //the value is 10

$dag4 = $sydatumopteken + 3; //the value is 11

$dag5 = $sydatumopteken + 4; //the value is 12

$dag6 = $sydatumopteken + 5; //the value is 13

  

if($sydatumopteken != $didagvandag || $didagvandag != $dag2 || $didagvandag != $dag3 || $didagvandag != $dag4 || $didagvandag != $dag5 || $didagvandag != $dag6 || $sydatumopteken != 1 || $sydatumopteken != 2 || $sydatumopteken != 3 || $sydatumopteken != 4) {

           Every time it runs, it ends up here. Can someone tell me why.

}
 
Last edited:
Hi Malcolm. Apologies, I am new....and have correct it :)
Much appreciated! Much easier to read, right? :)

Not too familiar with this, so an if statement works as if this condition is true. Then do this. So I think that one of your conditions is meeting it's criteria and is displaying the statement.
 
Actually, the if has $sysdatumopteken != $dag2 as well as all the following comparisons, which all hold true. This causes the if block to be entered.
Thanks for your response!

I am trying to test if the day today ($didagvandag = 08) is the same as the signed up date ($sydatumopteken = 08)
OR
if the day today ($didagvandag = 08) is the same as 5 days after signed up date ($dag2 = $sydatumopteken + 1 = 9, etc.)
OR
if the day today ($didagvandag = 08) is in the first 4 days in the month ($sydatumopteken != 1)

Only one of the above comparisons can be true.
If the day today ($didagvandag) is NOT the same as the signed up date ($sydatumopteken) or 5 days after that or the first four days in the month, then the IF function should be entered.
At the moment - the first statement IS true, and the IF function gets entered.
 
Much appreciated! Much easier to read, right? :)

Not too familiar with this, so an if statement works as if this condition is true. Then do this. So I think that one of your conditions is meeting it's criteria and is displaying the statement.
Definitely easier, thanks!

I want the IF function to enter (work) only if none of the conditions are true. If one of the conditions is true, then the IF function should not enter(work).
 
Definitely easier, thanks!

I want the IF function to enter (work) only if none of the conditions are true. If one of the conditions is true, then the IF function should not enter(work).
Perhaps make sure that in front of each condition you have != (Not) perhaps it's thinking one is not while the other isn't. I could be wrong though.
 
Perhaps make sure that in front of each condition you have != (Not) perhaps it's thinking one is not while the other isn't. I could be wrong though.
Many thanks for your responses and advice offered :) :)

I actually needed to replace || with && as Krusty the Senile suggested. Everything works smoooothly now.
 
I was going to suggest working in date objects, so you don't have to worry too much about month boundaries.
Maybe this will help:
PHP:
$signUpDate = date_create_from_format("d-m-Y", "08-10-2020");
$dateNow = date_create(); // current date/time. Refer to docs if timezone is required.
echo date_format($dateNow, "d-m-Y")."\r\n";
echo date_format($signUpDate, "d-m-Y")."\r\n";
echo date_diff($signUpDate, $dateNow)->days."\r\n";
 

New Threads

Buy us a coffee!

Back
Top Bottom