Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 41481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:11:15+00:00 2026-05-10T15:11:15+00:00

Given a date/time as an array of (year, month, day, hour, minute, second), how

  • 0

Given a date/time as an array of (year, month, day, hour, minute, second), how would you convert it to epoch time, i.e., the number of seconds since 1970-01-01 00:00:00 GMT?

Bonus question: If given the date/time as a string, how would you first parse it into the (y,m,d,h,m,s) array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:11:16+00:00Added an answer on May 10, 2026 at 3:11 pm

    This is the simplest way to get unix time:

    use Time::Local; timelocal($second,$minute,$hour,$day,$month-1,$year); 

    Note the reverse order of the arguments and that January is month 0. For many more options, see the DateTime module from CPAN.

    As for parsing, see the Date::Parse module from CPAN. If you really need to get fancy with date parsing, the Date::Manip may be helpful, though its own documentation warns you away from it since it carries a lot of baggage (it knows things like common business holidays, for example) and other solutions are much faster.

    If you happen to know something about the format of the date/times you’ll be parsing then a simple regular expression may suffice but you’re probably better off using an appropriate CPAN module. For example, if you know the dates will always be in YMDHMS order, use the CPAN module DateTime::Format::ISO8601.


    For my own reference, if nothing else, below is a function I use for an application where I know the dates will always be in YMDHMS order with all or part of the ‘HMS’ part optional. It accepts any delimiters (eg, ‘2009-02-15’ or ‘2009.02.15’). It returns the corresponding unix time (seconds since 1970-01-01 00:00:00 GMT) or -1 if it couldn’t parse it (which means you better be sure you’ll never legitimately need to parse the date 1969-12-31 23:59:59). It also presumes two-digit years XX up to ’69’ refer to ’20XX’, otherwise ’19XX’ (eg, ’50-02-15′ means 2050-02-15 but ’75-02-15′ means 1975-02-15).

    use Time::Local;  sub parsedate {    my($s) = @_;   my($year, $month, $day, $hour, $minute, $second);    if($s =~ m{^\s*(\d{1,4})\W*0*(\d{1,2})\W*0*(\d{1,2})\W*0*                  (\d{0,2})\W*0*(\d{0,2})\W*0*(\d{0,2})}x) {     $year = $1;  $month = $2;   $day = $3;     $hour = $4;  $minute = $5;  $second = $6;     $hour |= 0;  $minute |= 0;  $second |= 0;  # defaults.     $year = ($year<100 ? ($year<70 ? 2000+$year : 1900+$year) : $year);     return timelocal($second,$minute,$hour,$day,$month-1,$year);     }   return -1; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remotely tag a CVS archive for a given date and time,
I have noticed that regardless of a given script's execution time, every date() call
I want to check if a given date is more than a month earlier
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
Given this 3D bar graph sample code , how would you convert the numerical
Given a datetime.time value in Python, is there a standard way to add an
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a date range, I need to know how many Mondays (or Tuesdays, Wednesdays,
I want to convert an int to a byte[2] array using BCD. The int
I am trying to get an array of a date plus the next 13

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.