[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

perl date time calculation

Posted on 2009-04-03
7
Medium Priority
?
1,371 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 85

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

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 85

Accepted Solution

by:
ozo earned 2000 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 85

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans
Suggested Courses

830 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