tags:

views:

293

answers:

2

I m entering date in front side as 10:00 AM 12:00 PM etc...( means AM/PM format). now i want to save that value in databse in time datatype field, how do i save that am pm value into time datatype in mysql and again want to show as am pm on front side??

A: 

use mysql function DATE_FORMAT with %p parameter

Haim Evgi
+3  A: 

To insert:

# replace first argument of STR_TO_DATE with value from PHP/frontend
TIME( STR_TO_DATE( '10:00 PM', '%h:%i %p' ) );

To select:

# replace first argument with your time field
TIME_FORMAT( '22:00:00', '%h:%i %p' );

EDIT:
I'll just go ahead and presume you use mysql lib functions.

// first sanitize the $_POST input
// also, make sure you use quotes to identify the $_POST keys
$open = mysql_real_escape_string( $_POST[ 'MondayOpen' ] );
$close = mysql_real_escape_string( $_POST[ 'MondayClose' ] );

// this is the query, which should work just fine.
$sql = '
    INSERT INTO
        `table_lib_hours`
    SET
        `day_name` = "Monday",
        `day_open_time` = TIME( STR_TO_DATE( "' . $open . '", "%h:%i %p" ) ),
        `day_close_time` = TIME( STR_TO_DATE( "' . $close . '", "%h:%i %p" ) )
    ';

$result = mysql_query( $sql );

Then to retrieve the values:

$sql = '
    SELECT
        `day_open_time`,
        `day_close_time`,
        TIME_FORMAT( `day_open_time`, "%h:%i %p" ) as day_open_time_formatted,
        TIME_FORMAT( `day_close_time`, "%h:%i %p" ) as day_close_time_formatted
    FROM
        `table_lib_hours`
    WHERE
        `day_name` = "Monday"
    ';

$resultset = mysql_query( $sql );

This will return a result set where the formatted data is in the *_formatted fields

EDIT:
Adjusted %m (month) to %i (minutes). A thank you to Donny for the well spotted slip up.

fireeyedboy
`INSERT INTO table_lib_hours SET day_name='Monday', `day_open_time=TIME(STR_TO_DATE( '$_POST[MondayOpen]', '%h:%m %p' )),` `day_close_time=TIME(STR_TO_DATE( '$_POST[MondayClose]', '%h:%m %p' ))`display error`Column 'day_open_time' cannot be null`
diEcho
First of: you shouldn't use $_POST directly in your queries... really. You should sanitize the values first. But to better help you: can you also show the PHP code where you execute this query? (Please update your question, rather then post a comment, if you will. Because that's easier to read. Thanks).
fireeyedboy
`TIME( STR_TO_DATE( "' . $open . '", "%h:%m %p" ) )` gives parse error
diEcho
it should be`TIME( STR_TO_DATE( '" . $open . "', '%h:%m %p' ) )`
diEcho
when i run above codeit display error `day_close_time can not be null`
diEcho
@I Like PHP: yes, if you use double quotes as the string delimiter for `$sql`. But as you can see in my example I use single quotes.
fireeyedboy
@I Like PHP: Make sure you use appropriate quoting. So, If you are using double quotes around the `$sql` string, then you need single quotes as string delimiters around values you insert in MySQL and vice versa.
fireeyedboy
The reason I use single quotes for PHP strings is that PHP parses variables in double quoted strings. I like to avoid this. I only even explicitly insert variables in PHP string by using the concatenation operator (the dot `.`).
fireeyedboy
Correction, should be:`TIME_FORMAT( '22:00:00', '%h:%i %p' );` See here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Donny Kurnia
@Donny: oops, you're right. I'll adjust it. Thank you for the correction.
fireeyedboy