How to save date in MySQL database using PHP?

I want to store the date value in a MySQL database.

I have one PHP page called A.phpthat has 3 inputs: d, m, y, where d = (1 ... 31), m =(1 ... 12), and y = (1970 to 2009).

Now in b.phpI collect these values, and I need to save them in my db.

My database has a field with a name dobthat has a date type.

How can I construct a variable of type date from d, m, yand store it in the database?

+3
source share
5 answers

mysql "yyyy-mm-dd", , PHP, .

+8

:

$sql = 'insert into tbl (dob) values 
        (\'' . $month . '/' . $day . '/' . $year . '\')';

MySQL , .

, mysql_real_escape_string, SQL- .

+2

date, mktime():

$date = mktime(0, 0, 0, $month, $day, $year);

, :

$sql = "INSERT INTO table (dob) VALUES ('" . date('Y-m-d', $date) . "')";

, mySQLi:

$query = $db->prepare('INSERT INTO table (dob) VALUES (FROM_UNIXTIME(?))');
$query->bind_param('i', $date);
$query->Execute();
+1

UNIX db, ,

mktime()

, db,

date()

(, 31 2009 ., 10/31/2009 ..)

+1

. :

$dt = sprintf("%'04s-%'02s-%'02s",$y,$m,$d);
mysql_query("insert into mytable (dob) values ('$dt')");
+1

Source: https://habr.com/ru/post/1718543/


All Articles