Jump to content

Date & Time From .csv into Online Calendar


alexbeau10

Recommended Posts

Hi all,

 

I am currently working on a website for work and I am having a bit of difficulty with one part of it.  I am working on a calendar that displays the times of people shifts at work in an iframe.  The calendar is generated by some php and and the shift definitions and times are read from .csv file.  The problem I am having is with shifts that end at midnight/12am.  In the php there is a spot were you define the time that the calendar will display and I set it for 09:00 to 00:00 I do not get legible outputs.  I currently have set the shifts in the .csv file to end at 23:59 instead of 00:00. 

 

This is the php file that is read by the website and outputs the code for the iframe. 

 

<link rel=stylesheet type=text/css href=schedule.css />
<body class='schedule'>

<?php 

   include('AlexUScheduleMaker.php');
   $sm = new ScheduleMaker(1100, 600, 10, 20, 15, '','','','','','',''); //(width,height,left offset, top offset, interior padding)
   $sm->setBeginEndTime("09:00", "23:59"); 	// start/end time for schedule in 24hr 
time.
   											// can be changed for non 24hr sites. eg 9:00-17:00

$file_handle = fopen("alexu.csv", "r");

$i=0;
$shiftarray=array();

while (!feof($file_handle) ) {
	$shiftline = fgetcsv($file_handle, 1024);
	$shiftarray[$i] = $shiftline;
	$i++;
}

$numberofshifts = count($shiftarray);

for($j=0;$j<$numberofshifts;$j++) {
	$shift = new Shift($shiftarray[$j][0], $shiftarray[$j][1], $shiftarray[$j][2], $shiftarray[$j][3]);
	$sm->addShift($shift);
}		

$sm->printHTML();

fclose($file_handle);

?>


</body>

 

This is where the shift columns and such are formed from:

