PDA

View Full Version : day number inconsistency


bazz
03-09-2009, 01:17 AM
Hi,

OK I have totally confused myself so I can't see a way out of this.

I have a script which takes a timestamp and gets the day of week from it.


my$day_number = get_day_number($timestamp);
sub get_day_number {
my $day_number = shift;
return time2str("%w",$day_number);
}


seems fine.

However, that uses numbers 0-6.

When I read from the stored values of 0-6, this code tells me that I am out of range for the 0's.


my $day_start = $lp->{day_start};
my $formatted_day_start = Day_of_Week_to_Text($day_start);


Both codes work but the problem is that one uses 1-7 and the other uses 0-6.

How can I make the first block of code use the range of 1-7.

bazz

bazz
03-09-2009, 02:14 AM
OK, worked some more and it's straight forward :) lol

All I need do is convert 0-6 to Monday,Tuesday,Wednes... etc.

I can't find it in a module which works from 0-6.
my $formatted_day_start = Day_of_Week_to_Text($day_start) worsk wiht 1-7.

I would appreciate a pointer/shove.


This seems to have done the trick but I don't know how efficient it is.

my @days_of_week = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
my $day_start = $lp->{day_start};
my $formatted_day_start = $days_of_week[$day_start];

print qq( $day_start = 1
$formatted_day_start = Monday
);


functionally it's as I need.
bazz

FishMonger
03-09-2009, 04:10 AM
Which module are you using?

Which of the subs did you write and exactly what are they doing?

How can I make the first block of code use the range of 1-7.
The easiest solution would be to add 1 to the value returned in get_day_number(), but without seeing more of your code, I can't say that it's the best solution.

bazz
03-09-2009, 06:12 AM
Hi FishMonger,

I was trying to use
Date:Calc
Date:Parse
Date:Format


I got confused again because the days had the same number in both ranges except for the 7/0 and so, it looked like if I added 1 to each of them, it would knock them out.

if the code I posted in my last post is efficient, then I shall stick with it coz it is easy to pick up again for any future use.

bazz

FishMonger
03-10-2009, 01:35 AM
I don't see any real inefficiencies, so if it does what you need, then it should be fine.

bazz
03-10-2009, 01:41 AM
great, thanks.

Bazz

KevinADC
03-10-2009, 07:57 AM
Is that you over on sitepoint asking the same question bazz? Bad boy..... ;)

bazz
03-10-2009, 03:17 PM
I know, I was in a panic for an answer :(

I do ask some mysql stuff there to lighten the load around here.

bazz

oesxyl
03-10-2009, 05:23 PM
Is that you over on sitepoint asking the same question bazz? Bad boy..... ;)
you spy bazz to find what is doing? bad boy...:)

best regards

bazz
03-13-2009, 11:35 PM
lol :D
At least I don't participate in t e k - t i p s ;)

bazz