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: Beginner:
comparing dates

 



tarabhussain
stranger

Oct 25, 2001, 5:08 AM

Post #1 of 5 (1003 views)
comparing dates Can't Post

Hi I have a date that I'm retrieving from the database in the format 20011103.......I want to be able to compare it to today's date........but since this is a string I don't know how to do it.......

My question is: Is there a way to convert that into days like $yday of localtime?



mhx
Enthusiast / Moderator

Oct 25, 2001, 7:12 AM

Post #2 of 5 (998 views)
Re: comparing dates [In reply to] Can't Post

You could use a regular expression and the mktime function from Perl's POSIX module:

Code
#!/bin/perl -w 
use POSIX 'mktime';

$date = '20011103';
$date =~ /(\d{4})(\d{2})(\d{2})/;
$t1 = mktime( 0, 0, 0, $3, $2, $1-1900 );
$t2 = time;

print scalar localtime $t1, "\n",
scalar localtime $t2, "\n";

Now $t1 contains the time elapsed in seconds since January 1st 1970, which is the base also for Perl's time function. This way, you could easily compare $t1 and $t2 directly.

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



tarabhussain
stranger

Oct 25, 2001, 8:01 AM

Post #3 of 5 (996 views)
Re: comparing dates [In reply to] Can't Post

thanks i'll give it a try



jam
stranger

Oct 25, 2001, 8:11 AM

Post #4 of 5 (994 views)
Re: comparing dates [In reply to] Can't Post

Hi marcus,

I'm probably wrong but dont you have to -1 off the month as well as -1900 of the year, or is this not nessesary with mktime?

like

$t1 = mktime( 0, 0, 0, $3, $2-1, $1-1900 );

just a query as i also need to use this method in one of my scripts.

Jam



mhx
Enthusiast / Moderator

Oct 25, 2001, 12:49 PM

Post #5 of 5 (987 views)
Re: comparing dates [In reply to] Can't Post

Ooops Blush

You're right, I forgot about decrementing the month.
Thanks for pointing that out.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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