converting date/time to timestamp

P

Peter Pippinger

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
 
D

David Oswald

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.
 
G

Gunnar Hjalmarsson

Andrew said:
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top