Gossamer Forum
Home : General : Perl Programming :

time2iso returns default

Quote Reply
time2iso returns default
im using a line of code i was giving. its suppost to get the local time and assign it to a scalar which is then added to a database. the problem is that the time returned 1970-01-01 00:00:00, which i think is the default, and im not sure what im doing wrong.

$time = HTTP::Date::time2iso(localtime(time()));

thanks
Pedge
Quote Reply
Re: [pedge] time2iso returns default In reply to
If you want localtime as a scalar then use:

my $date = scalar localtime();
Quote Reply
Re: [Paul] time2iso returns default In reply to
originally the problem was that the date was added into the database but the days and month were one digit out because perl starts at 0 not 1. which is why someone suggested i use time2iso.

Pedge
Quote Reply
Re: [pedge] time2iso returns default In reply to
I'm not really sure what you mean, "perl" doesn't start at 0, arrays do though :)

Using the code I suggested above will return the correct date, if it didn't it would be a useless function :)
Quote Reply
Re: [Paul] time2iso returns default In reply to
when i added date to a sessions database, december would be month 11 and January would be 00. which is why im trying to change it. that was done with the code below:

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());

and i just tried my $date = scalar localtime(time());
and that returned a blank date

thanks
Pedge
Quote Reply
Re: [pedge] time2iso returns default In reply to
Quote:
and i just tried my $date = scalar localtime(time());
and that returned a blank date

You can remove time() as localtime() uses time() by default.

The syntax is correct so it shouldn't return a blank date. What code did you try?
Quote Reply
Re: [pedge] time2iso returns default In reply to
 
Quote Reply
Re: [Paul] time2iso returns default In reply to
my bad, yes that does work. (was html page was using older version of script)

that returns Fri Jan 10 16:47:12 2003
but i need it in date/time format for the database.
mysql >> session_date datetime NOT NULL default '0000-00-00 00:00:00'

can i use the time2iso to convert it???

thanks
Pedge
Quote Reply
Re: [pedge] time2iso returns default In reply to
I'd do this:

Code:
use POSIX qw/strfime/;

my $date = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);