Jump to content

Dates in an Array


pithed2

Recommended Posts

Ok, I'm obviously screwing up my code, as when I var_dump the array I get nothing, so what stupid little thing am I messing up here?

 

<?php
$preseason = strtotime('2009-08-11');
$week1 = strtotime('2009-08-18');
$week2 = strtotime('2009-08-25');
$week3 = strtotime('2009-09-01');
$week4 = strtotime('2009-09-08');
$week5 = strtotime('2009-09-15');
$week6 = strtotime('2009-09-22');
$week7 = strtotime('2009-09-29');
$week8 = strtotime('2009-10-06');
$week9 = strtotime('2009-10-13');
$week10 = strtotime('2009-10-20');

$weeks_array = array($preseason, $week1, $week2, $week3, $week4, $week5, $week6, $week7, $week8, $week9, $week10);

$today = strtotime(date(Y-m-d, time()));

$poll_week = '';

if ($today < $weeks_array[0]) {
$poll_week = 'Pre-Season';
}

if ($today > $weeks_array[10]) {
$poll_week = 'Closed';
}

if ($poll_week == '') {
for ($i=0; $i<12; $i++) {
	if ($today >= $week_array[$i] && $today < $week_array[$i+1]) {
		$poll_week = 'Week ' . $i;
	}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/169936-dates-in-an-array/
Share on other sites

I have fixed sytax errors and wrong variable referencing as below:

<?php

$preseason = strtotime('2009-08-11');
$week1 = strtotime('2009-08-18');
$week2 = strtotime('2009-08-25');
$week3 = strtotime('2009-09-01');
$week4 = strtotime('2009-09-08');
$week5 = strtotime('2009-09-15');
$week6 = strtotime('2009-09-22');
$week7 = strtotime('2009-09-29');
$week8 = strtotime('2009-10-06');
$week9 = strtotime('2009-10-13');
$week10 = strtotime('2009-10-20');

$weeks_array = array($preseason, $week1, $week2, $week3, $week4, $week5, $week6, $week7, $week8, $week9, $week10);
print_r($weeks_array);
$today = strtotime(date('Y-m-d', time()));
echo $today;

$poll_week = '';

if ($today < $weeks_array[0]) {
$poll_week = 'Pre-Season';
}

if ($today > $weeks_array[10]) {
$poll_week = 'Closed';
}

if ($poll_week == '') {
for ($i=0; $i<count($weeks_array); $i++) {
	if ($today >= $weeks_array[$i] && $today < $weeks_array[$i+1]) {
		$poll_week = 'Week ' . $i;
	}
}
}
echo $poll_week;
?>










Link to comment
https://forums.phpfreaks.com/topic/169936-dates-in-an-array/#findComment-896469
Share on other sites

Just to let you know it was missing quotes but also look at how you were calling the array here:

 

if ($today >= $week_array[$i] && $today < $week_array[$i+1]) {

 

As I had to change it to "$weeks_array" (notice the 's') as it should have been. easy mistake to make but just thought I'd point it out.

 

:-)

Link to comment
https://forums.phpfreaks.com/topic/169936-dates-in-an-array/#findComment-897049
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.