Convert a Javascript Date format to desired PHP format

后端 未结 3 594
情深已故
情深已故 2020-12-18 23:10

How can we convert Wed Jun 09 2010 which is returns from a javascript function and get in a php script.I need to convert this date format to 2010-06-09.Thanks

相关标签:
3条回答
  • 2020-12-18 23:49

    Either you can send the javascript date to php via query string:

    var mydate = encodeURIComponent('Wed Jun 09 2010');
    document.location.href = 'page.php?date=' + mydate;
    

    PHP

    echo date('Y-m-d', strtotime(urldecode($_GET['date'])));
    

    Or though a hidden field:

    echo date('Y-m-d', strtotime(urldecode($_POST['date'])));
    
    0 讨论(0)
  • 2020-12-18 23:55

    Or with DateTime:

    $date = new DateTime('Wed Jun 09 2010');
    echo $date->format('Y-m-d');
    

    The date format you can input to strtotime(), DateTime and date_create() are explained in the PHP manual. If you need more control over the input format and have PHP5.3, you can use:

    $date = DateTime::createFromFormat('D M m Y', 'Wed Jun 09 2010');
    echo $date->format('Y-m-d');
    
    0 讨论(0)
  • 2020-12-18 23:59
    <?php
    $jsDateTS = strtotime($jsDate);
    if ($jsDateTS !== false) 
     date('Y-m-d', $jsDateTS );
    else
     // .. date format invalid
    
    0 讨论(0)
提交回复
热议问题