?
Solved

if Today's Month is greater than

Posted on 2008-11-15
5
Medium Priority
?
539 Views
Last Modified: 2012-05-05
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
Comment
Question by:Mada123
  • 3
5 Comments
 
LVL 6

Expert Comment

by:cr4ck3rj4ck
ID: 22967762
Try the following:

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

Hope that helps,
CJ
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 22967797
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
 

Author Comment

by:Mada123
ID: 22967963
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22967993
...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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22967996
... 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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question