There's a standard module (as in "
comes with perl"),
Time::Local, which provides inverse functions for
localtime and
gmtime: timelocal() and timegm(). These can convert dates/times in a list format, (ss, mm, hh, dd, MM, yyyy), to an epoch time. As explained in
How do I find the difference in days between two dates, in a cool perl way? which was pointed to in another reply, this is a number of seconds since, typically, January 1st 1970, 00:00:00 GMT. That is the case on Unix and Windows, at least.
So first you must get your year/month/day out of the string:
$date = '05-02-2003';
my($d,$m,$y) = $date =~ /(\d+)-(\d+)-(\d+)/;
Subtract 1 from the month, as January is month 0. Seconds since the epoch can be found like this:
use Time::Local;
my $time = timegm(0, 0, 0, $d, $m-1, $y);
I use midnight, and GMT, as a reference, with regards to summer time issues — GMT doesn't have them. If you desire finer results, with an exact hours/minutes/seconds count, you can always use timelocal(), and real times.
The calculated value is 1044403200. You can see that it works well, like this:
print scalar gmtime($time);
which produces
Wed Feb 5 00:00:00 2003
OK?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.