After much thought and consideration about the whole process and what needs to be done. I then wanted to start testing to see how this might start to look in the web app. This post will cover some of the thought and testing process I used to work through how the process will work when it is finished.

Firstly I know that all classes that have been created so far are all in Australia/Brisbane timezone. So I know the offset for Brisbane is 36000 (+ 10 Hours). Eventually this will be stored in the database but for now I can store it as a static variable in the configuration file like this

$default_offset = 36000;

I then need to work out the current time zone offset from UTC for the calculations. I can do this using the date command

$tzoffset = date("Z");

Then I want to workout the difference between the class start date when it was originally created and the current time zone offset (i.e. to account for DST etc). This may return a negative number which is OK.

$tzdiff = $tzoffset - $default_offset;

Then for troubleshooting, I wanted to Display some info at the top of the pages while I worked through these issues.

echo date("g:ia d-m-Y") ." : Offset: $tzoffset : Difference: $tzdiff";

Next I needed to work out how to apply this to the site. What I needed to achieve is that what ever timezone I was in. I needed the date to return midnight of that day. (So if I was in Brisbane, It would need to return a unix time string equal to 29th June 2015 00:00:00, If I changed the time zone to Auckland it needed to adjust itself to the same time of 29th June 2015 00:00:00, Also if the time zone went into a negative zone i.e. GMT -10, it needed to return the same but for that timezone)

So while the date stays the same, the unix time string changes based on the offset of the time zone. The maths needed to make that calculation.

So I added the following code to one of the pages.

//Adjust the time and store as adjusted_date
$adjusted_date = $row['date_of_class'] - $tzdiff;
//Echo some diag info for calculations.
echo $row['date_of_class'] .":" .$adjusted_date;

Firstly I take away the difference (be it positive or negative) from the record in the database. (So if you take a negative you add to the value). This then gives me a value that reflects the unix time string I need to accurately display the information.

Lastly I update the displaying record

<? echo date("l jS F Y",$row['date_of_class']); ?>

<? echo date("l jS F Y",$adjusted_date); ?>

Which sets the time stamp displayed correctly as I change through different time zones. After all that testing I can write the following function to process this regularly.

	* getTZadjustment will check the time offset against the info stored in the database and return 
	* an adjusted value that will allow me to compensate for a change in timezone offset. (i.e. DST)
	* @original_time - Original time as stored in the database.
	* @stored_offset = Offset as stored in the database
	* @@RETURN adjusted_date = date as unix time string adjusted for the difference in offset. 
	function getTZadjustment($original_time, $stored_offset) {
		##Work out current offset
		$tzoffset = date("Z");
		##Work out the difference between the stored value and the current offset
		$tzdiff = $tzoffset - $stored_offset;
		##Work out the adjusted time string
		$adjusted_date = $original_time - $tzdiff;
		##Return string
		return $adjusted_date;
	} //getTZadjustment