...

View Full Version : Resolved Proper date range format?



sonny
11-19-2011, 03:05 AM
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?.



if (date("m") >= 9 && 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

mlseim
11-19-2011, 05:06 AM
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

// 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";
}

?>



.

sonny
11-19-2011, 05:17 AM
I figured it out, in case anyone needs something like this.



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

mlseim
11-19-2011, 03:55 PM
As you can see, specifying the year makes it 98% easier.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum