Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

converting time into 24hr

Quote Reply
converting time into 24hr
hi

if I've got a form passing in the following variables...

Hours.... 1-12
Minutes... 0-55
AM or PM

So as a result I may end up with...

01 25 PM

What I want to do is convert that into 24 hour format if PM is selected as apposed to am. So my question is, how do I convert this to 24 hour format (without adding 13,14 etc to the form)?

01:25:00

thanks

regan
Quote Reply
Re: [ryel01] converting time into 24hr In reply to
Hi. The following should get you on the right tracks :)

Code:
my $time;
my $hour = $IN->param('hour');
my $min = $IN->param('min');
my $ampm = $IN->param('ampm');

# add 12 hours if its in the PM... should give us
# 24 hour time. i.e 3pm, would become 15.00
if ($ampm =~ /pm/i) { $hour += 12; }
if ($hour == 24) { $hour = 0; }

my $time = qq|$hour:$min$ampm|;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Aug 13, 2004, 12:32 AM
Quote Reply
Re: [Andy] converting time into 24hr In reply to
that works until 12 + 12 = 24, and 10 minutes...

which would read 24:10:00

but should be 00:10:00
Quote Reply
Re: [ryel01] converting time into 24hr In reply to
Hi. Good point :D

Try adding this line in;

if ($hour == 24) { $hour = 0; }

Its dirty, but should do the job.

If you are looking at doing this better, you may want to take a look at GT::Date, where it has the date_sub and date_add options (not sure if it supports hours).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] converting time into 24hr In reply to
hmmm... interesting one...

you also need to set the hour to 0 if AM and the hour 12 is selected...

Code:
if ( $ampm eq 'PM' ) {
$hour += 12 unless $hour eq '12' ;
}
if ( $ampm eq 'AM' ) {
$hour = '00' if $hour eq '12' ;
}

that seems to do the trick. Crazy


r
Quote Reply
Re: [ryel01] converting time into 24hr In reply to
Quote:
ryel01
Veteran

Didn't realise you had gone Veteran :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] converting time into 24hr In reply to
LOL

GForum Suggestion...

Make the user ratings based on "Answers" not number of questions posted.

Wink

regan
Quote Reply
Re: [ryel01] converting time into 24hr In reply to
Quote:
Make the user ratings based on "Answers" not number of questions posted.

Hell, the first 1-2,000 of my posts were pretty much all questions too :D

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [ryel01] converting time into 24hr In reply to
If you want to use GT::Date, something like this should work:use GT::Date;
my $hour = $IN->param('hour');
my $min = $IN->param('min');
my $ampm = $IN->param('ampm');
my $time = "$hour:$min:$ampm";
my $newtime = GT::Date::date_transform($time,"%hh%:%MM%:%tt%","%HH%:%MM%:%ss%");
Quote Reply
Re: [afinlr] converting time into 24hr In reply to
Hi.

Or just the following in the template whereever needed (if i understand the requirements correctly):

<%GT::Date::date_get('', "%ddd%. %mmm% %d%, %yyyy% %HH%:%MM%:%ss%")%>

Thanks
Anup

Last edited by:

anup123: Aug 13, 2004, 8:58 AM