0

Hope you ok expert!! I just want to know whats wrong with my table, I created table 'table1' by using php with the following instruction.

<?php
        // use mysql connect

    $num="create table table1
     (student_id int primary key NOT NULL AUTO_INCREMENT,
       name varchar (10) NOT NULL,
          math varchar (20) NOT NULL,
          english varchar (10) NOT NULL,
     date DATE,
      month DATE,
         year DATE )";


   mysql_query($num,$con);
     mysql_close($con);
        ?> 

My question is why? when am input date,month,year it show something like this

    student_id name   math  english date       month        year
    1          simon  100   100     0000-00-00  0000-00-00  0000-00-00

And I expect this to be

    student_id name   math  english date  month   year
    1          simon  100   100     1     March   2013

Like this may be.

    <?php
    // use mysql connect

$num="create table table1
 (student_id int primary key NOT NULL AUTO_INCREMENT,
   name varchar (10) NOT NULL,
      math varchar (20) NOT NULL,
      english varchar (10) NOT NULL,
 date DATE strtotime,
  month DATE strtotime,
     year DATE strtotime)";


         mysql_query($num,$con);
                mysql_close($con);
    ?> 

2 Answers 2

2

Because you told it to store a date?

Dates are stored in the format YYYY-MM-DD. You then use PHP's strtotime to convert to a UNIX timestamp, then date to output it in the format of your choice (something like d/M/Y for "1/Mar/2013")

2
  • Ok, So may i put strtotime after the date eg DATE strtotime or?
    – Haika
    Nov 8, 2013 at 1:24
  • I said PHP's strtotime. That means you call strtotime() in PHP... Nov 8, 2013 at 9:00
0

You only need one DATE column in your table to store the century+year, month and day components.

To extract those components into separate columns,

SELECT DATE_FORMAT(mydatecol,'%M') AS month_name
     , DATE_FORMAT(mydatecol,'%e') AS day_of_month
     , DATE_FORMAT(mydatecol,'%Y') AS year
  FROM ... 

To insert a date value

INSERT INTO ... (..., mydatecol, ... ) VALUES (... ,'2013-03-01', ... )

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.