mysql date formatting with php

前端 未结 6 1758
庸人自扰
庸人自扰 2021-01-29 02:56

I have a date (\"mm/dd/yyyy\") and I want to convert it to a MySQL DATE data type (like yyyy-mm-dd)

How do I do it with PHP?

相关标签:
6条回答
  • 2021-01-29 03:24

    Nick rulez's answer also applies to inserts and updates:

    INSERT INTO my_table (id, date) values (1, str_to_date('10/30/2010','%m/%d/%Y'))
    
    0 讨论(0)
  • 2021-01-29 03:24

    Lots of ways to do it. I like always converting my dates to a timestamp cause I find it easiest to then do what I want.

    In which case:

    <?php
    echo date( "Y-m-d",strtotime("09/02/1988"));
    ?>
    

    http://codepad.viper-7.com/Z9vDv7

    0 讨论(0)
  • 2021-01-29 03:28

    Take a look at mysql function str_to_date()

    example

    select str_to_date('10/30/2010','%m/%d/%Y') -- 2010-10-30
    
    0 讨论(0)
  • 2021-01-29 03:28
    $date = '12/25/2011';
    $parts = explode('/', $date);
    $sql_date = $parts[2] . '-' . $parts[0] . '-' . $parts[1];
    
    0 讨论(0)
  • 2021-01-29 03:33

    If your date is in $your_date, then:

    $mysql_date = date('Y-m-d', strtotime($your_date));
    

    See strtotime() documentation.

    0 讨论(0)
  • 2021-01-29 03:33
    function date2mysql($date) {
    
       list($month, $day, $year) = explode('/', $date);
       $timestamp = mktime(0, 0, 0, $month, $day, $year);
       return date("Y-mm-d",$timestamp);
    }
    

    see the date manual for the format

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