Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

perl date time calculation

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;
Avatar of ozo
ozo
Flag of United States of America image

$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},scalar localtime(time),\$err,1);
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;
Also this is how you can convert a time into epoch seconds:
# Convert to seconds
my $epoch = timelocal($sec, $min, $hour, $mday, $mon, $year);
#you could also say
$dateDelta = Date::Manip::DateCalc($row_ref->{ordertime},"now",\$err,1);
Avatar of jackjohnson44
jackjohnson44

ASKER

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");
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, I meant to type
0,"%mh"
although the number there would only make a difference for %md or %mt