|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Hi there
I am having a little trouble with dates and was hoping somebody could steer me in the right direction. Currently I am using $mday = localtime(time); which gives me a reult of Wed Dec 6 10:39:19 2000 How would I go about changing this to 6/12/2000 Any help would be appreciated |
|
#2
|
|||
|
|||
|
Don't just use $mday. Here is an example from one of my old scripts:
sub get_date { $timeoffset = "0"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + (3600*$timeoffset)); if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $mon = "0$mon"; } $year_length = length $year; if ($year_length == 3) { $year = substr($year,1); } $month = ($mon + 1); if ($month < 10) { $month = "0$month"; } @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); $long_date = "$hour:$min:$sec $months[$mon] $mday, 20$year"; chop($long_date) if ($long_date =~ /n$/); chop($mday) if ($mday =~ /n$/); chop($month) if ($month =~ /n$/); chop($year) if ($year =~ /n$/); } As you can see, you can customize the output to your liking. |
|
#3
|
|||
|
|||
|
This sub routine is more standardized. That is, you can use it to convert into any format.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre> ## CONVERT_TIME(<timestring>,[timeval]) sub convert_time { undef $timestring; my $timestring = $_[0]; if ($_[1]) { $ctime = $_[1]; } else { $ctime = time(); } @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); @months = ('January','February','March','April','May','June','July','August','September','October','November', 'December'); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ctime); if ($hour < 10) { $timeHH = "0$hour"; } else { $timeHH = $hour; } $timeH = $hour; if ($hour > 12) { $newhour = $hour - 12; } else { $newhour = $hour; } if ($newhour == 0) { $newhour = 12; } if ($newhour < 10) { $timehh = "0$newhour"; } else { $timehh = $newhour; } $timeh = $newhour; if ($min < 10) { $timeNN = "0$min"; } else { $timeNN = $min; } $timeN = $min; if ($sec < 10) { $timeSS = "0$sec"; } else { $timeSS = $sec; } $timeS = $sec; $timeYY = substr(1900+$year,2,2); $timeYYYY = 1900+$year; $timeMMMM = $months[$mon]; $timeMMM = substr($timeMMMM,0,3); $mon++; if ($mon < 10) { $timeMM = "0$mon"; } else { $timeMM = $mon; } $timeDDDD = $days[$wday]; $timeDDD = substr($timeDDDD,0,3); if ($mday < 10) { $timeDD = "0$mday"; } else { $timeDD = $mday; } if ($hour > 12) { $ampm = "pm"; } else { $ampm = "am"; } $timestring =~ s/HH/$timeHH/g; $timestring =~ s/hh/$timehh/g; $timestring =~ s/H/$timeH/g; $timestring =~ s/h/$timeh/g; $timestring =~ s/nn/$timeNN/gi; $timestring =~ s/n/$timeN/gi; $timestring =~ s/ss/$timeSS/gi; $timestring =~ s/s/$timeS/gi; $timestring =~ s/yyyy/$timeYYYY/gi; $timestring =~ s/yy/$timeYY/gi; $timestring =~ s/mmmm/$timeMMMM/gi; $timestring =~ s/mmm/$timeMMM/gi; $timestring =~ s/mm/$timeMM/gi; $timestring =~ s/dddd/$timeDDDD/gi; $timestring =~ s/ddd/$timeDDD/gi; $timestring =~ s/dd/$timeDD/gi; $timestring =~ s/TT/U$ampmE/g; $timestring =~ s/tt/$ampm/g; $timestring =~ s/zz/CST/g; return $timestring; } [/code] Use it like this: $time=&convert_time("dd/mm/yyyy"); or $time=&convert_time("dd/mm/yyyy",$unixtime); if you have a specific time string you want converted (time()); |
|
#4
|
|||
|
|||
|
And don't use the chop command if you're just trying to get new lines off the end. Use chomp(). That way you wont have to check if it had a n at the end.
|
|
#5
|
||||
|
||||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by chrisdice4:
How would I go about changing this to 6/12/2000[/quote] Here's what I use: <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre> #======================================================================= # time2date() - convert time() value to date string # # usage: $time = time2date($time,'lt'); # where $time is a valid time() value # 'l' is optional and indicates 'long' format # 't' is optional and directs sub to append a time val # # long format: Wednesday, January 1, 2001 # default: 01/01/2001 sub time2date { my ($time,$format) = @_; my ($timestr,$longdate,$shortdate); my @day = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); my @mon = qw(January February March April May June July August September October November December); my $i = 0; my %wdays = map { substr($_,0,3) => $_ } @day; my %month = map { substr($_,0,3) => $_ } @mon; my %dmnum = map { substr($_,0,3) => ++$i } @mon; # prepend '0' to 0-9 months map { substr($dmnum{$_},0,0) = '0' if length($dmnum{$_}) == 1 } (keys %dmnum); # date string -- Monday, May 25, 2012 $longdate = scalar localtime($time); $longdate =~ s{(w+)s+(w+)s+(d+)s+([d:]+)s+(d+)} {$wdays{$1}, $month{$2} $3, $5}g; my $wday = $3; # time val -- 04:13:52 p.m. my ($h,$m,$s) = split /:/, $4, 3; my $ampm = $h < 12 ? 'a.m.' : 'p.m.'; $h -= 12 if $h > 12; $h += 12 if $h == 0; substr($h,0,0) = '0' if length($h) == 1; $timestr = join(':',$h,$m,$s) . " $ampm"; # date number -- 5/25/2012 $shortdate = join('/',$dmnum{$2},$wday,$5); if ($format =~ m{t}i) { for ($longdate,$shortdate) { $_ .= ' ' . $timestr; } } return ($format =~ m{l}i) ? $longdate : $shortdate; } [/code] |
|
#6
|
|||
|
|||
|
Hi there
Thank you all for your responses, I am going to go through each of them. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Formating dates in Perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|