Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Time Zone conflict betwen clients in a web application

by py_201 (Acolyte)
on Dec 15, 2009 at 06:16 UTC ( #812825=perlquestion: print w/ replies, xml ) Need Help??
py_201 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have to enter user's time in my database and show it to them next time they login as last login time. How can i go about it in perl?
what i have thought about it is that i can take localtime of server and convert it to some standerd time and store it in database. then while retrieving the time, i can take the client time zone and convert my standerd time to that time zone and show it to user.
But the problem is how can i convert localtime to standerd time? and how can i get user's time zone automatically.
Thanks

Comment on Time Zone conflict betwen clients in a web application
Re: Time Zone conflict betwen clients in a web application
by Anonymous Monk on Dec 15, 2009 at 06:38 UTC
Re: Time Zone conflict betwen clients in a web application
by ikegami (Pope) on Dec 15, 2009 at 07:11 UTC

    I have to enter user's time in my database and show it to them next time they login as last login time.

    You could use JavaScript to populate a hidden field with their localtime when they login.

    If you don't want to rely on JS or some other client-side application, you'll need to have the user select his timezone from a form like our Timezone Settings. Then, if you simply store the result of time on login, you can generate the user's last login time in the user's time zone using

    my $dt = DateTime->from_epoch( epoch => $epoch_time_stored_in_db, time_zone => $time_zone_stored_in_db, ); print $dt->strftime("Last login at %Y-%m-%d %H:%M:%S\n");
Re: Time Zone conflict betwen clients in a web application
by tokpela (Chaplain) on Dec 15, 2009 at 10:12 UTC

    You should be storing UTC time (also known as GMT) so that all of your timeframes are common across all timezones for consistency. Then, when you need to show the users their time - you can convert from GMT to localtime.

    You can get the gmtime in seconds by using the Time::Local module. Here is a snippet that you can use to show how to do the time conversions:

    use strict; use warnings; use Time::Local; my $time = time(); my $timegm = timegm(gmtime($time)); my $gmt = gmtime($time); my $timelocal = timelocal(localtime($time)); my $lmt = localtime($time); print "TIME: [$time]\n"; print "GMT: [$timegm]\n"; print "GMT AS TEXT: [$gmt]\n"; print "LOCALTIME: [$timelocal]\n"; print "LOCALTIME AS TEXT: [$lmt]\n";

    To get the localtime of GMT use the seconds value of the gmtime in localtime

    my $timelocal = timelocal(gmtime($timegm)); my $lmt = localtime($timegm);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812825]
Approved by redgreen
Front-paged by redgreen
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-12-22 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls