Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to print date period

         

lindajames

9:43 am on Jul 26, 2003 (gmt 0)

10+ Year Member



hi, i need to know how i can print a date period using perl in the following format:

25/06/2003 - 25/07/2003

the way it needs to be calculated is depending on the current date. for example if today is the 26th of July then the period will be like the above example.

If today's date was 30/07/03 then the period that the script prints should be 29/06/2003 - 29/07/2003

any suggestions would be appreciated.

cheers
linda

lindajames

7:35 pm on Jul 27, 2003 (gmt 0)

10+ Year Member



hi, i got the following code for it:


$today = get_today();
$lastmonth = get_prev_month($today);
print "$lastmonth - $today\n";

sub get_today {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
return "$mday/$mon/$year";
}

sub get_prev_month {
my $date = shift;
my($pday, $pmon, $pyear) = split(/\//, $date);
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
} else {
$pmon -= 1;
}
return "$pday\/$pmon\/$pyear";
}

it seems to be doing fine but when i run it i get 27/5/2003 - 27/6/2003.

theres two problems with that. firstly its from month 05 to month 06 when the month now is 07 and therefore it should be from month 06 - 07

and secondly today is the 27th its showing from the 27th of the previous month to the 27th of this month. if today is 27th it should show the date from 26 of the previous month to the 26th of this month (yesterday)

any suggestions would be appreciated.

cheers
linda

Damian

8:30 pm on Jul 27, 2003 (gmt 0)

10+ Year Member



>it should be from month 06 - 07
You could add 1 to $mon.
You can either write:
$mon = $mon + 1;
or the notation with which your script adds 1900 to $year:
$mon += 1;

To substract one day of $pday:
$pday = $pday -1;
or
$pday -= 1;

Storyteller

1:17 am on Jul 29, 2003 (gmt 0)

10+ Year Member



Here is an excellent article on date manipulation with CPAN modules. I think it covers periods.
[perl.com...]

lindajames

11:28 am on Aug 2, 2003 (gmt 0)

10+ Year Member



i tried the $pday -= 1; in the following way:

sub get_prev_month {
my $date = shift;
my($pday, $pmon, $pyear) = split(/\//, $date);
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
} else {
$pmon -= 1;
$pday -= 1;
}
return "$pday\/$pmon\/$pyear";
}

but it seems to not work properly. it only seems to subtract 1 day from the previous month and not from the current month.

any suggestions would be appreciated.

cheers
linda

Damian

1:14 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



I don't see the problem Linda. I tested it with the following little standalone script based on your sub routine and this works for me :

[perl]
#!E:\perl\bin\perl.exe
print "Content-type: text/html\n\n";

$pday = 1;
$pmon = 8;
$pyear = 2003;

if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
}

else {
$pmon -= 1;
$pday -= 1;
}

if($pday == 0) {
$pmon -= 1; # substract another from $pmon
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
}
if ($pmon =~ /^(0񔘞񕓤񖎣0)$/){$pday = 31; }
elsif ($pmon == "1"){$pday = 29; } # it's february
else {$pday = 30; }
}

print "$pday\/$pmon\/$pyear";

[/perl]

if the above works for you too, maybe there's something wrong with the format of $pmon when your sub routine recieves it. You can try printing the original $pmon to debug.

Sorry to complicate your problem but your sub routine does not consider $pday possibly being '1' ..then $pday should become either '30' or '31' .. hehe... it's a fun job we have no?
I added something to the above script to suggest a solution for that..february has 29 days right.. better check on how I determine the number of days in each month.. I did this by the 'counting knuckles" method ..

but er.. up to you to
consider february 28 days once every four years and stuff... ;)

<edit reason> edited to substract one more from $pmon if $pday =1 </edit reason>

Damian

1:28 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



By the way.. Storyteller's link is an excellent link if you want to learn more about date handling. Probably more efficient to learn about what others have done already then us trying to re-invent the wheel...

<added>Related and containing a possible solution:
[webmasterworld.com...] </added>

ShawnR

2:07 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have three suggestions:

Firstly "...only seems to subtract 1 day from the previous month and not from the current month..."

Yes, that is how you have written your code. Instead of modifying your 'get_prev_month() function, you could do:


$yesterday = get_today(-1);
$lastmonth = get_prev_month($yesterday);
print "$lastmonth - $yesterday\n";

And then modify your 'get_today' function to take a parameter being a number of days offset. When applying the offset, take note of 28 vs 30 vs 31 days and leapyears, as already indicated by Damian

Secondly"...it should be from month 06 - 07
You could add 1 to $mon.
You can either write:
$mon = $mon + 1;
or the notation with which your script adds 1900 to $year:
$mon += 1; ..."

I'd suggest that you must add one to the month. For some reason (I am yet to be convinced it is a good one), months go from 0 to 11 instead of 1 to 12. Same format for Javascript. Generally when adding one, the notation I'd like to see used $mon++;

Thirdly
It may be an idea to separate the date formating from the date manipulation. If you use this code for different countries, you will need different formats. It would be nice to just change the function which changes the printing format, without having to change any of the other functions which manipulate the date. What I am suggesting is to have the input and output of get_prev_month(), and the output of get_today(), be of the format returned by perl's time function rather than what your get_today() currently returns. Sorry to complicate things. Just a suggestion; take it or leave it. ;)

Shawn