1

I have an input field in my html page that gets Date from jQuery datepicker.

The format looks like this:

18/02/2017

I have a MYSQL field which is DATETIME

I need to insert the Date above into this MYSQL field.

So i tried this code:

$mysqlDate = date('Y-m-d H:i:s', strtotime($_POST['php_date']));

but the result of $mysqlDate is this:

1970-01-01 01:00:00

Could someone please advise on this issue?

Thanks in advance.

2
  • date("Y-m-d", strtotime($your_date) and try, I means remove his Commented Feb 18, 2017 at 13:15
  • @rahul_m, still the same. Commented Feb 18, 2017 at 13:16

2 Answers 2

2

Do one thing,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date'])));
echo $mysqlDate;

Give it a try,

it should work.

Note: 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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Source link.

Your new concern answer,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date']).' + '.rand(30,60*60*24*3).' seconds'));
Sign up to request clarification or add additional context in comments.

6 Comments

nice one... that did the trick. is there any way to add a random hour and minute to the $mysqlDate ?
Mate, your concern has been solved, as for question. Create new question regarding your this new problem , I assure you I will be there too. :)
Thanks ! If my answer solved your problem, you can accept my answer. Thanks again :)
will do.. its saying I need to wait for 2 minutes before I can accept it... :)
my heart not accepting this, check edit section of answer for your new concern. Thanks
|
2

You have to convert the posted date to a

YYYY-MM-DD

Format before converting it into date time.

You can do

$date=$_POST['php_date'];
list($day,$month,$year)=explode('/', $date);
$date=$year.'-'.$month.'-'.$day;

And then use $date.

2 Comments

si ricorda, the delimiter comes "primo" with explode() ;-) I fixed that. Otherwise, they'd be a conversion to string error.
Thanks mate! Didn't notice the mistake! :)

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.