Jump to content

timezone change with date()


emopoops

Recommended Posts

so i put all my times for posts in my  made from scratch forum(not phpbb3 or sm)

in my mysql db using the time()

and when i show the time on webpages i use the date('ymd/h:i:s', $time) kind of thing.

now the default timezone is America/New_York for php or whatever

but IM GOING to make a thing upon registering on my site that the user choses what timezone they are in.

 

now i have two questions.

1. arent there only 24 different timezone hours? (i know theres more names than that.. but does anyone know of 24 main ones so i can get all the zones but not have to have all the cities etc..

 

2. since im going to be storing the users timezone pick in a mysql db, im going to put their timezone pick in a session variable and then echo it into whatever way im displaying the time or at least i hope i can do that.. so is there someway i can do that..  to show the time in a certain timezone(the choice of the user that ill put in the session var)

?

 

any help? :confused:

Link to comment
Share on other sites

You could perhaps use either UTC, or GMT. Find the default time for UTC + 0, or find the default time for GMT + 0.

I know GMT = 20:42 (8:42PM) at the time this was posted.

All you'd have to do, is put GMT + 1 up to GMT + 12. Then GMT - 1 down to GMT - 12.

Same for UTC. Though thats a different time zone, so it'd be a different time.

 

Sorry can't be much help. GMT and UTC are the most commonly used ones, so.. just pick 1 or the other.

Link to comment
Share on other sites

Like this:

 

<?php
$timezone = -1; //This is the timezone from the UTC see http://en.wikipedia.org/wiki/List_of_time_zones
//$timezone can be anything between +12 and -12

$_SESSION['timezone'] = $timezone * 3600;

//And then do this:

gmdate(time(), $_SESSION['timezone'];
?>

 

You don't have to use a Session variable but I do. One thing this doesn't support is the use of Day-Light Saving Time. What you could do is give the user an option to +1 to $timezone if that user is in Daylight-Saving Time.

 

<?php
$dst = 1; //1 evaluates to boolean TRUE, 0 evaluates to boolean FALSE

$_SESSION['timezone'] = ($dst == 1) ? ($timezone + 1) * 3600 : $timezone * 3600;
?>

 

Alternatively, if you're using PHP 5, you can use, date_default_timezone_set. The only downfall this way is having to make a list/array of the timezones you need.

Link to comment
Share on other sites

isnt there a php function that is like timezone('atlantic, $time)

n time would be time().

i cant use the first one.. because well it seems like too much work with this daylight savings time.

also. i cant set the default timezone everytime a page is run. then it will be set wayy too many times. i was thinking about making a list of timezones but onely 24 because there is only 24 different hours right? just abunch of different citys.. tho.

i still am lost on what to do.

see i found this funciton:

 

http://us.php.net/date_timezone_set

 

but i think its something like this one:

http://us2.php.net/date_default_timezone_set

and like i said i cant keep setting the default timezone... can i? it seems like a waste and it seems like it would screw something up.. i mean theres a function at the bottom of the page that uses the change the default timezone but what if two scripts are running at the same time and they are in two different timezones? wont that effect it like one change after the other since its a php thing? or is it not like that im really confused.

 

 

 

Link to comment
Share on other sites

date_default_timezone_set() sets the timezone used for those date()-type  functions so you can just use date() and the correct offset will be set for the user.

 

Unfortunately, both methods require work. You don't have to set the timezone with date_default_timezone_set() or putenv('TZ=...') on every page, you can just put it in a common page and include that page. The PHP5 method does have support for Daylight Saving Time. Also different timezones set in date_default_timezone_set() shouldn't clash.

 

The method I use has backward compatibility and it's not significantly hard to implement -- just store the $dst value in the database under an enum-field or a tinyint-field, and use a checkbox in a form to have the use set it to either true or false.

 

If neither of those do it for you, it might be an alternative to look for a timezone-class but at that point you're just reinventing the wheel.

Link to comment
Share on other sites

well i dont understand anything. what im asking is if there  is a timezone to set per user? like a session variable.

i mean arent there 24 different timezones?(distinct)?

i mean its getting really hard to just let the future users on my site see all times in times that are not even in their right timezone. im trying to make something here. i cant figure anything out. its really upsettling.

im really not too old at php im kind of a noob.

i know that setting the default timezone will not work for anything because what if the values were alreayd put in the database using a different timezone. im so confused on what todo

Link to comment
Share on other sites

You could perhaps do what GoldenEye suggested, but have a form.. with a select box, with The timezones.

 

On submit {
//Set the users timezone put it in there database. Instead of 
$timezone = -1; 
//Have;
$timezone = $_POST['timezone'];
}

 

If the selection box has the values set for +12 to -12. Then the value will be inserted where $timezone is.

Maybe it'd be wise to set a cookie with the timezone value.

 

Then.. theres the default timezone that all uses use.. if you find out which timezone it is. Then hopefully.. when a users pick a timezone, it registers it in a cookie, and it should set it as THEIR timezone.

This may be the best place to start at, work on what GoldenEye said, and get it so $timezone = $_POST['timezone'];

I'd suggest then do a mysql_query and insert it into the database.

 

After that, when displaying the time, connect it to the database, or cookie, and get the +1 etc..

 

I'm not sure. Probably best to start here.

I could TRY work on something, though I cannot guaruntee it'd work, I'm pretty noob, but I could perhaps be of some assistance.

Link to comment
Share on other sites

To complement the last post (by c-o-d-e), I have written what his post suggested. Since I use this system myself, I know it works to do exactly what you want it to do. The array I provided in y.php is the exact array I use for my projects. In the provided link for a ZIP-File, there are two files. The two files are just two examples of how you would use the seconds-offset with gmdate. y.php exemplifies how you'd implement it into a form so the user can change it. z.php exemplifies how you'd assign it to a $_SESSION variable as if you were pulling it out of a database. It may or may not be a lot of work to implement depending on how you setup your code and how much code you have. I hope these are clear enough examples. If by chance you need some clarification, feel free to send me a private-message.

 

Download it (it's hosted on my personal virtual-directory)

http://eltanin.net/~elyk/dev/php/timezone_code.zip

Link to comment
Share on other sites

what does this mean?

if($row['dst'] == '1')
	$_SESSION['timezone'] = ($row['timezone'] + 1) * 3600;
else
	$_SESSION['timezone'] = ($row['timezone']) * 3600;
//$_SESSION['timezone'] should hold an integer with the value of 3600

were are u getting dst from ? a query? and for which user? lol or show rest of ur code :-)

 

that means if $row['dst'] equals 1 then display the timezone + 1 *3600

 

else then dont add the +1

Link to comment
Share on other sites

what does this mean?

if($row['dst'] == '1')
	$_SESSION['timezone'] = ($row['timezone'] + 1) * 3600;
else
	$_SESSION['timezone'] = ($row['timezone']) * 3600;
//$_SESSION['timezone'] should hold an integer with the value of 3600

 

Yes, as Gayner suggested, it's from a hypothetical database field named `dst` with a value of either '1' for TRUE or '0' for FALSE.

 

IF `dst` equals '1', then add 1-hour to the UTC-offset. So if the value of $row['timezone'] for yourself was -5 and you had $row['dst'] set to '1' then your UTC-offset would actually be -4. This is because when you enter Daylight-Saving Time, you set your clock back 1-hour.

 

IF you had $row['dst'] set to '0' then you would not add 1 to your value of $row['timezone'] so your UTC-offset would just -5.

Link to comment
Share on other sites

If you had a from.

<form action"" method="post">
<select name="timezone" id="timezone">
<option value="+12"> UTC +12</option>
all the way to..
<option value="-12"> UTC -12 </option>
</select>
</form>

 

Above that do the code I suggest

$timezone = $_POST['timezone'];

Enter the value into the users database.

$username = $_SESSION['username'];
$q = mysql_query("UPDATE users SET timezone = '$timezone' WHERE username = '$username'") or trigger_error("Query failed:" mysql_error());

Then you'd set it as the default timezone for that user

$_SESSION['timezone'] = $timezone * 3600;
gmdate(time(), $_SESSION['timezone'];

;

 

In the full code:

<?php
if(isset($_POST['submit'])){
$timezone = $_POST['timezone'];
$username = $_SESSION['username'];
$q = mysql_query("UPDATE users SET timezone = '$timezone' WHERE username = '$username'") or trigger_error("Query failed:" mysql_error());
if($q){
$_SESSION['timezone'] = $timezone * 3600;
gmdate(time(), $_SESSION['timezone'];
// rest of your code etc
} else
{ echo 'Something has gone wrong, please try again later'; }
}
else
{
?>
<form action"" method="post">
<select name="timezone" id="timezone">
<option value="+12"> UTC +12</option>
all the way to..
<option value="-12"> UTC -12 </option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
<?php } ?>

 

This may not be the correct way of doing it, but it'd be where I'd start at.

Perhaps then ask GoldenEye what you'd do after that.

Link to comment
Share on other sites

  • 2 weeks later...

Oh, well does time() and date() do daylight savings? I mean, how are you supposed to calculate daylight savings? How are you supposed to know which time was daylight savings and which one wasn't? For example, from an old thread two years back?

 

Google is your friend. http://www.toosweettobesour.com/2009/03/10/calculating-daylight-savings-time-boundary-in-php/

Link to comment
Share on other sites

time() is Unix time, the number of seconds since 00:00:00 UTC on January 1, 1970

If you did.. date("d-m-y H:i:s");

This will display similar to:

1-01-1970 00:00:00

 

If you did date("d-m-y H:i:s" time());

This will convert Unix Timestamp, into the format of 1-01-1970 00:00:00

Giving you the CURRENT date.

 

People think time() is the number of seconds SINCE 1970, meaning that since 1970, unix timestamp has been recorded in seconds. When infact, if you did; echo time(); This will give you the current time stamp in seconds, if you went back in time when the timestamp will be "0" you'll be in 1970. Lets say current timestamp is 142432523, it'd be that many seconds since 1-01-1970 00:00:00.

 

I'm not to sure what the rest of your message is suppose to mean. Though read the article Altec suggested, and please, do use google. In any chance, did the code I give to you.. become a use to you at all?

Link to comment
Share on other sites

// Place your query where it says "Do you query etc"
// Replace "timestamp" from $row['timestamp'] with the row of the timestamp value in your database.
$q = mysql_query("Do your query here to get the timestamp value") or trigger_error("Query failed: " mysql_error());
$row = mysql_fetch_array($q);

echo ''.date("d-m-y h:i:s" $row['timestamp']).'';

 

Does this solve your problem? if the timestamp value comes up in the database, it's most likely the problem is

date($timestamp) - I am sure this would display the timestamp. To have it display it in date form. It should be similar to..

date("d-m-y h:i:s" $timestamp);

This will change the timestamp value into date form.

Does that solve your problem?

Link to comment
Share on other sites

What exactly don't you understand.

 

date is a function that can accept one or two parameters/arguments. The first is a string that is used to format the output, the second is a valid unix timestamp. If the second parameter isn't supplied it will assume you wish to use the valid unix timestamp for the time you called it.

 

time is a function that returns the current unix timestamp (as such calling date("Y-m-d", time()) is the same as calling date("Y-m-d")).

 

mktime is a function that will create a valid unix timestamp when you provide it a month, day, year, hours, minutes and seconds.

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.