convert string to date and time

Hi -

Very new to perl - have searched the questions and can't find an answer I can understand :-)

I have two varchar fields in my mysql db that contain a date and a time as strings in the following formats:

date field looks like 04/25/06

time field looks like 9:45 AM

Question is -

How can I convert these text strings into actual dates and times that perl can work with - for date and time manipulations -

thanks in advance -

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It depends on what module you use, I use Time::ParseDate
the following code would print me 2006042521:45:00

$d = "04/25/06";
$t = "9:45 PM";
print &ParseDate("$d $t");
What sort of date/time manipulations do you want to do?
rwinnickAuthor Commented:
Hi -
I am trying to "fix" some pre existing perl code that I inherited which doesn't work :-)

The problem goes like this :-)

I have a date field which has a text string like 04/25/06
I then have a time field which has a text string like 6:45 PM
I need to take these date and time strings and turn them into a time stamp field using local server time (which is PST)
I then need to adjust this timestamp to reflect the timezone the client is in - which is represented in another field as a one digit offset (0,1,2,3)

Up till now I have tried to get the year, month, day, hour, min, and am/pm by using substr() and adjusting for string length 6:45 PM vs 10:45 AM

I have then tried to use timelocal() to create the time stamp

and then try to add 12 hrs if time is pm -

I will then have to compare this timestamp to local server time and decide if an event should happen if the timestamp is less than local time -

Am getting results but they are way off -

I'm sure there has to be a more elegant and correct method of accomplishing this :-)

But after more hours than I care to remember I stuck!

thanks -

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rwinnickAuthor Commented:
Hi Again -

By the way I tried the time::parsedate - but get an error msg and it can't find the module

$d = "04/25/06";
$t = "9:45 PM";
($M,$D,$Y) = split'/',$d;
($h,$m,$p) = $t=~/(\d+):(\d+)\s*(PM)?/i;
$h += 12 if $p;
use Time::Local;
$timestamp = timelocal(0,$m,$h,$D,$M-1,$Y+2000-1900);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwinnickAuthor Commented:
Hi ozo -

That is soooo cool and elegant - and it works!!

Not only have you helped me with this problem - you've shown me a standard to work towards -

thanks -

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.