Multiple date & time support

phpasks

New Member
Messages
145
Reaction score
0
Points
0
I will need multiple date & time support.

if one user login in UK - then that's time UK time display.

other one US then that's time US time zone display.

another one AU then that's user AU time zone display.

How it's handle using PHP.........

Cheers
Asif Khan
http://www.phpasks.com
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You just need to store the GMT (or any other) time as a UNIX timestamp and add/remove a certain amount depending on the timezone offset.
For example, a timestamp is 1000000 (GMT). If a user in timezone GMT needs the date, you don't have to change it and just use the date() function on it. If a user in GMT+1 requests it, you add 60*60 (60 minutes * 60 seconds = 1h) to it (= 1003600) and use the date() function on that.

- Marshian
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Can you give me any example for that code????


You just need to store the GMT (or any other) time as a UNIX timestamp and add/remove a certain amount depending on the timezone offset.
For example, a timestamp is 1000000 (GMT). If a user in timezone GMT needs the date, you don't have to change it and just use the date() function on it. If a user in GMT+1 requests it, you add 60*60 (60 minutes * 60 seconds = 1h) to it (= 1003600) and use the date() function on that.

- Marshian
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
An example:

PHP:
$result = mysql_fetch_array(mysql_query("some query here"));
$time = $result["time"]; //Unix timestamp, we suppose it's 1000000
$result = mysql_fetch_array(mysql_query("some query here"));
$usertimezone = $result["timezone"]; //We'll suppose this is -10 (= GMT-10)
$localisedtime = $time + $usertimezone*60*60; //1000000+(-10)*60*60
echo date("date format", $localisedtime);

- Marshian
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Thanks, Marshian

regards, Asif

An example:

PHP:
$result = mysql_fetch_array(mysql_query("some query here"));
$time = $result["time"]; //Unix timestamp, we suppose it's 1000000
$result = mysql_fetch_array(mysql_query("some query here"));
$usertimezone = $result["timezone"]; //We'll suppose this is -10 (= GMT-10)
$localisedtime = $time + $usertimezone*60*60; //1000000+(-10)*60*60
echo date("date format", $localisedtime);
- Marshian
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I have a quick daft question on this..

According to the above posts, I could use date_default_timezone set, but I've been using the putenv() function.

i.e. putenv ("TZ=".$_SESSION['MM_UTZ']);

where $_SESSION['MM_UTZ'] is the user's timezone preference.

This automatically works out the difference in Timezones with Timestamps (e.g. Time()) when echoing dates.


What exactly is the difference between the two?

Also, I still haven't managed to find a good solution for date inputs...
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
[FONT=&quot]Alternatively you can get a user to set their own timezone upon registration, and then fetch it from a database. I would suggest making just a tinyint field in your database, and then having an array of timezones. Check out the date_default_timezone_set() function. And for a list of timezones; try this.[/FONT]

Thanks Scopey

I have attached 2 files - one is the insert page and the other is a test time page, which contains the $ndttime= new DateTime($timefield, new DateTimeZone($utz)); function.

This subject seems to get more and more complex the more I get into it!

I hope you can help and will offer 100 credits to anyone who provides a conclusive solution.
 

Attachments

  • inserteventphp.txt
    11.7 KB · Views: 71
  • testphp.txt
    3.4 KB · Views: 70
Last edited:

scopey

New Member
Messages
62
Reaction score
0
Points
0
I'm not completely sure what you're trying to do, but I made a quick example php page to show what I meant about making an array of timestamps and setting a session variable for your timestamp.

Here's the code here:

PHP:
<?php
session_start();
if(sizeof($_POST) > 0 && $_POST['submit']){
    $_SESSION['timezone'] = $_POST['timezone'];
}
//I can only be bothered inserting a few timezones...
$timezones = array(
    array('stamp'=>"US/Eastern",'name'=>"United States - Eastern"),
    array('stamp'=>"US/Central",'name'=>"United States - Central"),
    array('stamp'=>"NZ",'name'=>"New Zealand"),
    array('stamp'=>"Europe/London",'name'=>"United Kingdom")
);
if(isset($_SESSION['timezone'])){
    date_default_timezone_set($timezones[$_SESSION['timezone']]['stamp']);
}


