Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Day of Millenium from Form Input

Status
Not open for further replies.

bnc123

Technical User
Mar 25, 2001
59
AU
I have a script which gives me the day of the millenium. It goes something like this:

#!/usr/bin/perl

require "cgi-lib.pl";

print &PrintHeader;

# Get the date and time information
($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST) = localtime(time);

# Fix the year to keep it Y2K compliant
$Year = $ShortYear + 1900;

$DayNumber = (($Year-2000)*365)+ $DayOfYear;

print <<"EOF";
<html><body>
Today is approximately day number $DayNumber of the millenium.
</body></html>
EOF

#End of program


That's all very well, if I want to know what day of the millenium it is today. But what if I want to know what day of the millenium it will be on 14 June 2005?

In other words, if I receive the date, month and year as form inputs, how will I calculate the day of the millenium?

Thanks for your help.
 
You note that your result is approximate, but you're making some incorrect assumptions here.

1. The current millenium began on Jan 1 2001, not Jan 1 2000.

1-1000: 1st millenium
1001-2000: 2nd millenium
2001-3000: 3rd (current) millenium

2. Not all years have 365 days. There's a leap year every four years or so. Formula for leap year:
($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0)

I'd suggest using Date::Calc and just calculate the number of days since 1 Jan 2001. (I think it's the DeltaDays function that does this, but check out the documentation.)

 
Now that I think about it, you'd need to calculate number of days since the last day in the previous millenium,
Dec 31 2000
. (Or calculate from Jan 1 2001 and then add 1.
 
mikevh, from our last discussion, you know that I am just a newbie. I downloaded the Date::Calc, but simply don't know what to do with it.

Can you please write me a small (and simple) script where say:
$day = 14
$month = 5 (for June, as the array will start from 0 and not from 1 and
$year is 2005

Let's say the output that will be the day of the millenium will be assigned to $day_mil


Please tell me what to do with Date::Calc, where to put it, what permissions to give it etc. I have gone through its documentation, but once again, drew a blank. So please do me a favour and give me some details.

Many thanks in advance.
 
This lets you enter a date in mmddyyyy format (with or without slashes) on the command line and tells you what day of the millenium it is. The script makes no attempt to verify that the days or months are legal.
Code:
#!perl
use strict;
use warnings;
use Date::Calc qw(Delta_Days);

my $indate = shift;
die qq(Bad date format!\n)
    unless $indate =~ /^(\d\d)[\/.-]*(\d\d)[\/.-]*(\d\d\d\d)$/;
my ($month, $day, $year) = ($1, $2, $3);

my $day_mil = Delta_Days(2000, 12, 31, $year, $month, $day);
print "$indate is day $day_mil of the millenium.\n";
perl bnc.pl 05/14/2005
05/14/2005 is day 1595 of the millenium.

perl bnc.pl 09/12/2004
09/12/2004 is day 1351 of the millenium.


As for Date::Calc, are you sure it's not already installed?
Try this at a command prompt:
perl -e "use Date::Calc;"
If this exits with no error, it's already installed.








 
Sorry mikevh, I have tried all sorts of things for the last few days, but don't seem to get any output from it. I want to run the script on a web server, where the day, month and year come in as form inputs. I don't want to run it on a local machine.

I guess, I am not as advanced in programming as you are. Sorry to have wasted your time.
 
No need to apologize.
My post was a "small (and simple)" script intended to demonstrate the use of Date::Calc to get the current day of the millenium. Obviously your code will be somewhat different if you're getting your input from a form. Post your code and I'll take a look.
 
The only code I had is right at the top of this page. Besides that I have no code.

FYI, I use cgi-lib.pl for parsing the incoming form fields. In my HTML form the text inputs will be given the names of 'day' 'month' and 'year'. Hence they will be introduced to my cgi script as $in{'day'}, $in{'month'} and $in{'year'}. They will be parts of @(indata).

You tell me what to do after that. Or any other way if you are not accustomed to cgi-lib.pl, but for running from a HTML form on a web server, not on a local machine.

Many Thanks.
 
If you have day, month, and year in $in{'day'}, $in{'month'} and $in{'year'}, you just need to pass them as arguments to Delta_Days, like so:
Code:
use Date::Calc qw(Delta_Days); #near top of script
...
...
my $day_mil = Delta_Days(2000, 12, 31, @in{qw(year month day)});
or, more longwinded:
Code:
use Date::Calc qw(Delta_Days); #near top of script
...
...
my $day_mil = Delta_Days(2000, 12, 31, $in{year}, $in{month}, $in{day});
$day_mil will contain the day of the millenium for the date in question.

Here's a way of doing it without Date::Calc.
Code:
#!perl
use strict;
use warnings;

main: {
    # days in each month
    my @daysmths = (undef, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
    my %in;
    @in{qw(month day year)} = (5, 14, 2005); #date to test
    
    my $day_mil = 0;
    # add days from 2001 through last year
    for my $year (2001..$in{year} - 1) {
        $day_mil += 365 + isleap($year); #account for leap year
    }
    
    # add days in current year through last month
    for my $i (1..$in{month} - 1) {
        $day_mil += $daysmths[$i];
        if ($i == 2) {
            $day_mil += isleap($in{year}); #account for leap year
        }
    }
    # add days in current month
    $day_mil += $in{day};
    
    print join("/", @in{qw(month day year)}), " is day $day_mil of the millenium.\n";
}

sub isleap {
    # Return true if year is a leap year, false otherwise.
    my $year = shift;
    ($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0);
}
Output:
5/14/2005 is day 1595 of the millenium.


I'm glad they wrote the module. :)

HTH



 
Thanks mikevh.

Some success atlast.

Everything works fine as long as the day month and year are defined within the script itself as 14, 5 and 2005. But as soon as I get them in as inputs from a form, it falls apart.

I have set up an HTML form at I have also uploaded the script to the cgi-bin. The script now looks like this:



Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

require "cgi-lib.pl";

#read the incoming data
&ReadParse;

#set the document type using the subroutine
print &PrintHeader;

use strict;
use warnings;


main: {
    # days in each month
    my @daysmths = (undef, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    my %in;
    @in{qw(month day year)} = ($in{'day'}, $in{'month'}, $in{'year'}); #date to test

    my $day_mil = 0;
    # add days from 2001 through last year
    for my $year (2001..$in{'year'} - 1) {
        $day_mil += 365 + isleap($year); #account for leap year
    }

    # add days in current year through last month
    for my $i (1..$in{'month'} - 1) {
        $day_mil += $daysmths[$i];
        if ($i == 2) {
            $day_mil += isleap($in{'year'}); #account for leap year
        }
    }
    # add days in current month
    $day_mil += $in{'day'};

    print join("/", @in{qw(day month year)}), " is day $day_mil of the millenium.\n";
}

sub isleap {
    # Return true if year is a leap year, false otherwise.
    my $year = shift;
    ($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0);
}


#End

Could you please fine tune the script to suit MY conditions. We are almost there. I think after a couple of more exchanges, I'll leave you in peace atlast (till the next time I run into problems, that is).

Thanks again.
 
Could you please fine tune the script to suit MY conditions.
???
Your problem seems to be with communication between the form and the script. I'm not very familiar with CGI, so I think I'll let you take it from here. Or perhaps someone else will be willing to help you.

 
SUCCESS ATLAST

It really works.

I tweaked it a bit. There was no communication problem.

I still have a lot of garbage in my script. I'll clean it up by 'trial & error' method, mostly because I don't understand most of what is going on.

My script now looks like this:

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

require "cgi-lib.pl";

#read the incoming data
&ReadParse;

#set the document type using the subroutine
print &PrintHeader;

#use strict;
#use warnings;

#Get month into words from figures
use Time::Local;
$year = $in{'year'};
$month = $in{'month'};
$day = $in{'day'};
$sec = 0;
$min = 0;
$hour = 0;

my $timelocal = timelocal($sec,$min,$hour,$day,$month,$year);
#my $wday = (localtime($timelocal))[6];
my $moy = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')[$month];
#my $dow = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')[$wday];



$dayy = $in{'day'};
$monthh = $in{'month'};
$yearr = $in{'year'};
#$monthh_x = $in{'month'}+1;


main: {
    # days in each month
    my @daysmths = (undef, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    my %in;
    @in{qw(month day year)} = ($monthh, $dayy, $yearr); #date to test

    #print "$in{'day'}, $in{'month'}, $in{'year'}\n";
    #exit(0);


    my $day_mil = 0;
    # add days from 2001 through last year
    for my $year (2001..$in{'year'} - 1) {
        $day_mil += 365 + isleap($year); #account for leap year
    }

    # add days in current year through last month
    for my $i (1..$in{'month'} - 1) {
        $day_mil += $daysmths[$i];
        if ($i == 2) {
            $day_mil += isleap($in{'year'}); #account for leap year
        }
    }
    # add days in current month
    $day_mil += $in{'day'};

    print "$dayy $moy $yearr, is day $day_mil of the millenium.\n";
}

sub isleap {
    # Return true if year is a leap year, false otherwise.
    my $year = shift;
    ($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0);
}


#End

Thanks mikevh. I couldn't have done it without your help.

Till the next time..... and have a nice day (I know I will now).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top