Solved

perl date time calculation

Posted on 2009-04-03
7
1,300 Views
Last Modified: 2012-05-06
I have a time stored as a string in a db.  I want to find the difference in minutes between that value and the current time.
$row_ref->{ordertime} = 04/03/09 07:08:13 pm
localtime(time) = Fri Apr 3 19:17:59 2009
my result shows as 19:-8:13

What am I doing wrong?
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},localtime(time),\$err,1);
my $weekDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%wv");
my $dayDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%dv");
my $hourDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%hv");
my $minuteDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%mv");
my $secondDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%sv");
$deltaStr = substr('00' . $hourDeltaStr,-2,2) . ':' .
substr('00' . $minuteDeltaStr,-2,2) . ':' .
substr('00' . $secondDeltaStr,-2,2) ;

$body .=$row_ref->{ordertime} . "=" . localtime(time) . "=" . $deltaStr;
0
Comment
Question by:jackjohnson44
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24066080
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},scalar localtime(time),\$err,1);
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24086077
you could change the way you get the date, and use perl time() instead of unix date.

example:

$prev_date=time();
.
.
.
$current_date=time();
# time reports seconds since the epoch (00:00:00 UTC, January 1, 1970) so
$minutes=($current_date-$prev_date)/60;
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24086101
Also this is how you can convert a time into epoch seconds:
# Convert to seconds
my $epoch = timelocal($sec, $min, $hour, $mday, $mon, $year);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 24086185
#you could also say
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},"now",\$err,1);
0
 

Author Comment

by:jackjohnson44
ID: 24124044
Thanks, how do I find out how many minutes in the dateDelta

I have this
                        $dateDelta = Date::Manip::DateCalc(DateCalc($row_ref->{ordertime},"- 0 hours"), scalar localtime(time),\$err,1);


I know how to find out the minutes?
I know how to get minutes, but not minutes since, for instance 1 hour 5 min, will print out 5, if I do this, not 65
                        my $minuteDeltaStr = Date::Manip::Delta_Format($dateDelta,0,"%mv");
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24124056
Date::Manip::Delta_Format( Date::Manip::DateCalc('04/03/09 07:08:13 pm',"now",\$err,1), 9,"%mh")
0
 
LVL 84

Expert Comment

by:ozo
ID: 24124803
sorry, I meant to type
0,"%mh"
although the number there would only make a difference for %md or %mt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now