Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

get remaining hours

by doubledecker (Scribe)
on May 06, 2013 at 04:29 UTC ( #1032169=perlquestion: print w/ replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Monks..

I'm trying to find out remaining difference of the day in hours, but not sure of how to proceed further. Here is what I've tried so far...

#!/usr/bin/perl use strict; use DateTime; my $start = '2013/01/08 09:26:07'; my $end = '2013/04/11 20:19:12'; my @start_list = split /[ :\/]/, $start; my @end_list = split /[ :\/]/, $end; my %start; @start{qw[year month day hour minute second]} = @start_list; my %end; @end{qw[year month day hour minute second]} = @end_list; my $start = DateTime->new(%start); my $end = DateTime->new(%end);

I want to get remaining hours left for start date and total hours elapsed for end date. any help is appreciated

Comment on get remaining hours
Download Code
Re: get remaining hours
by Kenosis (Priest) on May 06, 2013 at 06:17 UTC

    Perhaps the following will be helpful:

    use strict; use warnings; use Date::Parse; my $start = '2013/01/08 09:26:07'; my $end = '2013/04/11 20:19:12'; print sprintf "%.2f", ( str2time($end) - str2time($start) ) / 3600;

    Output:

    2241.88

    str2time parses a date/time string, returning a unix time value in seconds. It's used above to calculate the difference (delta) between your start and end dates/times. The output is the difference in hours.

Re: get remaining hours
by Anonymous Monk on May 06, 2013 at 07:06 UTC
Re: get remaining hours
by sundialsvc4 (Monsignor) on May 06, 2013 at 13:48 UTC

    The CPAN library, at http://search.cpan.org, contains thousands of packages which deals with dates, such as Date::Calc, Date::Manip, or Date::Simple.

    For example, click on the Date::Simple link and look at the EXAMPLES section:

    use Date::Simple (); my $date = Date::Simple->new('1972-01-17'); ...

    An opaque object as provided by this particular (pre-built, pre-tested) CPAN package, has among other things the ability to parse a date-string such as the one that you have, and to “do things with” the resulting date-value.   You do not have to be concerned with exactly how the magic works, because the logic has been rigorously tested (even as part of the process of installing the package on your system).   To you, that object “is a” date, and a very smart one at that.

    Although this particular package deals only with dates, not hours ... have a peek now at, say, Date::Calc::Object or Date::Calc.   :-)   Truly, there is nothing having to do with dates ... no, not even in the Mayan calendar (DateTime::Calendar::Mayan), if you are so inclined, whereby you must “roll your own.”

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032169]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-07-14 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls