Carlvic Japitana Lim
Carlvic Japitana Lim

Reputation: 149

Insert into date column as 0000-00-00

Good day,

I try to import data to my database using php and mysql script but on date column insert as 0000-00-00 instead of its actual date. Please check my code below

if (isset($_POST['submit'])) {
    $i=0;

if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";


}

date('Y-m-d', strtotime(`Closed On`));
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if($i>0) {
    $import="INSERT into ".$user."_products (`Invoice No`,`Receipt No`,`Category`,`Sub Category`,`ProductName`,`Sold On`,`Guest`,`GuestCenter`,
        `Employee Code`,`SoldBy`,`Quantity`,`Promotion`,`Price`,`Discount`,`Prepaidcard Redemption`,`Net Price`,`Tax`,`ShippingCharge`,
        `Package Redemption`,`Price Paid`,`Sale Value`,`Payment Type`,`Createdby`,`Status`,`Closed On`,`GuestCode`,`FirstVisit`,`Member`,`ClosedBy`,`BusinessUnit`) 
        values('$data[0]','$data[1]','$data[2]','$data[3]','".mysql_real_escape_string($data[4])."','$data[5]','".mysql_real_escape_string($data[6])."','$data[7]','$data[8]','$data[9]','$data[10]',
        '$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]',
        '$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]')";

        mysql_query($import) or die(mysql_error());
    }
    $i++;
}

Note: My date column is Closed On on my database.

Thanks !

Upvotes: 3

Views: 2111

Answers (3)

Pupil
Pupil

Reputation: 23948

You need to pass the string now to strtotime()

This will returns current time in UNIX format.

date('Y-m-d', strtotime('now'));

Your statement has two issues:

date('Y-m-d', strtotime(`Closed On`));

1) Closed On: does not evaluate to anything.

2) Enclosing function parameters with backtick is parse error. Parameters should be enclosed with single quotes.

Upvotes: 2

user5824966
user5824966

Reputation:

use this

date('Y-m-d', strtotime($data[24]));

Upvotes: 2

Gagan Upadhyay
Gagan Upadhyay

Reputation: 255

to insert date or timestamp into database you need to create date object. use this

date('y-m-d', strtotime($data['closed_on']))

Upvotes: 2

Related Questions