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
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'])));
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');
<?php
$jsDateTS = strtotime($jsDate);
if ($jsDateTS !== false)
date('Y-m-d', $jsDateTS );
else
// .. date format invalid