DATETIME data type. Changing time from 24hr to 12hr.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

DATETIME data type. Changing time from 24hr to 12hr.

Post by mattyboi »

Any ideas how I can change the time from 24hr time to 12hr time, and vise versa.

Input on a form element would be in 12hr time, would have to format it to 24hr time to store it in the TIME data type. Output would be in 24hr time would like to display it in 12hr time on the page.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Code: Select all

$hour += ($afternoon) ? 12 : 0;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Post by mattyboi »

can you explain how this works. still learning.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When someone enters a time, is there a checkbox or radio button to set whether or not the time is AM or PM? When the form is submitted, check the value of that variable and add 12 if it's set to PM.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

SELECT DATE_FORMAT('datecolumn', '%r') AS nicetime
FROM table

For 12am -> datetime i'd have a look at (php) strtotime and mysql FROM_UNIXTIME functions.
Post Reply