Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-19-2011, 03:05 AM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Proper date range format?

Hi
This works because I'm working with a single number, problem is I need to do
this with a month and day like between 11-19 and 12-15 etc, should I use a
timestamp or z for day of year? whats the most efficient way to do something
like this?.

PHP Code:
    if (date("m") >= && date("m") <= 11) {
       echo 
"This is within range";
       echo 
date("m");
    }
    else {
       echo 
"Not within range";
       echo 
date("m");
    }

// I need to do something like

    
if (date("m-d") >= "11-19" && date("m-d") <= "12-15") {
       echo 
"This is within range";
       echo 
date("m-d");
    } 
Thanks
Sonny

Last edited by sonny; 11-19-2011 at 05:19 AM..
sonny is offline   Reply With Quote
Old 11-19-2011, 05:06 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Since you're not concerned about "Year",
are you going to always assume it's between the first date and second date,
not the second and first date? There is a difference.

So if you have this:

11-19 ... 02-20 (you mean 11-19-2011 and 02-20-2012)

and not (11-19-2011 and 02-20-2011)?

If you're always assuming the 2nd date is further in the future than the first date,
you can use UNIX timestamp. Otherwise, the script would never work ... not knowing
which direction you're going. Do you see what I mean?

My test ... upload this and see what it does ...
PHP Code:
<?php

// some test values.
// in this example, the 2nd date is assumed to be 2012, not 2011.
$date1="11-17";
$date2="02-15";

// the UNIX timestamp right now.
$now=time();

// determine which year to use.
if(strtotime(date("Y")."-".$date1) < strtotime(date("Y")."-".$date2)){
$date1=strtotime(date("Y")."-".$date1);
$date2=strtotime(date("Y")."-".$date2);
}
else{
$date1=strtotime(date("Y")."-".$date1);
$date2=strtotime((date("Y")+1)."-".$date2);
}

if(
$now >= $date1 && $now <= $date2){
echo 
"Within range";
}
else{
echo 
"Outside of range";
}

?>

.
mlseim is offline   Reply With Quote
Old 11-19-2011, 05:17 AM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
I figured it out, in case anyone needs something like this.

PHP Code:
    if ( time() >= strtotime('2011-11-18') && time() <= strtotime('2011-11-27')) {
       echo 
"<p>Have a nice thanksgiving</p>";
    }
//elseif (do other dates etc if needed 

Sonny
sonny is offline   Reply With Quote
Old 11-19-2011, 03:55 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
As you can see, specifying the year makes it 98% easier.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 AM.


Advertisement
Log in to turn off these ads.