Gossamer Forum
Home : General : Perl Programming :

Getting first day of a month...

Quote Reply
Getting first day of a month...
Hi,

I'm really having trouble with this :(

Basically, I'm trying to build a calendar box, like so:

Code:
August 2007
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31


(the above is done with Date::Calc::Calendar(2007,8); , but its plain text formatting - and I would rather format it manually into tables/cells etc).

Now, I could do this once I have the following paramaters -

1) The start day
2) The number of days in a given month

The 2nd one, is pretty simple:

my $days = Days_in_Month($year,$month);

..but the 1st one has me stumped.

Does anyone have a good way of working out the day the 1st of a given month falls on?

TIA!

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Getting first day of a month... In reply to
I wrote a calendar script recently. Getting the first day of the month is not easy and involves a lengthy calculation (or at least it did for me). I managed to find the code by searching Yahoo and HotScripts for calendar scripts.

To give you an idea of how horrible it is to work out:

Code:
$day = $day[( $y + int($y / 4) - int($y / 100) + int($y / 400) + $d[$m - 1] + $d) % 7];
Quote Reply
Re: [Wychwood] Getting first day of a month... In reply to
Hi,

Thanks for the reply.

Man, its amazing there isn't a Perl module that has a function to do this <G> (I'm surprised that Date::Calc doesn't have it, as it gets worked out perfectly with the Calendar() function).

Ah well, I'd better get working on it =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Wychwood] Getting first day of a month... In reply to
Hi,

Turns out there is quite a cool Perl module around :)

Code:
#!/usr/bin/perl -w

use strict;
use CGI::Carp qw(fatalsToBrowser);

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

use Calendar::Simple;
use Data::Dumper;

my @curr = calendar(8,2007); # mm, yy

print Dumper(@curr);

It then gives you an array, so you can loop through - and work out the first day of the month :) (although, I don't need that now - as this formats it nicely into weekloy sub-arrays).

Just thought I would post, in case anyone else may have the same problem as me in the future :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Getting first day of a month... In reply to
Hi, you can try this module.

use Date::Tie;

# Someday
my $date = Date::Tie->new(year => 2007, month => 9, day => 1);

# 6
print $date->{weekday};
Quote Reply
Re: [nkcsx] Getting first day of a month... In reply to
Hi,

Thanks, I already managed to get it working with Calendar::Simple :) Thanks for the reply though.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Getting first day of a month... In reply to
Hi Andy,

I am writing a calendar program but unable to get the first day of month. If you are aware of it, can you just forward the program for my reference.

Actually in my program each and every month is starting from Sunday. Kindly help me.


Thanks and Regards,
Pavithra.
Quote Reply
Re: [pavithra.bt] Getting first day of a month... In reply to
Hi,

Give this a go (thats how I did it)

http://search.cpan.org/...b/Calendar/Simple.pm

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [pavithra.bt] Getting first day of a month... In reply to
 
I had a similar problem, here's how I solved it: http://search.cpan.org/~izut/Date-Simple-3.03/lib/Date/Simple.pm
use the day_of_week method.
eg.
Code:
my $date = Date::Simple->new('2009-01-01');
my $first = $date->day_of_week();

Doesn't get much easier.

Carl