SunQuest
           Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 6th, 2000, 04:15 AM
chrisdice4 chrisdice4 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Cape Town, WP, South Africa
Posts: 39 chrisdice4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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

Reply With Quote
  #2  
Old December 6th, 2000, 04:39 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.


Reply With Quote
  #3  
Old December 8th, 2000, 03:45 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
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());

Reply With Quote
  #4  
Old December 8th, 2000, 03:47 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
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.

Reply With Quote
  #5  
Old December 8th, 2000, 10:11 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
<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]


Reply With Quote
  #6  
Old December 11th, 2000, 02:12 AM
chrisdice4 chrisdice4 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Cape Town, WP, South Africa
Posts: 39 chrisdice4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hi there

Thank you all for your responses, I am going to go through each of them.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Formating dates in Perl


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway