stash_man
stash_man

Reputation: 319

strtotime returning false date

I have a problem with strtotime function in php, i'm trying to convert mm-dd-yyyy to yyyy-mm-dd. The date is entered on the text box and on submit it should save the date to the database. the problem is it returns a wrong date(1970-01-01) each time,which means my code isn't taking the variable i use to store the date, My code is:

//dateconvert
$submitdate = date($_POST['date']);
$date   = date("Y-m-d", strtotime($submitdate));

//storeindb
$query ="INSERT INTO ticket SET date = '$date'";
$result = mysql_query($query);

I'm a newbie,please help.

Upvotes: 1

Views: 3010

Answers (3)

Joke_Sense10
Joke_Sense10

Reputation: 5412

date format in $submitdate is incorrect Because the format yyyy/mm/dd should be yyyy-mm-dd so you will need to replace your / characters.

Try this:

$submitdate = str_replace("/","-",$_POST['date']);
echo date('Y-m-d', strtotime($submitdate));

Upvotes: 1

Jenz
Jenz

Reputation: 8369

Use the code as:

$submitdate = $_POST['date'];
$date = date("Y-m-d", strtotime($submitdate));

Hope this hepls.

Upvotes: 1

Jonathon
Jonathon

Reputation: 16333

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

http://php.net/manual/en/function.strtotime.php

You need to use the forward slash separator for mm/dd/yyyy formats.

Upvotes: 2

Related Questions