views:

26

answers:

2

Hi Guys,

I have a table with data exactly like this:

  ID  Heading       Description  Time       Location   Start_Date  End_Date
  63  Wed Serv      Small group  19:00:00   null       2010-10-13  null
  70  Fall Harvest  Calvary      null       Pepin, Wi  2010-10-30  null

My Calendar shows the events on the correct dates, but the time for both events is 7:00 pm and I can't figure out why. For the one event the time is null but it's still saying 7:00 pm on the calendar.

Take a look at the variable called $time

My script is this:

<?php

//if the current page is empty, set the date variable to the current time, else set the date variable to the currentpage
$date = empty($_GET['currentpage']) ? time() : strtotime($_GET['currentpage']);

//This puts the day, month, and year in separate variables
$day = date('d', $date);
$month = date('m', $date);
$year = date('y', $date);
$bigYear = date('Y', $date);
$first_day = mktime(0, 0, 0, $month, 1, $year);             //we need to generate the first day of the month
$month_name = date('F', $first_day);                        //get the current month's full spelling
$day_of_week = date('D', $first_day);                       //find out what day of the week the first day of the month falls on
$prevM = getDate(mktime(0, 0, 0, $month-1, 1, $year));      //gets an associative array of the previous month
$nextM = getDate(mktime(0, 0, 0, $month+1, 1, $year));      //gets an associative array of the next month                           
$prevMonth = "{$prevM['year']}-{$prevM['mon']}";            //gets the actual previous month's name. Subtracts the year from the month so you're not stuck in the current year.
$nextMonth = "{$nextM['year']}-{$nextM['mon']}";            //gets the actual next month's name. Subtracts the year from the month so you're not stuck in the current year.
$day_count = 1;                                             //counts the days up to 7 so we know when to start a new week
$day_num = 1;                                               //counter for the total number of days in the month
$dates = new dates();                                       //instantiate the dates object

//once we know what day of the week it falls on, we know how many blank days before it occur.
switch($day_of_week) {
    case "Sun": $blank = 0; break;
    case "Mon": $blank = 1; break;
    case "Tue": $blank = 2; break;
    case "Wed": $blank = 3; break;
    case "Thu": $blank = 4; break;
    case "Fri": $blank = 5; break;
    case "Sat": $blank = 6; break;
}

//then we need to determine how many days are in the month
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads 
echo "<table>".PHP_EOL;
echo "\t\t\t\t<thead>".PHP_EOL;
echo "\t\t\t\t\t<tr>".PHP_EOL;
echo "\t\t\t\t\t\t<th colspan=2 class=\"noBorder\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevMonth'>&laquo;</a></th>";
echo "\t\t\t\t\t\t<th colspan=3 class=\"noBorder\"><strong>$month_name"." "."$bigYear</strong></th>";
echo "\t\t\t\t\t\t<th colspan=2 class=\"noBorder\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextMonth'>&raquo;</a></th>";
echo "\t\t\t\t\t</tr>".PHP_EOL;
echo "\t\t\t\t\t<tr>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Sun</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Mon</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Tue</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Wed</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Thu</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Fri</th>".PHP_EOL;
echo "\t\t\t\t\t\t<th>Sat</th>".PHP_EOL;
echo "\t\t\t\t\t</tr>".PHP_EOL;
echo "\t\t\t\t</thead>".PHP_EOL;
echo "\t\t\t\t<tbody>".PHP_EOL;
echo "\t\t\t\t\t<tr>".PHP_EOL;

//first we take care of those blank days
while($blank > 0) 
{
    echo "\t\t\t\t\t\t<td>&nbsp;</td>".PHP_EOL;
    $blank = $blank-1;
    $day_count++;
}

