Printing next date within an array

Hi,

Im a complete novice when it comes to php but im trying to print the next date within an array. I have the following code but im not sure im anywhere near:

<?php function get_next_dressdown() {
// Dress Down Days
$dress = array(
‘7-Jan-2011’,
‘4-Feb-2011’,
‘18-Mar-2011’,
‘1-Apr-2011’,
‘6-May-2011’,
‘3-Jun-2011’,
‘1-Jul-2011’,
‘5-Aug-2011’,
‘2-Sep-2011’,
‘7-Oct-2011’,
‘4-Nov-2011’,
‘2-Dec-2011’,
);
$current = time()
if ($dress>=$current) {
echo date(“d<\s\u\p>S</\s\u\p> F Y”,$dress);
break;
}
?></p>

Anyone help me out or point me in the right direction?

Thanks,

Mike

A couple of things to note:

  • The dates in the array aren’t really in a viable format to be compared to the current time. It’d be better if they were in a timestamp format, i.e. an integer storing the amount of seconds elapsed since 1st Jan 1970. So when you’re comparing them, use mktime() to get the date in the array as a number. Then use the time() function to get the current timestamp to compare.
  • You’re trying to compare the whole array with the current time - so use a loop to compare each value - foreach with a break to stop it when it’s found a suitable date.

Hi Jake,

Thanks for the info so am i along the right lines with this:

<?php
// Dress Down Days
$dress = array(
‘07/01/2011’,
‘04/02/2011’,
‘18/03/2011’,
‘01/04/2011’,
‘06/05/2011’,
‘03/06/2011’,
‘01/07/2011’,
‘05/08/2011’,
‘02/09/2011’,
‘07/10/2011’,
‘04/11/2011’,
‘02/12/2011’,
);
$date = date( “d/m/Y”, time() );
foreach ($dress as $value >= $date) {
echo date(“d<\s\u\p>S</\s\u\p> F Y”,$date);
break;
}
?>

Kind of. I’ll give you a hand :stuck_out_tongue:

function get_next_dressdown(){
	$dress = array(
		'07/01/2011', 
		'04/02/2011', 
		'18/03/2011',
		'01/04/2011',
		'06/05/2011',
		'03/06/2011',
		'01/07/2011',
		'05/08/2011',
		'02/09/2011',
		'07/10/2011',
		'04/11/2011',
		'02/12/2011', 
	);
	$currentTime = time();
	foreach ($dress as $date){
		$dateParts = explode('/', $date);
		$dressTime = mktime(0, 0, 0, $dateParts[1], $dateParts[0], $dateParts[2]);
		if($dressTime > $currentTime){
			return $dressTime;
			break;
		}
	}
	return false;
}

The way you’d use this:


$nextDate = get_next_dressdown();
if($nextDate === false){
    echo 'No more dressdowns';
}else{
    echo 'Next dressdown: ', date("d<\\s\\u\\p>S</\\s\\u\\p> F Y", $nextDate);
}

Thanks Jake you are a legend. Wasnt even close!! :-p

On a seperate matter my rss feeds have stopped showing. Ive checked that the rss feeds are up and they are. Could you take a look at this for me and see if you can see anything obvious:

<p id=“weather”><?php
$rs = $rss->get(‘http://xml.weather.yahoo.com/forecastrss?p=UKXX1413&u=c’);
echo “<b>Today’s Weather</b><br /><span class=‘indent’>”;

$desc = explode(“<br />”,str_replace(“<BR />”,“<br />”,$rs[‘items’][0][‘description’]));
echo str_replace(“<img”,“<img alt=‘’”,$desc[0]);
echo str_replace(“,”,“<br />”,$desc[2]);
echo “</span>”;
?></p>

Last one i promise :slight_smile: