in reply to String (date) to time (integer)
Use Time::Local for the function timelocal, which is the inverse of the builtin localtime.
For example:
use strict; use warnings; use Time::Local; my $date = "06/06/2009"; my ($m,$d,$y) = $date =~ m|(\d+)/(\d+)/(\d+)|; my $timet = timelocal(0, 0, 0, $d, $m-1, $y); print "Date '$date' => $timet\n" # Updated -- check the reverse my $ltime = localtime($timet); print "$timet => $ltime\n"; # Results: # Date '06/06/2009' => 1244260800 # 1244260800 => Sat Jun 6 00:00:00 2009
Note that the first three arguments to timelocal above represent (respectively) the number of seconds, minutes, and hours; in your case they can be zero, as you're only interested in a one-day granularity.
Update: As Corion reminds me, the month passed to timelocal needs to be zero-based, not one-based the way humans represent dates. Changing "$m" to "$m-1" in my code fixed this (thanks, Corion!).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: String (date) to time (integer)
by Corion (Patriarch) on Jun 06, 2009 at 14:30 UTC | |
by northwestdev (Acolyte) on Jun 06, 2009 at 15:01 UTC | |
by Corion (Patriarch) on Jun 06, 2009 at 15:05 UTC | |
|
Re^2: String (date) to time (integer)
by dsheroh (Monsignor) on Jun 07, 2009 at 13:59 UTC |