changing format date MySql

后端 未结 4 578
南旧
南旧 2020-12-11 21:04

i have date type column in MySql. by default MySql date format is YYYY/MM/DD. but actualy i prefer to use DD/MM/YYYY format.

Could i change the column date f

相关标签:
4条回答
  • 2020-12-11 21:33

    Try echo date('d/m/Y', strtotime($date));

    This will take your existing date, convert it to UNIX time and transform it back to a date with your required format.

    0 讨论(0)
  • 2020-12-11 21:38

    As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like date column.

    I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:

    CREATE TRIGGER timestampper BEFORE INSERT ON table
    FOR EACH
    ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d/%m/%Y');
    
    CREATE TRIGGER timestampper2 BEFORE UPDATE ON table
    FOR EACH
    ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d/%m/%Y');
    

    You can't use TIMESTAMP or DATE as a column type, because these have their own format, and they update automatically.

    So, here's your alternative timestamp or date alternative! Hope this helped, at least I'm glad that I got this working.

    0 讨论(0)
  • 2020-12-11 21:48

    Other DBMS have a default format which you can change, even in a session basis. For instance, in Oracle you can simply change the NLS_DATE_FORMAT session variable and Oracle will use your custom format for both input and output. MySQL doesn't.

    There're a couple of server variables (date_format and datetime_format) that you can be tempted to consider suitable for this purpose:

    mysql> SHOW VARIABLES LIKE 'date%_format';
    +-----------------+-------------------+
    | Variable_name   | Value             |
    +-----------------+-------------------+
    | date_format     | %Y-%m-%d          |
    | datetime_format | %Y-%m-%d %H:%i:%s |
    +-----------------+-------------------+
    

    To begin with, they are server-side and they are not dynamic (they cannot be set at runtime). But the worse part is that they were never actually used. These variables have never affected date format at all. They're currently deprecated and queued for deletion.

    To sum up: no, you cannot. Date formats are fixed.

    0 讨论(0)
  • 2020-12-11 21:58

    No, you can't change the default MySQL format for DATE, DATETIME or TIMESTAMP columns.

    But you can use MySQL functions in your SQL statements to output a DATE expression as a string in different format.

    DATE_FORMAT( datecol , '%m/%d/%Y')  AS datecol
    

    (That will work fine in SELECT list, but avoid using this in any predicates (i.e. the WHERE clause). There, you'll want to reference the bare column, and convert strings of your preferred format 'MM/DD/YYYY' using the STR_TO_DATE function, e.g.

    datecol >= STR_TO_DATE('07/16/2012','%m/%d/%Y')
    

    With that said, I think you will really be better served using the MySQL default DATE format in your interactions with the database, and handling formatting changes in your code.

    0 讨论(0)
提交回复
热议问题