How to convert MM/DD/YYYY to YYYY-MM-DD?

前端 未结 10 1763
忘了有多久
忘了有多久 2020-12-14 18:03

I have a jquery calendar that sets the input value to MM/DD/YYYY

How would I convert it so that my database column (date) can accept it correctly?

EDIT

相关标签:
10条回答
  • 2020-12-14 18:34

    You want to do this in PHP, right?

    1. Use Explode

      $christmas = "12/25/2010";
      $parts = explode('/',$christmas);
      $yyyy_mm_dd = $parts[2] . '-' . $parts[0] . '-' . $parts[1]
      
    2. Use strptime to parse it to a timestamp and then strftime to format it the way you want it.

    0 讨论(0)
  • 2020-12-14 18:38

    We need more information?

    1) What script is inserting into database? I am assuming PHP

    2) Also we need to know how you are storing the date and in what format?

    My solution would be:

    $dates = preg_split('/\//',$_POST['date']);
    
    $month = $dates[0];
    $day = $dates[1];
    $year = $dates[2];
    
    $finalDate = $year.'-'.$month.'-'.$day;
    
    0 讨论(0)
  • 2020-12-14 18:39

    Try the following code,

    <?php
    $date = "10/24/2014";
    $date = DateTime::createFromFormat("m/d/Y" , $date);
    echo $date->format('Y-m-d');
    ?>
    
    0 讨论(0)
  • 2020-12-14 18:45

    Try this:

    $date = explode('/', '16/06/2015');
    
    $new  = date('Y-m-d H:i:s', strtotime(implode('-', array_reverse($date))));
    

    Returns: 2015-06-15 00:00:00

    Hope this helps!

    0 讨论(0)
  • 2020-12-14 18:46

    Try the following code:

    $date = "01-14-2010";
    echo $your_date = date("Y-m-d", strtotime($date));
    

    It will echo 1969-12-31.

    0 讨论(0)
  • 2020-12-14 18:48

    if you just want one line try this:

    $time = "07/12/2010";
    
    $new_time = preg_replace("!([01][0-9])/([0-9]{2})/([0-9]{4})!", "$3-$1-$2", $time);
    
    var_dump($time);
    var_dump($new_time);
    
    0 讨论(0)
提交回复
热议问题