How to subtract two date in Perl?
Hi all,
I have two datetime as string in variable how i can subtract to get the different in seconds? $startdat = "2007-11-17 12:50:22"; $stopdat = "2007-11-17 12:53:22"; $result = $stopdat - $startdat; But it is not working, can you guys please help to get the differences in sec? Thank You Abdul |
First you'll need to convert your dates to seconds, i've not used perl for a number of years, so here's a result from google:
http://www.unix.org.ua/orelly/perl/cookbook/ch03_03.htm Once you've converted to seconds it'll be easy to subtract... |
HI,
I tried the following code but i did not work for me. $startdat = "2007-11-17 12:51:22"; $date2 = ParseDate($startdat); #Convert string into date use Time::Local; $TIMEa = timegm($date2); print $TIMEa; Error: perl /var/lib/asterisk/agi-bin/test.pl Day '' out of range 1..31 at /var/lib/asterisk/agi-bin/test.pl line 21 Any Idea please? Thank You |
If I remember the article, you had to split your date string into it's constituent parts before passing it to the function!
|
One way is to get the date diff using Delta_YMDHMS from http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod, then cvt to epoch secs eg
use Time::Local; $TIME = timelocal($sec, $min, $hours, $mday, $mon, $year); |
Hi friends,
Thank you for your help i used the following code and it worked well for me. $startdat = "2007-11-17 12:51:22"; $stopdate = "2007-11-17 12:52:22"; my ($years, $months, $days, $hours, $mins, $secs) = split /\W+/, $startdat; my ($yeart, $montht, $dayt, $hourt, $mint, $sect) = split /\W+/, $stopdate; my $times = timelocal($secs,$mins,$hours,$days,$months,$years); my $timet = timelocal($sect,$mint,$hourt,$dayt,$montht,$yeart); $time = $timet - $times; print $time; |
H friends,
Again i start getting error once the month changed. this is the error i am getting Month '12' out of range 0..11 at /var/lib/asterisk/agi-bin/test2.pl line 44 this is my code: PHP Code:
Thank You |
Code:
$ cat t.pl |
Be careful when working with dates. If there is no time zone as part of the date string, and you are in a region where there is daylight savings in effect for some of the year there, the exact date and time is ambiguous.
If you format dates without a time zone, you will have problems like this time sequence where the time jumps "back". I've actually seen this sort of thing in production environment, and it can be a problem: Code:
Sun Oct 28 01:58:10 2007 Code:
Sun Oct 28 01:58:10 BST 2007 |
All times are GMT -5. The time now is 10:47 PM. |