Ok, well lets cut to the chase. I am trying to make a function which alters the timestamp according to what a user specifies through their control panel (+-GMT).
I have managed to work something to accurately change the time if they live in a time zone which is + GMT or GMT.
Eg: i have a message which is dated 21:55. The function can add whatever it likes to the hour and alter the day/month if need be.
However if the user is -GMT then im stuck as it gives me all sorts of funny times.
The messages are stored with their timestamps asfollows
day | month | year | hour | min |
and the user account has 'timezone' which is the adjustment value.
Here is my code so far. Any crit to improve would be nice and also how i could do the negative adjustment would also be good. Take your time everyone i realsie its a longer piece of code. But tis heavily commented so it should be fairly easy to work out what each part does.
$query = "SELECT time_format, timezone FROM userinfo WHERE username='".$_SESSION['SPuserID']."'";
//Use while objects in case we query from within a loop.
$SQL->query_db_while($query);
$tformat = $SQL->fetch_row_while();
//Calculate the time difference on server time.
$nhour = $hour+$tformat['timezone'];
//If alteration is positive
if ($nhour >= 24) {
//Hour has increased to a level above that of 24:00.
//Work out by how much and alter time accordingly.
$dif = $nhour-24;
if ($dif < 10) {
$hour = "0$dif";
}
//That was fun, now alter the date to suit.
$day++;
//BUT WAIT! if date is last in month we have to alter the date back to 1 AND the month plus 1.
if ($day == 31){
if (($month == 2)||($month == 4)||($month == 6)||($month == 9)||($month == 11)) {
//These months only have 30 days.
$day = "01";
$month++;
if ($month == 13) {
$month = "01";
$year++;
}
}
} elseif(($day == 29)&&($month == 02)&&(!date("L",mktime(0,0,0,1,1,$year)))){
//It is the 29th of Feb and the year is not a Leap year.
//It is now march the 1st
$month++;
$day = "01";
}
//If alteration is negative
} else if ($nhour<0){
//This is where I want to work out the time when the adjustment is negative.
} else {
$hour = $nhour;
}