16

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 Answers 3

22
<?php
$jsDateTS = strtotime($jsDate);
if ($jsDateTS !== false) 
 date('Y-m-d', $jsDateTS );
else
 // .. date format invalid
Sign up to request clarification or add additional context in comments.

1 Comment

Nice because you are also checking the invalid date format.
11

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');

Comments

4

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'])));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.