What im trying to do, is find the difference between to dates, one in a mysql timestamp format, the other in the
localtime format, in the form of seconds:minutes:hours etc. I tried to use
Class::Date, but all i could get it to do was give me the number of seconds between the dates, which when i tried to get it to give me a nice string, always gave me something like '1970 january 1..', b/c it adds the seconds onto that date or something like that. None of the other modules on
cpan seemed to do what i wanted, they all just appeared to be methods for deterimining a specific date in time. Anyone know of a good module or something? This is the code i came up with:
my ($i,$d,@posttime,@difference,@t);
my $posttime=shift;
my @mytime=(localtime)[0..5];
$mytime[5]+=1900;
$mytime[4]+=1;
for(my $i=0;$i<scalar @t;$i++){next if $i==0;$mytime[$i]=(length $
+mytime[$i]==1)?'0'.$mytime[$i]:$mytime[$i]};
unshift @posttime,(substr "$posttime",0,4);
for($i=4;$i<14;$i+=2){unshift @posttime,(substr "$posttime",$i,2);
+}
if(($mytime[0]-$posttime[0])<0){$mytime[1]--;$mytime[0]+=60;}
$difference[0]=($mytime[0]-$posttime[0]);
if(($mytime[1]-$posttime[1])<0){$mytime[2]--;$mytime[1]+=60;}
$difference[1]=($mytime[1]-$posttime[1]);
if(($mytime[2]-$posttime[2])<0){$mytime[3]--;$mytime[2]+=24;}
$difference[2]=($mytime[2]-$posttime[2]);
if(($mytime[3]-$posttime[3])<0){$mytime[4]--;$mytime[3]+=31;}
$difference[3]=($mytime[3]-$posttime[3]);
if(($mytime[4]-$posttime[4])<0){$mytime[5]--;$mytime[4]+=12;}
$difference[4]=($mytime[4]-$posttime[4]);
$difference[5]=($mytime[5]-$posttime[5]);
$i=0;$d='';
my @englishtime=('second(s)','minute(s)','hour(s)','day(s)','month
+(s)','year(s)');
#for(@difference){$d.="$_ $mytimeime[$i] ";$i++;}
$d="[$difference[0]s:$difference[1]m:$difference[2]h - $difference
+[3]d/$difference[4]m/$difference[5]y]";
return $d;
I know its incredibly kludgy and barely runs under use strict, which is mostly why im looking for a good module to use.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.