Jump to content

I don't understand my own function that I wrote a while back!


Recommended Posts

My first time on the phpfreaks forum, and thanks in advance for any help I might get here.

 

A buddy of mine wrote some code for me a while back - my PHP is rusty (you could say it was never shiny), and I'm struggling to understand what's going on so I can enhance it.

 

This code powers http://nextmanlyferry.com. The whole code suite is available in google code - http://code.google.com/p/php-timetable-app/

 

In a nutshell, the function reads through a text file (name based on the current day of the week) which contains a series of times (events) on new lines e.g. 1930, 2000, 2030 etc. The current time is compared with the event times in the file, and outputs 3 variables - $inboundprevEventTime, $inboundEventTime and $inboundnextEventTime. The first is the event time that's just passed, the next the event time that is next, and the last the event time that is next + 1. There's also some checks to read into the next and previous day's times if it's the tail end of beginning of the day.

 

I set up some config variables such as hour offset from UTC in config.php, and some time variables such as $date in timevariables.php.

 

I want to enhance this to allow me to output the event times coming within the next hour (preferred) or the next four events.

 

I'd imagine you might need to ask some more questions to help... but I'd be really grateful if someone could help me on this one.

 

Thanks in advance....

 

<?php

require("config.php");

inboundeventTime($hoursfromUTC,$inboundprevEventTime,$inboundEventTime,$inboundnextEventTime);


function inboundeventTime($offset,&$inboundprevEventTime,&$inboundEventTime,&$inboundnextEventTime)

{
   	$inboundEventTime=$inboundprevEventTime=$inboundnextEventTime=0; 
$txtFolder="inboundtimes/";

require("timevariables.php");

//read today
$lines=array_map("trim",file($txtFolder."{$date}.txt"));
sort($lines);
if(!$lines)return;

//read tomorrow
$linesNext=array_map("trim",file($txtFolder."{$dateNext}.txt"));
sort($linesNext);
if(!$linesNext)return 0;

//read yesterday
$linesPrev=array_map("trim",file($txtFolder."{$datePrev}.txt"));
sort($linesPrev);
if(!$linesPrev)return 0;

for($i=0;$i<count($lines);$i++)
{
	if($lines[$i]>$time) 
	{
		//first event for day
	    if($i==0)
			$inboundprevEventTime=$linesPrev[count($linesPrev)-1]; //take from prev day
	    else
			$inboundprevEventTime=$lines[$i-1];

	    $inboundEventTime=$lines[$i];
	    
		//last event for day
	    if($i== (count($lines)-1)  )
			$inboundnextEventTime=$linesNext[0]; //take from next day
	    else
			$inboundnextEventTime=$lines[$i+1];
		return;
	}
}

//default first event from nextday
    $inboundprevEventTime=$lines[count($lines)-1];
    $inboundEventTime=$linesNext[0];
    $inboundnextEventTime=$linesNext[1];

}

?>




Hey bundy - that's correct. The times are in a Monday.txt file for example. I set it up in that way for ease of managing the times without having to manage a database. Everything is stored (and there's not much to store) in flat files for that reason. It also allows the lay-person to update the timings without knowledge of a database, setting up admin screens, etc.

 

Not sure how I would move all times into one file and be able to read them out differently for different days? E.g. saturday times are different to weekday times.

 

 

Well, your file could look this this:

 

[MON/1830/Event1]
[MON/0700/Event2]
[TUE/1200/Event3]
[WED/0000/Event4]
[THU/2234/Event5]
[FRI/1100/Event6]
[sAT/0333/Event7]
[sUN/0001/Event8]
[FRI/2359/Event9]
[WED/2359/Event10]
[sUN/2359/Event11]

OK - but I'm not seeing how that helps my current issue?

 

Ideally I would like to setup another couple of variables:-

 

$inboundEventTime=$lines[$i]; //this already exists
$inboundnextEventTime=$lines[$i+1]; //this also already exists and works
$inboundEventTime3=$lines[$i+2]; // these next two are new
$inboundEventTime4=$lines[$i+3]; 

 

But whatever way I try and set those up I can't get those two new variables to return anything.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.