Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
September 24th, 2012, 07:52 AM
 miteshpant
Registered User

Join Date: Sep 2012
Posts: 3
Time spent in forums: 19 m 38 sec
Reputation Power: 0
Complte Difference Between Two Dates

my @dim =(0,31,28,31,30,31,30,31,31,30,31,30,31);

my @timeData = localtime(time);
my \$hr=\$timeData[2];
my \$min=\$timeData[1];
my \$sec=\$timeData[0];
my \$dt=\$timeData[3];
my \$mnt=\$timeData[4] + 1;
my \$yr =\$timeData[5] + 1900;

my \$t1 = 2012-09-21 14:27:00';
my \$t2 = "\$mnt\-\$dt\-\$yr \$hr:\$min:\$sec";

print "The difference between \$yr\-\$mnt\-\$dt \$hr:\$min:\$sec and \$at";
\$mnt =~ s/09/9/;
\$mnt =~ s/08/8/;
\$mnt =~ s/07/7/;
\$mnt =~ s/06/6/;
\$mnt =~ s/05/5/;
\$mnt =~ s/04/4/;
\$mnt =~ s/03/3/;
\$mnt =~ s/02/2/;
\$mnt =~ s/01/1/;

\$dt =~ s/09/9/;
\$dt =~ s/08/8/;
\$dt =~ s/07/7/;
\$dt =~ s/06/6/;
\$dt =~ s/05/5/;
\$dt =~ s/04/4/;
\$dt =~ s/03/3/;
\$dt =~ s/02/2/;
\$dt =~ s/01/1/;

\$da[1] =~ s/09/9/;
\$da[1] =~ s/08/8/;
\$da[1] =~ s/07/7/;
\$da[1] =~ s/06/6/;
\$da[1] =~ s/05/5/;
\$da[1] =~ s/04/4/;
\$da[1] =~ s/03/3/;
\$da[1] =~ s/02/2/;
\$da[1] =~ s/01/1/;

\$da[2] =~ s/09/9/;
\$da[2] =~ s/08/8/;
\$da[2] =~ s/07/7/;
\$da[2] =~ s/06/6/;
\$da[2] =~ s/05/5/;
\$da[2] =~ s/04/4/;
\$da[2] =~ s/03/3/;
\$da[2] =~ s/02/2/;
\$da[2] =~ s/01/1/;

\$t1[0] =~ s/09/9/;
\$t1[0] =~ s/08/8/;
\$t1[0] =~ s/07/7/;
\$t1[0] =~ s/06/6/;
\$t1[0] =~ s/05/5/;
\$t1[0] =~ s/04/4/;
\$t1[0] =~ s/03/3/;
\$t1[0] =~ s/02/2/;
\$t1[0] =~ s/01/1/;
\$t1[0] =~ s/00/0/;

\$t1[1] =~ s/09/9/;
\$t1[1] =~ s/08/8/;
\$t1[1] =~ s/07/7/;
\$t1[1] =~ s/06/6/;
\$t1[1] =~ s/05/5/;
\$t1[1] =~ s/04/4/;
\$t1[1] =~ s/03/3/;
\$t1[1] =~ s/02/2/;
\$t1[1] =~ s/01/1/;
\$t1[1] =~ s/00/0/;

\$t1[2] =~ s/09/9/;
\$t1[2] =~ s/08/8/;
\$t1[2] =~ s/07/7/;
\$t1[2] =~ s/06/6/;
\$t1[2] =~ s/05/5/;
\$t1[2] =~ s/04/4/;
\$t1[2] =~ s/03/3/;
\$t1[2] =~ s/02/2/;
\$t1[2] =~ s/01/1/;
\$t1[2] =~ s/00/0/;

my \$ds=0;
my \$dmin=0;
my \$dhr =0;
my \$dd=0;
my \$dm =0;
my \$dy=0;
if(\$yr % 4 == 0)
{
\$dim[2] = 29;
}
if(\$sec < \$t1[2])
{

\$sec = \$sec + 60;
\$min = \$min -1;
\$ds = \$sec - \$t1[2];

}
else
{

\$ds = \$sec - \$t1[2];
}

if(\$min < \$t1[1])
{

\$min = \$min + 60;
\$hr = \$hr -1;

\$dmin = \$min - \$t1[1];
}
else
{

\$dmin = \$min - \$t1[1];
}

if(\$hr < \$t1[0])
{
\$hr = \$hr + 24;
\$dt = \$dt -1;

\$dhr = \$hr - \$t1[0];
}
else
{
\$dhr = \$hr - \$t1[0];
}

if(\$dt < \$da[2])
{
\$dt = \$dim[\$mnt] + \$dt;
\$mnt = \$mnt -1;

\$dd = \$dt - \$da[2];
}
else
{
\$dd = \$dt - \$da[2];
}

if(\$mnt < \$da[1])
{
\$mnt = \$mnt + 1;
\$yr = \$yr -1;

\$dm = \$mnt - \$da[1];
}
else
{
\$dm = \$mnt - \$da[1];
}
\$dy = \$yr - \$da[0];

print " is YEAR \$dy MONTH \$dm DAYS \$dd HOURS \$dhr MINUTES \$dmin SEC \$ds <br>";

#here is the output
#The difference between 2012-9-24 8:51:3 and 2012-09-21 #14:27:00 is YEAR 0 MONTH 0 DAYS 2 HOURS 18 MINUTES 24 #SEC 3

#2
September 24th, 2012, 07:53 AM
 miteshpant
Registered User

Join Date: Sep 2012
Posts: 3
Time spent in forums: 19 m 38 sec
Reputation Power: 0

#3
September 24th, 2012, 09:56 AM
 keath
!~ /m\$/

Join Date: May 2004
Location: Reno, NV
Posts: 4,099
Time spent in forums: 2 Weeks 4 Days 8 h 20 m 20 sec
Reputation Power: 1809
You didn't ask a question, but I suggest you use the DateTime module to calculate the difference between dates. It's aware of timezones, leap years, and all the other complexities of dates.

 Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Complte Difference Between Two Dates