Date Ranges without using the year?

Hi all,

I need to display a different image on my page dependent on what the date is and whether today’s date is within certain ranges set by me.

I know how to do this using a full date, converted into Unix Timestamps but the problem with this is that I only need to check the day and month, the year is not important.

For example. If today’s date is between 1st Jan - 31st Jan, show image 1. If it is between 1st Feb - 28th Feb, show image 2.

You see, if I convert them into Unix Timestamps, I need a year and doing that means I have to change the script every year to update the timestamps used for the ranges. I’d rather have a script that ignored the year and just displayed results based on whether or not it was within the ranges set (regardless of the year)

Can anyone help?

Sounds like you don’t need a date range, just to know which month it is:


$month = date('m');
echo "<img src='images/months/$month.jpg'>";

// 01.jpg, 02.jpg, 03.jpg so on...

Hi,
You can use this code to get date without using year.

CREATE TABLE dt_tb (
id int(2) NOT NULL auto_increment,
dt datetime NOT NULL default ‘0000-00-00 00:00:00’,
dt2 date NOT NULL default ‘0000-00-00’,
PRIMARY KEY (id)
) TYPE=MyISAM;

raj23, that doesn’t make any sense

can you explain ~how~ what you posted can be used for the solution that cbresources is looking for?

Raj, like Rudy says, your answer has nothing to do with the question asked. Like I already said in another thread, it’s very nice that you’re trying to help out here, but do read the posts carefully, and try to answer the questions asked.

Hi cranial-bored.

Firstly, thank you for your help. I’m sorry but I was probably a little too vague with my question. I didn’t want to give away too much because the website I’m developing isn’t ready to launch yet… perhaps in this instance it might have paid to be a little more specific.

It’s not the month that I’m after. I was just using the dates in my original post as an example (perhaps a bad one).

The reason I’m wanting to do this is so that I know what “star sign” season we are in… so for instance, if the date falls between 20th January & 19th February… I need to display the “Aquarius” image. If it falls between 20th February - 19th March, I need to display the “pisces” image… and so on and so fort.

So I need to find “today’s date” and strip the year… Then I need to write 12 if statements that check if the final $date falls between my 12 timelines and then echo the correct image.

I hope that makes more sense now.

I eagerly await your response :slight_smile:

Raj, thank you for trying to help but I don’t believe I mentioned SQL anywhere in my post… this is a forum for PHP help. Thanks!

you don’t ~have~ to do it that way

that’s called embedding your data into your code

better would be to define a table with the start and stop dates you’re looking for, then do a BETWEEN condition with CURRENT_DATE

simpler, less code, less chance for error, easier to develop…

It is possible to quickly get the day of the year, starting with Jan 1 being 0.


echo date('z'); // gives 28 today, the 29th of Jan

So you can create a simple function to associate a day number with a name, so:


function get_starsign( $n ){

if( $n <= 19) return 'sagitarius'; // these are made up dates
if( $n <= 45 ) return 'aquarius';  // these are made up dates

// and so on

}

echo '<img src="'. get_starsign( date('z')) .'.jpg" />';


Gives: when day = 28
<img src=“aquarius.jpg” />

[fphp]date[/fphp]

You can then improve my code saample by studying how a switch works.

Hi,
I created one a while back and maybe you can use some of it here:


<?php

$todays_date = date('m-d');

    // this is for FALL    
    
    if($todays_date >= "09-21" && $todays_date <= "12-20") {
    
        if($todays_date >= "10-25" && $todays_date <= "11-01") {
        $date_logo = "samhain.gif";
        } elseif ($todays_date >= "11-21" && $todays_date <= "11-30") {
        $date_logo = "thanksgiving.gif";
        } else {
        $date_logo = "fall.gif"; 
        }
    }
    
    
    // These are for WINTER from 12-21 to 12-31
    
    if($todays_date >= "12-21" && $todays_date <= "12-31") {

        if($todays_date >= "12-24" && $todays_date <= "12-25") {
        $date_logo = "yule.gif";
        } elseif ($todays_date >= "12-29" && $todays_date <= "12-31") {
        $date_logo = "newyear.gif";
        } else {
        $date_logo = "winter.gif"; 
        }
    }
    
 // These are for WINTER from 01-01 - 03-18

    if($todays_date >= "01-01" && $todays_date <= "03-18") {

        if($todays_date >= "01-01" && $todays_date <= "01-03" ) {
        $date_logo = "newyear.gif";
        } elseif ($todays_date >= "02-02" && $todays_date <= "02-14") {
        $date_logo = "valentines.gif";
        } elseif ($todays_date >= "03-15" && $todays_date <= "03-17") {
        $date_logo = "stpattysday.gif";
        } else {
        $date_logo = "winter.gif"; 
        }
    }    
    
    
    // this is for SPRING
    
    if($todays_date >= "03-19" && $todays_date <= "06-20") {

    $date_logo = "spring.gif"; 
    }
    
    // this is for SUMMER    
    if($todays_date >= "06-21" && $todays_date <= "09-20") {
    
    $date_logo = "summer.gif";
    
    }


?>


<img src="http://www.yoursite.com/images/<?=$date_logo?>">

You can change or add accordingly. It works awesome on my site.

The gifs change depending on the season and holiday.

I didn’t add the rest of the code and hope you get the idea.

Thanks,
Kevin

Sorry,
You want this:

20th February - 19th March, I need to display the “pisces”


<?php
$todays_date = date('d-m');

    // this is for "pisces" 

    if($todays_date >= "19-02" && $todays_date <= "20-03") {
    $date_logo = "pisces.gif";
    }


    // this is for "aries" 
 
     if($todays_date >= "21-03" && $todays_date <= "19-04") {
     $date_logo = "aries.gif";
     }

?>