简体   繁体   中英

string to date using php or mysql

Can anyone please help me how to convert string to date

I have 3 dropdowns(year,month,date)and i got date value as 2012-january-12.

How can i convert it like 2012-01-12(yyyy-mm-dd).

$dob = $year.'-'.$month.'-'.$day;

I used str_to_date('$dob','%Y-%M-%e') in the query itself but its stops executing the query.

Here is the query

      $sql = "insert into issio_asc_workers (lname_fname_dob, asc_id, asc_lastname, asc_firstname, asc_middlename, asc_dob, asc_phone_mobile, asc_phone_work, asc_ssn,
              asc_email, asc_phone_preferred, asc_status, asc_emp_yesno, asc_emp_status, asc_user_type, asc_npi, asc_credentials, asc_committee, asc_speciality, 
              asc_group, asc_startdate)values ('$lfd', '$ascid', '$lname', '$fname', '$mname', 
             'str_to_date('$dob','%Y-%M-%e')', '$phCell', '$phOffice', '$ssn', '$email', '$preffered', '$asc_status', '$empType', '$asc_emp_status', '$asc_user_type', '$npi', 
             '$credential', '$commitee ', '$speciality', '$group', 'str_to_date('$sdate','%Y-%M-%e')')";

Thnaks In advance....,

$date = DateTime::createFromFormat('Y-m-d\TH:i:s', '2010-06-03T14:29:00');

Try this... Fill in the date with your information received from the form, then put the variable in the MySQL Insert query

It is a good idea to marshall your variables before you insert them into your sql query. This is no more difficult for the program to run, but makes your life as a programmer much easier:

$dob_asDate = str_to_date('$dob','%Y-%M-%e');

$sql = "insert into issio_asc_workers (lname_fname_dob, asc_id, asc_lastname, asc_firstname, asc_middlename, asc_dob, asc_phone_mobile, asc_phone_work, asc_ssn,
              asc_email, asc_phone_preferred, asc_status, asc_emp_yesno, asc_emp_status, asc_user_type, asc_npi, asc_credentials, asc_committee, asc_speciality, 
              asc_group, asc_startdate)values ('$lfd', '$ascid', '$lname', '$fname', '$mname', 
             '$dob_asDate', '$phCell', '$phOffice', '$ssn', '$email', '$preffered', '$asc_status', '$empType', '$asc_emp_status', '$asc_user_type', '$npi', 
             '$credential', '$commitee ', '$speciality', '$group', 'str_to_date('$sdate','%Y-%M-%e')')";

This way you can echo $dob_asDate and ensure it's what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM