converting date/time to timestamp

Discussion in 'Perl Misc' started by Peter Pippinger, Oct 21, 2003.

  1. Hello NG,

    i know, this might be also a FAQ, but i havn´t found this in the Perldoc. Maybe
    someone can point me to the right place in the Handbook?

    I have two variables:
    $date="10/21/2003"
    $time="18:30"

    And i want to have the UNIX-timestamp for these variables in $timestamp.

    How can i convert this?

    Thanx for any hints and best regards!
    Peter
     
    Peter Pippinger, Oct 21, 2003
    #1
    1. Advertising

  2. Peter Pippinger

    David Oswald Guest

    "Peter Pippinger" <> wrote in message
    news:bn2mhu$frp$03$-online.com...

    > i know, this might be also a FAQ, but i havn´t found this in the Perldoc.

    Maybe
    > someone can point me to the right place in the Handbook?
    >
    > I have two variables:
    > $date="10/21/2003"
    > $time="18:30"
    >
    > And i want to have the UNIX-timestamp for these variables in $timestamp.


    The process is to convert those two variables to seconds since epoc, and
    then plug that into the 'localtime' function in scalar context. You can
    look at perlfaq4 from the POD that came with your Perl installation for a
    discussion of dates and times, and at perlfunc "localtime" and "time" for a
    discussion of Perl's built-in time functions.

    There is also a very convenient module on CPAN called Date::Manip. It, and
    Date::Transform are both very good at deciphering date/time strings in many
    different formats and converting them into a more standardized format. You
    can find those modules along with their documentation at search.cpan.org.

    Good luck.
     
    David Oswald, Oct 21, 2003
    #2
    1. Advertising

  3. Andrew Shitov wrote:
    >
    > use Time::Local;
    >
    > $date="10/21/2003";
    > $time="18:30";
    >
    > my ($mon, $mday, $year) = split /\//, $date;
    > my ($hour, $min) = split /:/, $time;
    > print timelocal (0, $min, $hour, $mday, $mon, $year);


    That prints the number of epoch seconds representing November, 21.
    -----------------------------------------------------^^^^^^^^

    The last line should rather be:

    print scalar localtime timelocal(
    0, $min, $hour, $mday, $mon - 1, $year);

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 21, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phillip B Oldham

    Convert date/time to unix timestamp?

    Phillip B Oldham, Feb 10, 2009, in forum: Python
    Replies:
    2
    Views:
    4,501
    M.-A. Lemburg
    Feb 10, 2009
  2. Replies:
    1
    Views:
    992
    Chris Rebert
    Jul 22, 2010
  3. Andyza
    Replies:
    6
    Views:
    1,493
    Evertjan.
    Mar 5, 2009
  4. Replies:
    6
    Views:
    381
    Dr J R Stockton
    Mar 6, 2007
  5. Sadia Bashir
    Replies:
    0
    Views:
    151
    Sadia Bashir
    Jan 30, 2014
Loading...

Share This Page