Link to home
Start Free TrialLog in
Avatar of Richard Winnick
Richard Winnick

asked on

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 -

Richard
Avatar of star_trek
star_trek

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");
Avatar of Tintin
What sort of date/time manipulations do you want to do?
Avatar of Richard Winnick

ASKER

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 -

Richard
Hi Again -

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

R
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 -

Richard