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:
Dates

 



Wiliam
Novice

Aug 23, 2000, 1:47 AM

Post #1 of 7 (745 views)
Dates Can't Post

Does anyone in the world have any snippet of code for me to convert the following date:

7 August 2000

or even

07092000

into the number of seconds or minutes or days or whatever from a given date. That is, the number of minutes from the year 1999 or the year 2000 or something similar?

Thankyou for your help.

Wil.



Kanji
User

Aug 25, 2000, 1:05 PM

Post #2 of 7 (745 views)
Re: Dates [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -wl

use strict;
use Date::Calc qw( Delta_Days );

my %dates = ( "then" => "07082000", "now" => "25082000" );

while ( my( $when, $date ) = each %dates ) {
my ( $d, $m, $y ) = $date =~ /^(\d{2})(\d{2})(\d{4})$/;
$dates{ $when } = [ $y, $m, $d ];
}

my $days = Delta_Days( @{ $dates{"then"} }, @{ $dates{"now"} } );

print $days;</pre><HR></BLOCKQUOTE>


Wiliam
Novice

Aug 29, 2000, 4:13 AM

Post #3 of 7 (745 views)
Re: Dates [In reply to] Can't Post

Thankyou for the piece of code. Any ideas on how to work this?

Wil.


Kanji
User

Aug 31, 2000, 4:42 PM

Post #4 of 7 (745 views)
Re: Dates [In reply to] Can't Post

Cut n' paste into a new file. Make sure the file is executable. Run from your favourite shell.


Wiliam
Novice

Sep 1, 2000, 12:18 AM

Post #5 of 7 (745 views)
Re: Dates [In reply to] Can't Post

So "then" is my old date and "now" is my new date. Right?

Cheers,

Wil.


Kanji
User

Sep 1, 2000, 2:57 AM

Post #6 of 7 (745 views)
Re: Dates [In reply to] Can't Post

Wouldn't make much sense if it was the other way around.

BTW, the only lines you really need are ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use Date::Calc qw( Delta_Days );
my $days = Delta_Days( @then, @now );</pre><HR></BLOCKQUOTE>

Delta_Days() expects it's input to be a six-element array consisting of $year1, $month1, $day1 (aka @then, 3 elements), $year2, $month2, and $day2 (aka @now, another 3 elements), so most of that code just deals with taking a string (ie, 20000901) and breaking that up into an array (ie ("2000","09","01") ).

If your dates were already in array form, you could just use the barebones version of the code above.

Those names of variables, BTW, are purely arbritrary.


Wiliam
Novice

Sep 1, 2000, 3:13 AM

Post #7 of 7 (745 views)
Re: Dates [In reply to] Can't Post

Yah, but what would that code output to me? What will the result be?

Wil.

 
 


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

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