Jump to content

[SOLVED] php/javascript time?? or system/server time??


DonPatricio

Recommended Posts

hi,

i got basic knowledge about php and javascript. however i have no clue about date/time function in php and javascript.

 

what i want to do is to show the current time in my timezone. however, i want to display the CORRECT time and not the time on the computer on the bottom right. because everybody can change his system time and the WRONG time would be displayed. so, how can i display the CORRECT time of my pacific america time zone. is there a way to do that with the server time?? but i have no clue, please help me guys.

thanks a lot.

Link to comment
Share on other sites

You just need to create a function that takes two parametesr (the user's time-zone and a timestamp) and then modifies that according to the server's time-zone.

 

Say if your Server was EST (GMT - 5) and you were GMT... then you'd just do something like...

 

// 0 = GMT

$timestamp = time();

$mytimezone = 0;

timeZone( $mytimezone, $timestamp );

 

Then inside the function, for 0 you wanna add 18000 onto the timestamp... 3600 seconds = 1 Hour, 18000 = 5 Hours... so that would turn your server-time (GMT - 5) into GMT - 0...

 

I probably did a rubbish job of explaining that but I'm tired.

 

Maybe this function I made will give you an idea...

 

<?php
function timezone( $timestamp )
{
	switch( $_SESSION['mem_timezone'] )
	{
		// (GMT - 12 Hours) Enitwetok, Kwajalien
		case '-12';
		$this->output = $timestamp - 25200;
		break;

		// (GMT - 11 Hours) Midway Island, Samoa
		case '-11';
		$this->output = $timestamp - 21600;
		break;

		// (GMT - 10 Hours) Hawaii
		case '-10';
		$this->output = $timestamp - 18000;
		break;

		// (GMT - 9.5 Hours) French Polynesia
		case '-9.5';
		$this->output = $timestamp - 16200;
		break;

		// (GMT - 9 Hours) Alaska
		case '-9';
		$this->output = $timestamp - 14400;
		break;

		// (GMT - 8 Hours) Pacific Time (US & Canada)
		case '-8';
		$this->output = $timestamp - 10800;
		break;

		// (GMT - 7 Hours) Mountain Time (US & Canada)
		case '-7';
		$this->output = $timestamp - 7200;
		break;

		// (GMT - 6 Hours) Central Time (US & Canada), Mexico City
		case '-6';
		$this->output = $timestamp - 3600;
		break;

		// (GMT - 5 Hours) Eastern Time (US & Canada), Bogota, Lima
		case '-5';
		$this->output = $timestamp;
		break;

		// (GMT - 4 Hours) Atlantic Time (Canada), Caracas, La Paz
		case '-4';
		$this->output = $timestamp + 3600;
		break;

		// (GMT - 3.5 Hours) Newfoundland
		case '-3.5';
		$this->output = $timestamp + 5400;
		break;

		// (GMT - 3 Hours) Brazil, Buenos Aires, Falkland Is.
		case '-3';
		$this->output = $timestamp + 7200;
		break;

		// (GMT - 2 Hours) Mid-Atlantic, Ascention Is., St Helena
		case '-2';
		$this->output = $timestamp + 10800;
		break;

		// (GMT - 1 Hour) Azores, Cape Verde Islands
		case '-1';
		$this->output = $timestamp + 14400;
		break;

		// (GMT) Casablanca, Dublin, London, Lisbon, Monrovia
		case '0';
		$this->output = $timestamp + 18000;
		break;

		// (GMT + 1 Hour) Brussels, Copenhagen, Madrid, Paris
		case '1';
		$this->output = $timestamp + 21600;
		break;

		// (GMT + 2 Hours) Kaliningrad, South Africa
		case '2';
		$this->output = $timestamp + 25200;
		break;

		// (GMT + 3 Hours) Baghdad, Riyadh, Moscow, Nairobi
		case '3';
		$this->output = $timestamp + 28800;
		break;

		// (GMT + 3.5 Hours) Tehran
		case '3.5';
		$this->output = $timestamp + 30600;
		break;

		// (GMT + 4 Hours) Abu Dhabi, Baku, Muscat, Tbilisi
		case '4';
		$this->output = $timestamp + 32400;
		break;

		// (GMT + 4.5 Hours) Kabul
		case '4.5';
		$this->output = $timestamp + 34200;
		break;

		// (GMT + 5 Hours) Ekaterinburg, Karachi, Tashkent
		case '5';
		$this->output = $timestamp + 36000;
		break;

		// (GMT + 5.5 Hours) Bombay, Calcutta, Madras, New Delhi
		case '5.5';
		$this->output = $timestamp + 37800;
		break;

		// (GMT + 5.75 Hours) Kathmandu
		case '5.75';
		$this->output = $timestamp + 38700;
		break;

		// (GMT + 6 Hours) Almaty, Colombo, Dhaka
		case '6';
		$this->output = $timestamp + 39600;
		break;

		// (GMT + 6.5 Hours) Yangon, Naypyidaw, Bantam
		case '6.5';
		$this->output = $timestamp + 41400;
		break;

		// (GMT + 7 Hours) Bangkok, Hanoi, Jakarta
		case '7';
		$this->output = $timestamp + 43200;
		break;

		// (GMT + 8 Hours) Hong Kong, Perth, Singapore, Taipei
		case '8';
		$this->output = $timestamp + 46800;
		break;

		// (GMT + 8.75 Hours) Caiguna, Eucla
		case '8.75';
		$this->output = $timestamp + 49500;
		break;

		// (GMT + 9 Hours) Osaka, Sapporo, Seoul, Tokyo, Yakutsk
		case '9';
		$this->output = $timestamp + 50400;
		break;

		// (GMT + 9.5 Hours) Adelaide, Darwin
		case '9.5';
		$this->output = $timestamp + 52200;
		break;

		// (GMT + 10 Hours) Melbourne, Papua New Guinea, Sydney
		case '10';
		$this->output = $timestamp + 54000;
		break;

		// (GMT + 10.5 Hours) Lord Howe Island
		case '10.5';
		$this->output = $timestamp + 55800;
		break;

		// (GMT + 11 Hours) Magadan, New Caledonia, Solomon Is.
		case '11';
		$this->output = $timestamp + 57600;
		break;

		// (GMT + 11.5 Hours) Burnt Pine, Kingston
		case '11.5';
		$this->output = $timestamp + 59400;
		break;

		// (GMT + 12 Hours) Auckland, Fiji, Marshall Island
		case '12';
		$this->output = $timestamp + 61200;
		break;

		// (GMT + 12.75 Hours) Chatham Islands
		case '12.75';
		$this->output = $timestamp + 63900;
		break;

		// (GMT + 13 Hours) Kamchatka, Anadyr
		case '13';
		$this->output = $timestamp + 64800;
		break;

		// (GMT + 14 Hours) Kiritimati
		case '14';
		$this->output = $timestamp + 68400;
		break;

		// Default to Server time - (GMT - 5 Hours) Eastern Time (US & Canada), Bogota, Lima
		default:
		$this->output = $timestamp;
		break;
	}
	return $this->output;
}
?>

Link to comment
Share on other sites

Well, of course... that code is from within a class... it won't work if you just copy and paste it, that's not why I posted the code. I posted it to try and give you an idea of what you need to do.

 

If you have a basic knowledge of PHP, then you should be able to create a simple function to do this. I did all the Mathematics for you. :P

Link to comment
Share on other sites

EDIT: CODE tags removed as the syntax colourer messed up again.

 

It's only basic though, you just need to create a function that takes two paramaters...

 

Assuming your server's time-zone is EST...

 

Now if you want to use this, all you have to do is replace every instance of $this->output with $output.

 

Then you could do something like this:

 

<?php

// Get the current time-stamp...

$timestamp = time();

// Set the time-zone to GMT.

$timezone = '0';

timezone( $timezone, $timestamp );

echo( date('l jS M Y, g:i a', $output) );

?>

 

Here's the code: Just change every instance of $this->output to $output like I said.

 

<?php

function timezone( $timezone, $timestamp)

{

switch( $timezone )

{

// (GMT - 12 Hours) Enitwetok, Kwajalien

case '-12';

$this->output = $timestamp - 25200;

break;

 

// (GMT - 11 Hours) Midway Island, Samoa

case '-11';

$this->output = $timestamp - 21600;

break;

 

// (GMT - 10 Hours) Hawaii

case '-10';

$this->output = $timestamp - 18000;

break;

 

// (GMT - 9.5 Hours) French Polynesia

case '-9.5';

$this->output = $timestamp - 16200;

break;

 

// (GMT - 9 Hours) Alaska

case '-9';

$this->output = $timestamp - 14400;

break;

 

// (GMT - 8 Hours) Pacific Time (US & Canada)

case '-8';

$this->output = $timestamp - 10800;

break;

 

// (GMT - 7 Hours) Mountain Time (US & Canada)

case '-7';

$this->output = $timestamp - 7200;

break;

 

// (GMT - 6 Hours) Central Time (US & Canada), Mexico City

case '-6';

$this->output = $timestamp - 3600;

break;

 

// (GMT - 5 Hours) Eastern Time (US & Canada), Bogota, Lima

case '-5';

$this->output = $timestamp;

break;

 

// (GMT - 4 Hours) Atlantic Time (Canada), Caracas, La Paz

case '-4';

$this->output = $timestamp + 3600;

break;

 

// (GMT - 3.5 Hours) Newfoundland

case '-3.5';

$this->output = $timestamp + 5400;

break;

 

// (GMT - 3 Hours) Brazil, Buenos Aires, Falkland Is.

case '-3';

$this->output = $timestamp + 7200;

break;

 

// (GMT - 2 Hours) Mid-Atlantic, Ascention Is., St Helena

case '-2';

$this->output = $timestamp + 10800;

break;

 

// (GMT - 1 Hour) Azores, Cape Verde Islands

case '-1';

$this->output = $timestamp + 14400;

break;

 

// (GMT) Casablanca, Dublin, London, Lisbon, Monrovia

case '0';

$this->output = $timestamp + 18000;

break;

 

// (GMT + 1 Hour) Brussels, Copenhagen, Madrid, Paris

case '1';

$this->output = $timestamp + 21600;

break;

 

// (GMT + 2 Hours) Kaliningrad, South Africa

case '2';

$this->output = $timestamp + 25200;

break;

 

// (GMT + 3 Hours) Baghdad, Riyadh, Moscow, Nairobi

case '3';

$this->output = $timestamp + 28800;

break;

 

// (GMT + 3.5 Hours) Tehran

case '3.5';

$this->output = $timestamp + 30600;

break;

 

// (GMT + 4 Hours) Abu Dhabi, Baku, Muscat, Tbilisi

case '4';

$this->output = $timestamp + 32400;

break;

 

// (GMT + 4.5 Hours) Kabul

case '4.5';

$this->output = $timestamp + 34200;

break;

 

// (GMT + 5 Hours) Ekaterinburg, Karachi, Tashkent

case '5';

$this->output = $timestamp + 36000;

break;

 

// (GMT + 5.5 Hours) Bombay, Calcutta, Madras, New Delhi

case '5.5';

$this->output = $timestamp + 37800;

break;

 

// (GMT + 5.75 Hours) Kathmandu

case '5.75';

$this->output = $timestamp + 38700;

break;

 

// (GMT + 6 Hours) Almaty, Colombo, Dhaka

case '6';

$this->output = $timestamp + 39600;

break;

 

// (GMT + 6.5 Hours) Yangon, Naypyidaw, Bantam

case '6.5';

$this->output = $timestamp + 41400;

break;

 

// (GMT + 7 Hours) Bangkok, Hanoi, Jakarta

case '7';

$this->output = $timestamp + 43200;

break;

 

// (GMT + 8 Hours) Hong Kong, Perth, Singapore, Taipei

case '8';

$this->output = $timestamp + 46800;

break;

 

// (GMT + 8.75 Hours) Caiguna, Eucla

case '8.75';

$this->output = $timestamp + 49500;

break;

 

// (GMT + 9 Hours) Osaka, Sapporo, Seoul, Tokyo, Yakutsk

case '9';

$this->output = $timestamp + 50400;

break;

 

// (GMT + 9.5 Hours) Adelaide, Darwin

case '9.5';

$this->output = $timestamp + 52200;

break;

 

// (GMT + 10 Hours) Melbourne, Papua New Guinea, Sydney

case '10';

$this->output = $timestamp + 54000;

break;

 

// (GMT + 10.5 Hours) Lord Howe Island

case '10.5';

$this->output = $timestamp + 55800;

break;

 

// (GMT + 11 Hours) Magadan, New Caledonia, Solomon Is.

case '11';

$this->output = $timestamp + 57600;

break;

 

// (GMT + 11.5 Hours) Burnt Pine, Kingston

case '11.5';

$this->output = $timestamp + 59400;

break;

 

// (GMT + 12 Hours) Auckland, Fiji, Marshall Island

case '12';

$this->output = $timestamp + 61200;

break;

 

// (GMT + 12.75 Hours) Chatham Islands

case '12.75';

$this->output = $timestamp + 63900;

break;

 

// (GMT + 13 Hours) Kamchatka, Anadyr

case '13';

$this->output = $timestamp + 64800;

break;

 

// (GMT + 14 Hours) Kiritimati

case '14';

$this->output = $timestamp + 68400;

break;

 

// Default to Server time - (GMT - 5 Hours) Eastern Time (US & Canada), Bogota, Lima

default:

$this->output = $timestamp;

break;

}

return $output;

}

?>

Link to comment
Share on other sites

Let's try to sort a few things out.

 

The only place you can get your visitor's timezone is client-side with javascript and - as you point out - they could have mis-set the time.  And since the time on a visitor's computer has nothing to do with the time in your timezone, who cares what it is and whether it's right or wrong.

 

The time in your time zone (wherever you are) is always the same - server time plus or minus the timezone offset for your location relative to the server location. You don't need special functions or classes or switches to work that out; you just need simple math.

Link to comment
Share on other sites

The way I do things, is have a member set their time-zone somewhere in a User Control Panel... then this function just does the "simple mathematics" you spoke of.

 

It adds (or subtracts) x seconds from the time-stamp parameter and returns the new time-stamp.

 

There's probably a better way to do this, there's always a better way to do things, but it works. For me at least.

 

It took me like half an Hour to write that function too... couldn't find a Calculator. :D

Link to comment
Share on other sites

Thanks both of you.

 

The purpose of that is to program a PN-System like on this forum. When a message gets inserted into the db I really need the right time, because PN's from all over the world could be written and I need one standard time and that is my location. I hope I can figure all that out...

Thanks a lot

Link to comment
Share on other sites

Ok, one more thing though. I got this function now. That should be fine right now:

 

<?php

$timestamp = time();
echo( date('l  jS M Y, g:i a', $timestamp-9) );S

?>

 

However, when I alter my system time on my computer the time on the website is altered as well. That was not really what I wanted to do.

Link to comment
Share on other sites

Maybe if you're developing locally...  but if you use the time() function online it will give you the server time, and it will have absolutely nothing to do with the time on your computer.

 

BTW, you don't have to worry about different users timezones affecting what is being put in the database.  When you do the INSERT query, just use the MySQL now() function, which is also server-based.

 

<?php

$sql = "INSERT INTO table (all, your, basic, fields, here, insert_time) VALUES ($all, $your, $basic, $values, $here, now())";

$query = mysql_query($sql) or die("Error inserting record.");

?>

 

Theo

Link to comment
Share on other sites

... because PN's from all over the world could be written and I need one standard time and that is my location.

 

Then what you might want to consider is using GMT which is always correct regardless of your server location.  GMT is about as standard a standard as you can get ... and gmdate() is a php function.

Link to comment
Share on other sites

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.