<?php

   //class which creates a schedule in html
   //holds a collection of Event objects (defined below)
   class ScheduleMaker
   {
      //member variables
      
      var $width; //total width of schedule
      var $height; //total height of schedule
      var $left; //left offset of schedule
      var $top; //top offset of schedule
      var $beginTime;
      var $endTime;
      var $shifts = array();
      var $padding; //side padding of schedule 
      var $shifttype = shift;
      var $mynetid;
            
      var $title1;
      var $title2;
      var $title3;
      var $title4;
      var $title5;
  var $title6;
      var $title7;

      //constructor
      //$sm = new ScheduleMaker(100,100,100,100,5);
      function ScheduleMaker($width=0, $height=0, $left=0, $top=0, $padding=5, $title1, $title2,$title3,$title4,$title5,$title6,$title7)
      {
         $this->width = $width;
         $this->height = $height;
         $this->left = $left;
         $this->top = $top;
         $this->padding = $padding;
         
         $this->title1 = $title1;
      	 $this->title2 = $title2;
   		 $this->title3 = $title3;
  		 $this->title4 = $title4;
         $this->title5 = $title5;
         $this->title6 = $title6;
         $this->title7 = $title7;
      }

      //24hr time
      function setBeginEndTime($begin, $end)
      {
         $this->beginTime = $begin;
         $this->endTime = $end;
      }


      //add shifts
      function addShift($shift)
      {
         array_push($this->shifts, $shift);
      }

      //prints to HTML
      function printHTML()
      {
         $w = $this->width;
         $h = $this->height;
         $l = $this->left;
         $t = $this->top;
         
	$title1 = $this->title1;
        $title2 = $this->title2;
        $title3 = $this->title3;
        $title4 = $this->title4;
        $title5 = $this->title5;
        $title6 = $this->title6;
        $title7 = $this->title7;         

         //total widths 
         $totalWidth = $w - $this->padding*6;
         $totalHeight = $h - $this->padding*2;

         $totalTime = TimeToNumber($this->endTime) - TimeToNumber($this->beginTime);

         //widths of things
         $colwidth = $totalWidth * 0.14;
         //$colwidth2 = $totalWidth * 0.2 + 1;
         //$colwidth3 = $totalWidth * 0.3 + 2;
         $halfpadding = $this->padding * 0.5;
         
         //locations for columns
         
         $starts[1] = $l + $this->padding;
         $ends[1] = $starts[1] + $colwidth;
         
         $starts[2] = $ends[1] + $this->padding;
         $ends[2] = $starts[2] + $colwidth;
         
         $starts[3] = $ends[2] + $this->padding;
         $ends[3] = $starts[3] + $colwidth;
         
         $starts[4] = $ends[3] + $this->padding;
         $ends[4] = $starts[4] + $colwidth;
         
         $starts[5] = $ends[4] + $this->padding;
         $ends[5] = $starts[5] + $colwidth;
         
         $starts[6] = $ends[5] + $this->padding;
         $ends[6] = $starts[6] + $colwidth;
         
         $starts[7] = $ends[6] + $this->padding;
         $ends[7] = $starts[7] + $colwidth;         
         
         $tippytop = $t - 15;
         
         echo "<div class='background' style='position:absolute left:${l}px; top:${t}px; height:${h}px width:${w}px'>\n";
         
         
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[1]px; top:${tippytop}px; width:${colwidth}px'>Monday ${title1}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[2]px; top:${tippytop}px; width:${colwidth}px'>Tuesday ${title2}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[3]px; top:${tippytop}px; width:${colwidth}px'>Wednesday ${title3}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[4]px; top:${tippytop}px; width:${colwidth}px'>Thursday ${title4}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[5]px; top:${tippytop}px; width:${colwidth}px'>Friday ${title5}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[6]px; top:${tippytop}px; width:${colwidth}px'>Saturday ${title6}</div>\n";
         echo "<div class='weeklabel' align=center style='position:absolute; left:$starts[7]px; top:${tippytop}px; width:${colwidth}px'>Sunday ${title7}</div>\n";
         
         
         //looks at every column that could hold shifts
         
         foreach( $this->shifts as $shift )
         {
            for($i = 1; $i <= 7; $i++)
            {
               $startTime = TimeToNumber($shift->startTime);
               $endTime = TimeToNumber($shift->endTime);
               $interval = TimeToNumber($endTime) - TimeToNumber($startTime);
               $top = $t + $this->padding + (TimeToNumber($startTime) - TimeToNumber($this->beginTime)) * $totalHeight / $totalTime;
               $height = $totalHeight * $interval / $totalTime; 
               $netid = $shift->netid;
               
               $startTime = TimeToAMPM($shift->startTime);
               $endTime = TimeToAMPM($shift->endTime);

			//spits out html for each shift per column



			$mynetid = $_POST["mynetid"];

			if(stristr($netid,"open")||(!$netid)||stristr($netid," "))
			{
				$shifttype = openshift;
			} else if(stristr($netid,"${mynetid}"))
			{
				$shifttype = myshift;
			} else
			{
				$shifttype = shift;
			}
               				
               
               if(preg_match("/$i/", $shift->days) == 1)
               {
              
               
               //Write nested if statement
               //if startTime>endTime...
               //print html in two divs
               
         			if((TimeToNumber($shift->endTime)) < (TimeToNumber($shift->startTime)))
        			{
        			$heighta = /*$totalHeight -*/ ((24.00 - TimeToNumber($shift->startTime)) / 24)*$totalHeight; //part of shift in first day
        			
        			
        			$html = "<div class='${shifttype}' align=center style='position:absolute; left:$starts[$i]px; top:${top}px; width:${colwidth}px; height:${heighta}px; z-index:$z'>\n";
                 	$z++;
                  	$html .= "$startTime - $endTime<br><b>$netid</b><br>(cont..)";
                  	$html .= "</div>\n";
                  	echo $html; //print this part
                  	
                  	$j = $i+10; //goes to next day of week
                  	$verytop = $t + $this->padding; //represents beginning a new day
                  	$heightb = ((TimeToNumber($shift->endTime)) / 24) * $totalHeight; //part of shift next day
                  	
        			$html = "<div class='${shifttype}' align=center style='position:absolute; left:$starts[$j]px; top:${verytop}px; width:${colwidth}px; height:${heightb}px; z-index:$z'>\n";
                 	//$z++;
                  	$html .= "$startTime - $endTime<br><b>$netid</b>";
                  	$html .= "</div>\n";
                  	echo $html;//print this part
                  	
                  	
        			}else
        			{
             
               
               //else...
               
               // ...this is going to be fun...
               // woot. it works!
               
               	
                  $html = "<div class='${shifttype}' align=center style='position:absolute; left:$starts[$i]px; top:${top}px; width:${colwidth}px; height:${height}px; z-index:$z'>\n";
                  $z++;
                  $html .= "$startTime - $endTime<br><b>$netid</b>";
                  $html .= "</div>\n";
                  echo $html;
                
                  	}
               }
            } 
         }
         echo "</div>\n";
      }

   }


   class shift
   {
      var $days; //form of day/shift. eg Thursday 1st column -> "41", Sunday 2nd column -> "72"
      var $startTime; //format "hour:min" in 24hr time
      var $endTime; //24hr
      var $netid;  //consultant netid
            
      function shift( $netid, $days, $start, $end )
      {
         $this->days = $days;
         $this->startTime = $start;
         $this->endTime = $end;
         $this->netid = $netid;
      }
   }

   //converts time to a number
   
   function TimeToNumber($time)
   {
      list($hour, $minute) = split(":", $time, 2);
      $new_time = $hour + $minute/60.0;
      return $new_time;
   }

   //converts 24hr to 12hr time string
   
   function TimeToAMPM($time)
   {
      list($hour, $minute) = split(":", $time, 2);

         
      if($hour < 12)
      {
         if($hour == 0) // this part isn't working.. i want it to. Make it work. -> to do.
         				// woot. I fixed it. Awesome...
         {
            $hour = 12;
         }

         $new_time = "$hour:$minute AM";
         return $new_time;
      }
      else
      {
         if($hour != 12)
         {
            $hour -= 12;
         }
         
         $new_time =  "$hour:$minute PM";
         return $new_time;
      }
   }
?>

 

I can include a sample .csv file if needed or any additional info.

 

Any help is greatly appreciated with this.

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/176188-date-time-from-csv-into-online-calendar/
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.