简体   繁体   中英

AM PM not inserting into mysql table

Have a field curdate, like

 <?php
    $date = date_default_timezone_set('Asia/Kolkata');
    $time= date("l, F d, Y h:i:s A"); 
    ?>
    <input type="hidden" value="<?php echo $time; ?>" name="curdate" id="curdate"/>

it will take the current date and time to database.

There is another column 'curdatesql', it will take the above time to mysql time format:

 "' . $this->SanitizeForSQL($formvars['curdate']) . '",
 STR_TO_DATE("' . $this->SanitizeForSQL($formvars['curdate']) . '","%W, %M %d, %Y %h:%i:%s %p"),

The problem is its not taking the AM, PM part into mysql table...Why is that?

Its taking like : 2013-07-20 13:26:35 (No AM PM present).

MySQL has several date and time formats . You seem to use DATETIME which displays values in the form YYYY-MM-DD HH:MM:SS . Note that HH is a 24-hour format.

You should not store AM/PM extra, as this would be completely redundant. In the best case, you would waste memory, in worst case you would get inconsistencies.

Instead, you should handle this in the presentation layer:

if(hh >= 12) {
    PM
} else {
    AM
}

hours = hh % 12

Note that you can use MySQL DATE_FORMAT to get the format you like. It could look like this:

SELECT DATE_FORMAT(myStoredDatabaseAttribute, '%h %p') FROM myTable

My sql the date is stored in 24 hours format and also in binary. If the representation requires a 12 hour display format you will have to handle it in the presentation layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM