PDA

View Full Version : modifying time format


bazz
03-31-2005, 06:12 PM
HI,

here's a script I found and which outputs the time as yyyymmdd. I want to modify it to output the date as, 1st (or 2nd 3rd 4th etc), September yyyy.

I haven't a clue which bits to change so any pointers (without actually doing it for me), would be appreciated. I've searched but don't seem to find this specific piece.

I'm away now to look up the possibility that an array might be useful eg:

@month (January, February, March, April, May, June, July, August, September, October, November, December); but I can see that there isnt yet a tie in between this data and the top line of the code.


###################################
#GET THE TIME AND DATE
###################################
($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst) = localtime(time);
$dayofyear++;
if ($day < 10) { $day = 0 . $day; }
if ($month < 10) { $month = 0 . $month; } $month++;
$hour = &hours($hour);
$minute = &minutes($minute);
$year += 1900;
$site_time = "$hour:$minute (GMT)";
$site_date = "$year/$month/$day";

sub minutes {
$minute = shift;

if ($minute < "10") {
$minute = 0 . $minute;
return $minute;
} else {
return $minute;
}
}

sub hours {
$hour = shift;

if ($hour > 15) {
$hour += 8;
if ($hour > 24) {
$hour -= 24;
}
return $hour;
} else {
$hour += 8;
return $hour;
}
}


Bazz

mlseim
03-31-2005, 07:35 PM
Bazz ... something like this?

==========================================================

#!/usr/bin/perl

print "Content-type: text/html\n\n";

$day = "22";

$last = substr($day, -1, length($day)); #Get the last digit
$first = substr($day, 0, 1); #for days that are like: 01,02,03 etc.
if($first == 0){$day = $last;} #change them to: 1,2,3 etc.

unless(($first == 1)&&(length($day) == 2)){ #take care of the "teens"
if($last == 0){$day = $day."th";}
if($last == 1){$day = $day."st";}
if($last == 2){$day = $day."nd";}
if($last == 3){$day = $day."rd";}
if($last == 4){$day = $day."th";}
if($last == 5){$day = $day."th";}
if($last == 6){$day = $day."th";}
if($last == 7){$day = $day."th";}
if($last == 8){$day = $day."th";}
if($last == 9){$day = $day."th";}
}
else
{
$day = $day."th";
}


print "$day<br><br>\n";

#############################

EDIT ... oops, I forgot about the "teens" ... added it above.



.

bazz
04-01-2005, 10:41 AM
Max, as ever, you're brill :thumbsup:

Eventually, I got the month to show textually as well which has enabled me to remove a big post here :) Used the if elsif to do it. I hope that that is the more server efficient way.

Bazz

mlseim
04-01-2005, 12:51 PM
Bazz ...

Perl is a lot like C++ ...

Both programming languages have a "main" program that
begins. In C++ you must define "main", in Perl, it's assumed.
Variables in "main" are global.

If you define variables or arrays in a subroutine, they become
known locally only to that subroutine.

See some good explanations here:
http://www.google.com/search?hl=en&q=perl+subroutine+global+variables

systray
04-06-2005, 03:18 PM
this here should do exactly the same, but is more Perl-like (yes, perl is a lot like C++ and is not like C++ ;) )


sub convert_days ($) {
my ($day) = (@_);
$day =~ s/^0//; # remove "heading" zeros in case $day is a string like "09"
$day .= $day == 1 ? 'st' : # if then else construct with ?: operator
$day == 2 ? 'nd' :
$day == 3 ? 'rd' : 'th';
return $day;
}convert_days(1) returns 1st
convert_days(2) returns 2nd
convert_days(3) returns 3rd
convert_days(4) returns 4th

systray

rwedge
04-07-2005, 02:37 AM
To handle 21 through 31 :) $day .= $day =~ /1$/ ? 'st' :
$day =~ /2$/ ? 'nd' :
$day =~ /3$/ ? 'rd' : 'th'; /Bob

systray
04-07-2005, 06:57 AM
yup, Bob, you're right :thumbsup: , I didn't think that far ;)

systray
04-07-2005, 10:55 AM
I had to find a better regular expression, so here we go, converting all numbers from 1 to 31 correctly ...finally :D

sub convert_days($) {
my ($day) = (@_);
$day =~ s/^0//; # remove zeros at the beginning in case $day is a string
$day .= $day =~ /^[^1]?1$/ ? 'st' :
$day =~ /^[^1]?2$/ ? 'nd' :
$day =~ /^[^1]?3$/ ? 'rd' : 'th';
return $day;
}McPerl - I'm loving it