print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>';
print date('l \\t\h\e jS \of F Y')."<br />".date('g\:ia')."<br /><br />";
print"<form action=\"".$_SERVER['REQUEST_URI']."\" method=\"post\">";
print "<select name='timezone'>";
for($i=0;$i<sizeof($timezones);$i++){
    print "<option value='{$i}'>{$timezones[$i]['name']}</option>";
}
print "</select>";

print '<input type="submit" name="submit" value="Set timezone" />
</form>
</body>
</html>';
?>
I havn't had time to test it as Absolut is down -.- . But all you need to do is finish and put that $timezone definition in a global include file and then you can call date() like you normally would, and the timezone will be set for that particular user.

[EDIT] -> I have now tested it... Works fine. Look here for the script in action!
 
Last edited:

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Thanks Scopey, but I already have the timezones set from user preferences with this bit of code about half way down the page of inserteventphp.

session_start();
//set page timezone
if ($_SESSION['MM_UTZ'] == NULL){
$_SESSION['MM_UTZ']="UTC";}
putenv ("TZ=".$_SESSION['MM_UTZ']);
//set page timeformat
if ($_SESSION['MM_UTF'] == NULL){
$_SESSION['MM_UTF']="Y-m-d H:i:s";
}

This is fine for echos from the DB with timestamp format but my problem is getting a date into it!

i.e.

form field + JS date/time picker

date/time picker returns a formatted date in Y-m-d H:i:s.

the new datetime object needs to take this $_POST value and convert into a timestamp (numeric) string, associated with the users timezone.

This is stored in the table in numeric format.

When this is echo'd from the DB, it translates the numeric object and returns a date/time object relative to the browser.

For instance, if user 1 in GB inserts a date/time (e.g. 2008-08-31 12:00:00), the new datetime object will enter a unix date: 1220180400 (GB)

When user 2 in EST-6 echos this value, it will take into account the time difference and return a date/time value 2008-08-31 06:00:00.

My problem is getting the new datetime object intot he database (ref the insert script).

Is this any clearer?

I have a page that tests this at http://www.freecrm.x10hosting.com/timetest.php
 
Last edited:

scopey

New Member
Messages
62
Reaction score
0
Points
0
Thanks Scopey, but I already have the timezones set from user preferences with this bit of code about half way down the page of inserteventphp.

session_start();
//set page timezone
if ($_SESSION['MM_UTZ'] == NULL){
$_SESSION['MM_UTZ']="UTC";}
putenv ("TZ=".$_SESSION['MM_UTZ']);
//set page timeformat
if ($_SESSION['MM_UTF'] == NULL){
$_SESSION['MM_UTF']="Y-m-d H:i:s";
}

This is fine for echos from the DB with timestamp format but my problem is getting a date into it!

i.e.

form field + JS date/time picker

date/time picker returns a formatted date in Y-m-d H:i:s.

the new datetime object needs to take this $_POST value and convert into a timestamp (numeric) string, associated with the users timezone.

This is stored in the table in numeric format.

When this is echo'd from the DB, it translates the numeric object and returns a date/time object relative to the browser.

For instance, if user 1 in GB inserts a date/time (e.g. 2008-08-31 12:00:00), the new datetime object will enter a unix date: 1220180400 (GB)

When user 2 in EST-6 echos this value, it will take into account the time difference and return a date/time value 2008-08-31 06:00:00.

My problem is getting the new datetime object intot he database (ref the insert script).

Is this any clearer?

I have a page that tests this at http://www.freecrm.x10hosting.com/timetest.php

Understood. That is quite a difficult request though. I'll have to think about it a bit and come back with a solution.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I'm sort of getting there with this one.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

$dto_evstart= new DateTime($_POST['form_evstart'], new DateTimeZone($_SESSION['usertimezone]));

$insertSQL = sprintf("INSERT INTO EVENTS (EVSTART, EVDURATION) VALUES ($dto_evstart->format('Y-m-d H:i:s'), $_POST['evduration'])",



The trouble is, it is just inserting the Y-m-d H:i:s format and not storing the timezone data.

Is there a different format that I need to use that includes timezone information?
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Well, I think that mysql converts times stored in a Timestamp field to UTC, and then to the relative time when selected. So I think if you used a "SET time_zone = timezone" query upon a user request, you wouldn't have to worry about it in php.

However, the Timestamp field only has a range of 1970 to 2038. If you plan on storing dates out of that range, you'll need to use the DateTime type(which doesn't convert to UTC when storing). In this case, I don't see why you need to store the timezone with it. Why not convert it to UTC before storing? Then all that matters is the requesting user's timezone.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Well, I think that mysql converts times stored in a Timestamp field to UTC, and then to the relative time when selected. So I think if you used a "SET time_zone = timezone" query upon a user request, you wouldn't have to worry about it in php.

However, the Timestamp field only has a range of 1970 to 2038. If you plan on storing dates out of that range, you'll need to use the DateTime type(which doesn't convert to UTC when storing). In this case, I don't see why you need to store the timezone with it. Why not convert it to UTC before storing? Then all that matters is the requesting user's timezone.

OK - this makes the most sense so far...

Firstly, all my automated dates (such as creation dates) are in default UTC format from Time() functions. These do echo correctly in different timezones.

So the idea so far is good.

However, when you say "mysql converts times stored in a Timestamp field to UTC", do you mean that the database itself (storage field in timestamp format) converts a full date/time/TZ string i.e. "2008-09-03 19:35:15 GB" or does it convert the first main string i.e "2008-09-03 19:35:15"?

Or is it the php that converts the string first before storing in Epoch Timestamp value?

That said, all I need to do is to figure out how to convert a date/time string into a timestamp UTC value!

Oh - and I've just increased the offer for this solution (also posted in marketplace) to 800 credits because it is doing my nut in!:rant2:
Edit:
Finally cracked it myself...

provided you have used the user preferences to set the default timezone, you can use a simple strtotime($_POST['datetimefield']) to convert the datetime string into a epoch UTC value.

When a different user signs in, simply echo the stored value using date($storedvalue) and it will account for the time difference automatically - I can't believe it was this simple!!!!
 
Last edited:

freecrm

New Member
Messages
629
Reaction score
0
Points
0

Not sure how this applies to the problem of multiple timezones..

gmdate is just like date() only returning a value in GMT.

The majority of the issues here relate to echoing stored values from an epoch timestamp Time() which, for someone in the states, wouldn't be printed with gmdate().

If you were thinking about creating a timestamp, this also creates a problem for anyone not in the GMT timezone as it assumes a GMT stamp, regardless of server time.

As I have recommended before, always STORE in UTC (Time()) epoch values and print/echo relating to each user's timezone preference using date().

If the timezone preference has been set correctly at the serever end (using putenv(TZ=....)), the server thinks it is working in the specified timezone and the date() funtion will echo in relation to the UTC value stored.

In addition, the date() function can be manipulted to show the date in whatever format the user sets his/her preferences to.

i.e.

PHP:
<?php echo date('$S_timeformat_preference',$datevalue);?>
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Finally this is working for all timezone like every country.

like USA, UK, Europian Country.........

please tell me.

Not sure how this applies to the problem of multiple timezones..

gmdate is just like date() only returning a value in GMT.

The majority of the issues here relate to echoing stored values from an epoch timestamp Time() which, for someone in the states, wouldn't be printed with gmdate().

If you were thinking about creating a timestamp, this also creates a problem for anyone not in the GMT timezone as it assumes a GMT stamp, regardless of server time.

As I have recommended before, always STORE in UTC (Time()) epoch values and print/echo relating to each user's timezone preference using date().

If the timezone preference has been set correctly at the serever end (using putenv(TZ=....)), the server thinks it is working in the specified timezone and the date() funtion will echo in relation to the UTC value stored.

In addition, the date() function can be manipulted to show the date in whatever format the user sets his/her preferences to.

i.e.

PHP:
<?php echo date('$S_timeformat_preference',$datevalue);?>
 
Top