user3021251
user3021251

Reputation: 9

date format in php is giving wrong result

I have created one form as following:

<form action="test.php" method="POST" name="edituser">
<input type="text" id="to_date_picker" placeholder="Select Date" name="todate" class="input" size="50" />
<input name="addnew" type="submit" value="Add &amp; Save" id="submitbtn" />
</form>
<script type="text/javascript">
$(function() {
$('#to_date_picker').datepick();
});
</script>

test.php: is as following:

$todate = $_POST['todate'];
$newtodate = date("Y-m-d", strtotime($todate));
 echo $newtodate."<br>\n";

here it is giving wrong output. here date picker is a calender from which date is picked.

Upvotes: 0

Views: 119

Answers (4)

user1717044
user1717044

Reputation: 61

You can use new DateTime($todate); if you have PHP 5 >= 5.2.0 which is easier to use in my own opinion. Or if you don't want tu change the format return by your JavaScript, use the method DateTime::createFromFormat('j-M-Y', '15-Feb-2009'); which returns a DateTime object from a "custom" date (see documentation, link below).

Link : http://www.php.net/manual/fr/datetime.construct.php http://www.php.net/manual/fr/datetime.createfromformat.php

Upvotes: 0

Uday Hiwarale
Uday Hiwarale

Reputation: 4157

use this instead

$('#to_date_picker').datepick(
dateFormat: 'dd-mm-yy'
);

Upvotes: 0

GK Soft
GK Soft

Reputation: 103

You can search for mktime function, then you can parse your datetime for the proper format.

Upvotes: 0

user2776543
user2776543

Reputation:

What is your dateformat? Because the default dateformat for datepick is "mm/dd/yyyy". strtotime accept this format for date:

American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6"

I think that your datepick returns 01/17/2006. Look for this.

Upvotes: 1

Related Questions