Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:34:55+00:00 2026-06-17T08:34:55+00:00

I have a script which imports a csv file into a MySQL table using

  • 0

I have a script which imports a csv file into a MySQL table using explode to separate the fields. my last field in the csv file is a date time field but in the incorrect format as per image below:
enter image description here

I want to use the below code to insert the csv into MySQL and change the format of the last column from mm/dd/yyyy hh:mm:ss am/pm to the MySQL format of yyyy-mm-dd hh:mm:ss

Existing working script (just doesn’t import date time field as format is wrong):

$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "LoadsOverWB.csv";
foreach(split($lineseparator,$csvcontent) as $line) {

    $lines++;

    $line = trim($line," \t");

    $line = str_replace("\r","",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    $line = str_replace("'","\'",$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode("','",$linearray);

    if($addauto)
        $query = "replace into $databasetable(loadnumber,weighbillnumber,VehicleRegistration,haulier,vehicleweight,rolledproductkg,hegrosskg,roofinhkg,nonmetalkg,wbtotalkg,datetime) values('','$linemysql');";
    else
        $query = "replace into $databasetable(loadnumber,weighbillnumber,VehicleRegistration,haulier,vehicleweight,rolledproductkg,hegrosskg,roofinhkg,nonmetalkg,wbtotalkg,datetime) values('$linemysql');";

    $queries .= $query . "\n";

    @mysql_query($query);
}

Thanks in advance as always, much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:34:56+00:00Added an answer on June 17, 2026 at 8:34 am

    Just add two lines after exploding

    $key    =   count($linearray);
    $linearray[$key - 1]    =   date('Y-m-d H:i:s',strtotime($linearray[$key - 1]));
    
    $linemysql = implode("','",$linearray);
    

    Also before inserting print_r the result to see if the format has been changed.

    EDITS :

    Looking at the library you are using i assume the last index of each row is your date column(TIME OVER WEB). So i am count the length of array and count – 1 gives me the last index so than i am applying php date function.
    date(‘format’,strtotime(datevalue))
    strtotime converts the string given to numbers like 32431234 and then date function takes tow parameters. Format and number. I am sure that’s enough for you to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a powershell script which imports data from a csv file, and then
I'm running an import script which imports a CSV dump of a database into
I have a script that generates a csv file using the following code: header('Content-type:
I have a small script we're using to read in a CSV file containing
I have a ruby script which will read data from some CSV file and
I have following script which processes emails and save them to csv file. there
I have an R file which imports a file, does some data manipulation, and
I have a script that imports CSV files. What ends up in my database
I'm still fairly new to writing SQL scripts. I have a script which imports
well, I have a script which processes some data, and then it imports it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.