Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL

前端 未结 8 1793
南笙
南笙 2020-11-29 11:18

MySQL stores the date in my database (by default) as \'YYYY-MM-DD\' The field type for my date is \'DATE\' (I do not need any time storage).. Is there a simple way to change

相关标签:
8条回答
  • 2020-11-29 11:39

    Just use the Mysql built in function DATE_FORMAT()

    SELECT DATE_FORMAT(some_date_field, "Y/m/d");
    
    0 讨论(0)
  • 2020-11-29 11:43

    i'm using this script and put it on upper line.

    $(function(){
        var pickerOpts = {
            dateFormat: "yy-mm-dd"
        };  
        $("#datepicker").datepicker(pickerOpts);
    });
    

    and this one in your form.

    <input id=**"datepicker" type="date"** name="Start"  size="9" value="<?php  
    
    echo $Start;
    
    ?>" />
    

    It will appear as d/m/y on your page but in your database y/m/d.

    0 讨论(0)
  • 2020-11-29 11:51

    You can display you date in any format you want in your pages, in mysql i realy don't know, in php you can use this function: date ( string $format [, int $timestamp ] ). So you can do this:

    echo date( "d/m/Y", strtotime ( $your_date ) );

    You can find full description here: http://php.net/manual/en/function.date.php

    0 讨论(0)
  • 2020-11-29 11:52

    If you used some Framework like Zend, it would be very easy because you have plenty of classes to handle databases and date formatting. Then you would always see the output as the Brazilian date format. Just like Delphi TDateField uses Windows date format in the current computer, Zend will look up in your own configuration.

    0 讨论(0)
  • 2020-11-29 11:58

    In PHP, you could :

    • Transform the date to a timestamp, using strtotime
    • Format it, using date

    A bit like this, I'd say :

    $timestamp = strtotime($date_from_db);
    echo date('d/m/Y', $timestamp);
    

    But this will only work for dates between 1970 and 2038, as timestamps are stored as 32 bits integers, counting from 1970-01-01.


    In MySQL, I suppose the date_format function would do the trick.
    For example :

    mysql> select date_format(curdate(), '%d/%m/%Y');
    +------------------------------------+
    | date_format(curdate(), '%d/%m/%Y') |
    +------------------------------------+
    | 19/03/2010                         |
    +------------------------------------+
    1 row in set (0.03 sec)
    


    And, for the sake of completness, another solution, in PHP, that doesn't suffer from the limitation of 1970-2038 would be to use the DateTime class, and, especially :

    • DateTime::__construct to parse the date returned by the DB
    • DateTime::format to format the date to whatever format you want.

    For example, this portion of code :

    $date = new DateTime('2010-03-19');
    echo $date->format('d/m/Y');
    

    would get you this output :

    19/03/2010
    
    0 讨论(0)
  • 2020-11-29 11:58

    After getting it from the DB, use $time = strtotime($value) to convert it to a timestamp in PHP. Then use date("d/m/Y", $time) to get it in that format.

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