Forum Moderators: open

Message Too Old, No Replies

Reload same page with a parameter

         

CaNNibuZZ

1:46 am on Sep 13, 2010 (gmt 0)

10+ Year Member



Hi, is there a way to reload the same page with a parameter passed using ajax? So if I am on a page called www.watever.com/time.php, I would like it reloaded as www.watever.com/time.php?timezone=08:00

I am trying to get a guest visitors timezone using javascript, then reload the same page so that php can access the value of the variable.

This javascript code does sort of what I want -

<script language="JavaScript">
var d = new Date()
var timezone = -d.getTimezoneOffset()/60;
location.href="time.php?timezone=" + timezone;
</script>


So after the first reload of course I can echo the timezone in the time.php page now, but of course the page keeps looping/refreshing every time that piece of javascript is loaded so that's no good. I would also prefer the user didnt see a page refresh at all if possible, hence the reason I am looking at ajax to try to accomplish this.

Anyway I am completely new to ajax so any assistance would be very much appreciated, thanks.

Fotiman

1:21 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your PHP code, maybe you could only include this script code if the timezone was not passed in.

As for an AJAX solution, if you use jQuery you could do something like this:


var d = new Date()
var timezone = -d.getTimezoneOffset()/60;
$.get('time.php', {timezone: timezone}, function(data) {
// data contains the result of the ajax request
});

CaNNibuZZ

2:46 am on Sep 14, 2010 (gmt 0)

10+ Year Member



Thanks for the reply. Yes i'm using jQuery. I put the javascript in the header of my page and then tried to use
echo $_GET['timezone']
but it just gives me an 'undefined index' error.

To explain this problem a bit more, I am trying to do this in phpBB external page. So it's a .php page that inserts values into a corresponding .html template.

I have a timezone.php that finds out a logged in users timezone through the phpBB database (that part is easy to get $timezone = $user->timezone + $user->dst;) and it displays that time through using the timezone.html page for display outlay. timezone.html includes separate header.html and footer.html files. The header is where I am putting the javascript at the moment.

The problem is with getting a guest (not logged in) timezone. That value isn't in the phpBB DB of course so I need to find it another way. Apparently it can't be done using PHP so I have to try something else like a javascript solution.

I am open to any solution to this if someone can think of something eg use cgi or whatever. You would think there would be an easy way like use a $_SERVER attribute or something eg. $_SERVER[USER_TIME_OFFSET].

It's crazy hard for such a seemingly simple task.

Fotiman

1:44 pm on Sep 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure you use isset before trying to access the $_GET['timezone']

if (!isset($_GET['timezone'])) {
// $_GET['timezone]'] contains the timezone
}
else {
// no timezone
}

CaNNibuZZ

1:21 am on Sep 19, 2010 (gmt 0)

10+ Year Member



yes i used isset but that doesnt really help much because if it's not set then it defeats the purpose. When would it ever be set then if it isn't now? Anyway I am still searching for a solution, I can't believe how difficult this is. someone suggested i do something like this in my php file -

$time_offset  = "<script type='text/javascript'>
// <![CDATA[
function TimezoneDetect(){
var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
var intOffset = 10000; //set initial offset high so it is adjusted on the first attempt
var intMonth;
var intHoursUtc;
var intHours;
var intDaysMultiplyBy;

//go through each month to find the lowest offset to account for DST
for (intMonth=0;intMonth < 12;intMonth++){
//go to the next month
dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);

//To ignore daylight saving time look for the lowest offset.
//Since, during DST, the clock moves forward, it will be a bigger number.
if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
intOffset = (dtDate.getTimezoneOffset() * (-1));
}
}

return intOffset;
}

//Find start and end of DST
function DstDetect(){
var dtDstDetect = new Date();
var dtDstStart = '';
var dtDstEnd = '';
var dtDstStartHold = ''; //Temp date hold
var intYearDayCount = 732; //366 (include leap year) * 2 (for two years)
var intHourOfYear = 1;
var intDayOfYear;
var intOffset = TimezoneDetect(); //Custom function. Make sure you include it.

//Start from a year ago to make sure we include any previously starting DST
dtDstDetect = new Date()
dtDstDetect.setUTCFullYear(dtDstDetect.getUTCFullYear() - 1);
dtDstDetect.setUTCHours(0,0,0,0);

//Going hour by hour through the year will detect DST with shorter code but that could result in 8760
//FOR loops and several seconds of script execution time. Longer code narrows this down a little.
//Go one day at a time and find out approx time of DST and if there even is DST on this computer.
//Also need to make sure we catch the most current start and end cycle.
for(intDayOfYear = 1; intDayOfYear <= intYearDayCount; intDayOfYear++){
dtDstDetect.setUTCDate(dtDstDetect.getUTCDate() + 1);

if ((dtDstDetect.getTimezoneOffset() * (-1)) != intOffset && dtDstStartHold == ''){
dtDstStartHold = new Date(dtDstDetect);
}
if ((dtDstDetect.getTimezoneOffset() * (-1)) == intOffset && dtDstStartHold != ''){
dtDstStart = new Date(dtDstStartHold);
dtDstEnd = new Date(dtDstDetect);
dtDstStartHold = '';

//DST is being used in this timezone. Narrow the time down to the exact hour the change happens
//Remove 48 hours (a few extra to be on safe side) from the start/end date and find the exact change point
//Go hour by hour until a change in the timezone offset is detected.
dtDstStart.setUTCHours(dtDstStart.getUTCHours() - 48);
dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() - 48);

//First find when DST starts
for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
dtDstStart.setUTCHours(dtDstStart.getUTCHours() + 1);

//If we found it then exit the loop. dtDstStart will have the correct value left in it.
if ((dtDstStart.getTimezoneOffset() * (-1)) != intOffset){
break;
}
}

//Now find out when DST ends
for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() + 1);

//If we found it then exit the loop. dtDstEnd will have the correct value left in it.
if ((dtDstEnd.getTimezoneOffset() * (-1)) != (intOffset + 60)){
break;
}
}

//Check if DST is currently on for this time frame. If it is then return these values.
//If not then keep going. The function will either return the last values collected
//or another value that is currently in effect
if ((new Date()).getTime() >= dtDstStart.getTime() && (new Date()).getTime() <= dtDstEnd.getTime()){
return new Array(dtDstStart,dtDstEnd);
}

}
}
return new Array(dtDstStart,dtDstEnd);
}

if (DstDetect())
{
document.write(TimezoneDetect() + 60);
}
else
{
document.write(TimezoneDetect());
}
// ]]>
</script>";

$zero = 0;
$timenow = date("D d M Y H:i:s");
$tserver = strtotime($timenow, $zero);
$timenow .= " UTC";
$tutc = strtotime($timenow, $zero);
$diff = ($tserver - $tutc)/60;
$t_offset = $diff - $time_offset;


I know it's not valid html doing it that way but at least it gives me the number I am looking for. But the problem is, I can echo the number, so echo $time_offset will print say 600 to my browser. I thought that was the solution until I tried to do something with that value. Like cast it to an integer, or times it by 1 all give me a result of '0'.

istype says that the returned value is a string, it won't cast to an integer without losing the value so i tried a substr to see what was going on, substr($time_offset,0,13) gives me a value of '<script type=' which is of course the start of the javascript assigned to that variable.

So my question is, can something be done with this returned javascript object or whatever it is so I can turn it into a useable value. You would think (int)$time_zone would do it but instead it destroys the value because it's not being seen as a number, but instead is seen by php as a string of javascript code.

Is this solution fail or not? It's not ajax but hey if it works it will do. Doesn't look promising though, just a dirty hack really.