简体   繁体   English

AM PM未插入mysql表

[英]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: 还有另一列“ curdatesql”,它将花费以上时间到mysql时间格式:

 "' . $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? 问题是它没有将AM,PM部分放入mysql表中...为什么呢?

Its taking like : 2013-07-20 13:26:35 (No AM PM present). 它的取像是:2013-07-20 13:26:35(不存在AM PM)。

MySQL has several date and time formats . MySQL有几种日期和时间格式 You seem to use DATETIME which displays values in the form YYYY-MM-DD HH:MM:SS . 您似乎使用的DATETIME以YYYY-MM-DD HH:MM:SS的形式显示值。 Note that HH is a 24-hour format. 请注意, HH是24小时制。

You should not store AM/PM extra, as this would be completely redundant. 您不应该额外存储AM / PM,因为这将完全多余。 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. 请注意,您可以使用MySQL DATE_FORMAT获得所需的格式。 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. 我的sql日期以24小时格式以及二进制格式存储。 If the representation requires a 12 hour display format you will have to handle it in the presentation layer. 如果表示形式需要12小时的显示格式,则必须在表示层中进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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