CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
What was the date last Monday?

 



peter.l.jones
New User

Jul 6, 2002, 8:37 AM

Post #1 of 4 (353 views)
What was the date last Monday? Can't Post

Hello

I need to supply set dates in a GUI pull down menu.
The dates would be , Today, Yesterday, Last24 Hours,
Monday,Tuesday, Wednesday etc.
Using locatime I am able to calculate the date for Today
and Yesterday but how can I do this for a particular day in the week?


davorg
Thaumaturge / Moderator

Jul 6, 2002, 10:16 AM

Post #2 of 4 (351 views)
Re: [peter.l.jones] What was the date last Monday? [In reply to] Can't Post

This is adapted from code in Chapter 6 of Data Munging with Perl

Code
my $when = 1; # Monday is 1 according to localtime 

my $now = time;
my @now = localtime($now);

# how many day do we need to go back
my $diff = ($now[6] - $when + 7) % 7);

# Subtract $diff days
my $then = $now - (24 * 60 * 60);

At the end of this calculation, $then contains the "epoch seconds" value for the current time on the previous Monday. You can convert that to a human readable value with any of the usual methods. I like POSIX strftime.

Code
use POSIX 'strftime'; 
print strftime '%Y-%m-%d', localtime $then;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


peter.l.jones
New User

Jul 9, 2002, 10:47 AM

Post #3 of 4 (347 views)
Re: [davorg] What was the date last Monday? [In reply to] Can't Post

Hi

Thanks this worked well. I just needed to edit the line

# Subtract $diff days
my $then = $now - (24 * 60 * 60);

to add ($diff*24*60*60)



Regards



Pete


davorg
Thaumaturge / Moderator

Jul 10, 2002, 5:27 AM

Post #4 of 4 (344 views)
Re: [peter.l.jones] What was the date last Monday? [In reply to] Can't Post

Er... yeah... I guess that was a deliberate error to see if you were paying attention.

Or something like that :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives