• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

if Today's Month is greater than

I have a variable $Today=date('F'); and it echos November.

I have an if statement

<? if ($Today > December) {
include 'Dec.php'; }
      else
      {
}

but it still shows December data. Can someone please tell me what I'm doing wrong?
0
Mada123
Asked:
Mada123
  • 3
1 Solution
 
cr4ck3rj4ckCommented:
Try the following:

<?php
   if($Today == "December"){
      include 'Dec.php';
   }else{
      // Other months go here
   }
?>

Hope that helps,
CJ
0
 
Roger BaklundCommented:
You got working code from cr4ck3rj4ck, but he did not tell you why your code is wrong.

First, the reason you are not getting an error message, is because PHP thinks December must be a constant, since it is not in quotes (string literal) and it is not preceeded by a $ (variable). Since the constant December is not defined, php will assume the value is 'December'.

The value of $Today is 'November'. PHP does not understand the consept of months, it sees these values just as strings. When comparing a string starting with 'N' with a string starting with 'D', PHP will say that the string starting with 'N' is the "biggest", as it comes later in the alphabet.  As a consequence ('November' > 'December') is true in PHP.
0
 
Mada123Author Commented:
Thanks for the explanation. With that, I was able to figure out that  I need to do this instead to get what I want:

$Today=date('m');

<? if ($Today > '12') {
include 'Dec.php';
} else {

}
?>
0
 
Roger BaklundCommented:
...actually, you still have a problem. You will notice in february... the string '2' is later in the alphabet than the string '12'. You need to do an integer comparison:

<? if ((int)$Today > 12) {
include 'Dec.php';
} else {
 
}
?>

Open in new window

0
 
Roger BaklundCommented:
... this will however never be true, as (12 > 12) is false. This should be correct:
<? if ((int)$Today >= 12) {
include 'Dec.php';
} else {
 
}
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now