//while we're not at the last day of the month...
while($day_num <= $days_in_month) 
{           
    //get the events from the table
    foreach($dates->getRows() as $results) 
{   
//parse the date field
$eventStartYear = substr($results['start_date'], 0, 4);
$eventStartMonth = substr($results['start_date'], 5, 2);
$eventStartDay = substr($results['start_date'], 8, 2);
$eventEndYear = substr($results['end_date'], 0, 4);
$eventEndMonth = substr($results['end_date'], 5, 2);
$eventEndDay = substr($results['end_date'], 8, 2);
$heading = $results['heading'];
$description = $results['description'];
$time = strftime("%l:%M %P", $results['time']);


//if the event fields are not null and the event day is equal to the day counter, and the event month and year are equal to the current month
// AND it is a multiple day event
if(!is_null($eventStartDay) && !is_null($eventStartMonth) && !is_null($bigYear) && !is_null($eventEndDay) && !is_null($eventEndMonth) && !is_null($eventEndYear) && $day_num == $eventStartDay && $month == $eventStartMonth && $bigYear == $eventStartYear)
{   
    $currentDay = $eventStartDay;

    //while the current day is less than or equal to the end day of the event
    while($currentDay <= $eventEndDay)
    {
        //echo "id = ".$id."<br />";
        //echo "time = ".$time."<br />";
        //echo "time2 = ".$time2."<br />";
        echo "\t\t\t\t\t\t<td><a href=\"displayEvent.php?id=$id\">$day_num<h4>".$heading."</h4><h5>".$time."</h5></a></td>".PHP_EOL;
        $currentDay++;
        $day_num++;
        $day_count++;
    }
 }

 if(!is_null($eventStartDay) && !is_null($eventStartMonth) && !is_null($bigYear) && $day_num == $eventStartDay && $month == $eventStartMonth && $bigYear == $eventStartYear)
 {
    //echo "id = ".$id."<br />";
    //echo "time = ".$time."<br />";
    //echo "time2 = ".$time2."<br />";
    echo "\t\t\t\t\t\t<td><a href=\"displayEvent.php?id=$id\">$day_num<h4>".$heading."</h4><h5>".$time."</h5></a></td>".PHP_EOL;
    $day_num++;
    $day_count++;

 }

 //make sure we start a new row every week
 if($day_count > 7) 
 {
     echo "\t\t\t\t\t</tr>".PHP_EOL;
     echo "\t\t\t\t\t<tr>".PHP_EOL;
     $day_count = 1;
 }

}

                        //print days that don't have events
                        echo "\t\t\t\t\t\t<td><a href=\"noEvent.php\">$day_num</a></td>".PHP_EOL;
                        $day_count++;
                        $day_num++;

                        //make sure we start a new row every week
                        if($day_count > 7) 
                        {
                           echo "\t\t\t\t\t</tr>".PHP_EOL;
                           echo "\t\t\t\t\t<tr>".PHP_EOL;
                           $day_count = 1;
                        }
                    }

                    //finish the table with some blanks if needed
                    while($day_count > 1 && $day_count <= 7) {
                        echo "\t\t\t\t\t\t<td>&nbsp;</td>".PHP_EOL;
                        $day_count++;
                    }

                    echo "\t\t\t\t\t</tr>".PHP_EOL;
                    echo "\t\t\t\t</tbody>".PHP_EOL;
                    echo "\t\t\t</table>".PHP_EOL;

                ?>
+1  A: 

What happens when you pass NULL to strftime()? You might need to wrap an if statement around that assignment and use an empty string for null times.

If that's not the problem, I would suggest you dump your $results['time'] value to make sure it's set to null when you think it is.

Don Kirkby
+1  A: 

The problem is with the $time = strftime("%l:%M %P", $results['time']); line. If you pass strftime a null value, it will default to 12:00am UTC, which in your timezone will be 7:00pm. This is why both records show up as 7:00pm. You should add some logic to set a default time to show up for records with unspecified times.

For example:

$time = $results['time'] != null ? strftime("%l:%M %P", $results['time']) : "";
thetaiko
I have this line in the header of my page `<?php date_default_timezone_set('America/Detroit'); ?>` Should i not put this at the top but rather in an if statement if the $time variable is not null?
Catfish
What do you want it to show if the time is `null`? The example above should display "12:00 am"
thetaiko
I want no time to show then so i just `$time = "";`if the result['time'] is null.
Catfish
@Catfish - edited the example in my code for you.
thetaiko