Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

A Better Way To Get Days Since Monday of Last Week

by Tommy (Chaplain)
on Jun 07, 2005 at 15:23 UTC ( #464339=perlquestion: print w/ replies, xml ) Need Help??
Tommy has asked for the wisdom of the Perl Monks concerning the following question:

I'm wondering what other ways might you (better) obtain the number of days since monday (and friday for that matter) of the previous week?
use Time::ParseDate; # ... # use some logic to find beginning of last week # note: (localtime)[6] = wday (0-6) where 0 = Sunday my($today) = (localtime)[6]; my($monday_lastw) = { # figure out days since monday of last week 0 => 6, 1 => 7, 2 => 8, 3 => 9, 4 => 10, 5 => 11, 6 => 12, }->{ $today }; my($saturday_lastw) = $monday_lastw - 6; # (days since sat. of last wk +) # get unix timestamp of the date on /monday/ of last week # (first second of last week) push(@parms, (parsedate(qq{$monday_lastw days ago 00:00:00}))[0]); # get unix timestamp of the date on /saturday/ of last week # (last second of last week) push(@parms, (parsedate(qq{$saturday_lastw days ago 23:59:59}))[0]);
--
Tommy Butler, a.k.a. Tommy

Comment on A Better Way To Get Days Since Monday of Last Week
Download Code
Re: A Better Way To Get Days Since Monday of Last Week
by jbware (Chaplain) on Jun 07, 2005 at 15:37 UTC
    If I understand your question, I think the following would give you # of days since Monday of last week.
    @t=localtime; print $t[6]+6;
    For # of Days since last Friday it'd be:
    @t=localtime; print $t[6]+2;

    -jbWare
Re: A Better Way To Get Days Since Monday of Last Week
by davidrw (Prior) on Jun 07, 2005 at 15:40 UTC
    Can use some Date::Calc methods (add what's gone by in this week to what was left in the previous week):
    use Date::Calc qw/Today Day_of_Week Decode_Day_of_Week/; print Day_of_Week( Today ) + (7 - Decode_Day_of_Week('Monday')); print Day_of_Week( Today ) + (7 - Decode_Day_of_Week('Friday'));
Re: A Better Way To Get Days Since Monday of Last Week
by dave0 (Friar) on Jun 07, 2005 at 15:42 UTC
    You might want to try Date::Calc. The Monday_Of_Week() function should help a lot.
Re: A Better Way To Get Days Since Monday of Last Week
by blazar (Canon) on Jun 07, 2005 at 15:46 UTC
    I'm wondering what other ways might you (better) obtain the number of days since monday (and friday for that matter) of the previous week?
    use Time::ParseDate;
    I'm not familiar with Time::ParseDate but at first sight it wouldn't seem the best tool to accomplish your task since your logic is much like "do, then undo". I suppose that some of the functions from Date::Calc (which I have advantageously used) may be better suited for it. Just a resonable guess...
Re: A Better Way To Get Days Since Monday of Last Week
by Roy Johnson (Monsignor) on Jun 07, 2005 at 15:46 UTC
    my $today = (localtime)[6]; my $target_day = 1; # monday $target_day -= 7; # last week printf "%d days since target day\n", $today - $target_day;

    Caution: Contents may have been coded under pressure.
      Purrrrrrrrfect. Why do these 'simple logic' questions evade me all the time?! What you show is _exactly_ what i was looking for a mathematical solution rather than a hard coded if/then sequence (really that's what i was doing) covering all the bases. Imagine doing that for something with hundreds or thousands of possible states! Sheesh! I feel so stupid.
      --
      Tommy Butler, a.k.a. Tommy

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://464339]
Approved by pelagic
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-09-01 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (15 votes), past polls