in reply to Re: date
in thread Year shows as 20102 in date
Pass it the timestamp you want converting (or leave blank for the current time), and in return you'll get something like 13 Feb 2002 18:48 in your current timezone.sub library_todayis { my ($time)=@_; if ($time<1) { $time=time(); } my @months=qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!; my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $tz) = loca +ltime($time); if ($day<10) { $day="0".$day; } if ($hour<10) { $hour="0".$hour; } if ($min<10) { $min="0".$min; } if ($sec<10) { $sec="0".$sec; } $year = $year + 1900; return ("$day $months[$mon] $year $hour:$min"); }
Update: Thanks to little, for a 'duh! why are you doing it like that?' here's a shortened version doing it the slightly better way using sprintf:
Which, I have to confess is a lot better looking and probably a bit faster (although I haven't benchmarked it)sub library_todayis { my $time=shift || time(); my @months=qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!; my ($sec,$min,$hour,$day,$mon,$year)=localtime($time); $year+=1900;$mon++; return sprintf("%02d $months[$mon] $year %02d:%02d",$day,$hour,$min); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: date
by little (Curate) on Feb 13, 2002 at 18:54 UTC |