My hosting server is located in Texas and when I use the localtime() function, I get Texas local time rather than Massachusetts local time (where I currently reside). I want to standardize all my time information so that when I store it into the MySQL database, I store GMT rather than any localtime, and always do a timezone adjustment when pulling it out. I know there is a time() function which returns the number of seconds since Jan 01, 1970, but the problem with that is, I don't know if there's a way to format that into a readable time/date or whether time() returns the localtime or Greenwich Mean Time. What is the best way to do this? Also, what is the best way to store timestamps?

9 Years
Discussion Span
Last Post by davidianstyle

You don't. If you're problem is storing the time then simply store the number that time() returns. Then to display the time, assuming that the time retrieved from the DB is stored in the variable $time, do

gmtime($time) // to see it in GMT
localtime($time) // to see it in localtime


Thanks, I have found that if you set the environment variable TZ, perl will figure out the localtime for the timezone I set.

use lib ("/usr/lib/perl5/5.8.8");
use Time::Local;

sub GetTime {
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday);                                                                                    
        $ENV{TZ} = ':/usr/share/zoneinfo/US/Eastern';                                                                                                        
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time);                                                                                                         
        $year += 1900;                                                                                                         

        my $ampm;
        if ($hour > 12) {                                                                                                         
                $hour -= 12;                                                                                                         
                $ampm = 'pm';                                                                                                         
        } else {
                $ampm = 'am';                                                                                                         

        if ($hour < 10) {                                                                                                         
                $hour = '0' . $hour;                                                                                                         
        if ($min < 10) {                                                                                                         
                $min = '0' . $min;                                                                                                         
        if ($sec < 10) {                                                                                                         
                $sec = '0' . $sec;                                                                                                         

        return $hour . ":" . $min . ":" . $sec . $ampm;                                                                                                         
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.