473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert US date to MySql date

code green
1,726 Expert 1GB
I am transfering data from a CSV file to a MySql database.
Unfortunately the dates are in 2008.27.08 US string format instead instead of 2008-08-27 database format.
The CSV data is first read into an associative array where the column names become the key names.
So I need to search the keys to find 'date' as part of the key name then convert the element to 'Y-m-d' format.
Is there a way of doing this other than a rather clumsy
[PHP]foreach($record as $key=>&$element)
{
if(strpos($key,'date'))
$element = date('Y-m-d',strtotime($element));
}[/PHP]
Aug 27 '08 #1
8 3164
code green
1,726 Expert 1GB
I have dates in the format 2008.08.12. They are from an oscommerce feed.
How do I convert them to a MySql format.
The following produces 2008-12-08T00:00:00+00:00 [PHP]$date = date('c',strtotime($date)); [/PHP] The month should be August not December.
Aug 28 '08 #2
Dormilich
8,658 Expert Mod 8TB
It looks that replacing the "." by "-" should do the trick. I guess the dots make the date() function believe it's an american style date (with day next to year).

Maybe the comments section of the php date() function reference offer some insight (like this one php date comment)
Aug 28 '08 #3
code green
1,726 Expert 1GB
Thanks Dormilich.
Replacing the dots with dashes will produce 2008-12-08 which mysql will treat as 8th of December.
The date is 12th of August.
I don't think the format 2008.12.08 as 8th August is recognised anywhere in the world!
Maybe if the server had US settings it could be recognised correctly but my server has UK settings.
Think I will try to persuade the data supplier to mend his ways
Aug 28 '08 #4
Dormilich
8,658 Expert Mod 8TB
Think I will try to persuade the data supplier to mend his ways
if that fails you can invert the date manually (like explode - invert - implode or preg_replace/preg_match). I remember seeing one comment (php.net) using something like that.
Aug 28 '08 #5
dlite922
1,584 Expert 1GB
FYI:

here's a basic function:

(I didn't write it, copy pasted from an app. )

[PHP]

function convertUSADateToMysql($date)
{
$mysqlDate = "";

if ($date != "")
{
$date = str_replace("/", "-", $date);

list($month, $day, $year) = explode ("-", $date);

if (strlen($day) < 2)
{
$day = "0" . $day;
}

if (strlen($month) < 2)
{
$month = "0" . $month;
}

if (strlen($year) < 3)
{
$year = "20" . $year;
}

$mysqlDate = $year . '-' . $month . '-' . $day;
}
return $mysqlDate;
}

[/PHP]



you're welcome,




Dan
Aug 28 '08 #6
Atli
5,058 Expert 4TB
Couldn't you just use a regex on the CVS file before it is read into your array?
Aug 28 '08 #7
If you have to put data from CSV to MySql tables then you can also do in following way.

it is my test csv file.(test.csv)
Expand|Select|Wrap|Line Numbers
  1. "0375","GF","Brentwood","2008.27.08"
  2. "0376","GF","O St. Market","2008.27.08"
  3. "0378","Gs","Tivoli Square","2008.27.08"
  4. "0383","ods","Van Ness","2008.27.08"
  5. "0379","Foods","Wisconsin Ave - GROCERY","2008.27.08"
  6.  
And this is the mysql query
Expand|Select|Wrap|Line Numbers
  1. LOAD DATA INFILE "C:/Documents and Settings/Dheeraj/Desktop/test.csv" 
  2. INTO TABLE from_csv 
  3. FIELDS TERMINATED BY "," 
  4. OPTIONALLY ENCLOSED BY '"' 
  5. LINES TERMINATED BY '\r\n'
  6. (id, name,city,@varDate)
  7. SET curDate=CONCAT(SUBSTRING_INDEX(@varDate,'.',1),'-',SUBSTRING_INDEX(SUBSTRING_INDEX(@varDate,'.',-1),'.',1),'-',SUBSTRING_INDEX(SUBSTRING_INDEX(@varDate,'.',-2),'.',1));
  8.  
And this is the structure of my table
Expand|Select|Wrap|Line Numbers
  1. id            varchar(50) 
  2. name      varchar(50)
  3. city         varchar(50)  
  4. curDate   date 
  5.  

Hope this will help you.

Best of luck.
Aug 29 '08 #8
code green
1,726 Expert 1GB
Wow! Thanks for all the suggestions guys.
I will look at the merits of each one.
The CSV file is opened and read by a class so that needs to remain generic.
And the data is input via a MySql class so I don't really want to add extra load in there.
I need something to manipulate the data in between, but I am hoping to develop this into a generic class eventually so ideally,
I suppose I need a function to handle dates and convert them all to ISO 8601 format (ANSI SQL).from all standards.
Hopefully your ideas will help me write this.

This forum is excellent for bouncing ideas of people helping one to see the true problem more clearly
Aug 29 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
0
by: Andre Winarko | last post by:
How do you convert a date dd/mm/yyyy from a .txt file into mysql date format when using LOAD DATA INFILE ? __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free,...
0
by: Randall Parker | last post by:
I realize that methods like DateTime.ToLocalTime() and DateTime.ToUniveralTime() exist. But suppose one wants to query out data from a database that has UTC times in it. Suppose one wants to have...
4
by: sang | last post by:
How can I convert a string like '11-oct-2006' into a valid mysql date? the date_format doesnot change the string in requried date format. create table sample(name varchar(20),date varchar(16));...
3
by: ssmeshack | last post by:
Hi all, Im have a problem here. Im using VWD 2008 (c#) and Mysql 2005. I dont know how to convert datetime data that i retrieve from mysql database to only date or time. Can anyone help me? Here...
3
by: Bobby Edward | last post by:
In mysql the db stores date/time as this: yyyy-mm-dd Is there an easy way to convert it to mm/dd/yyyy? How about if it shows up in a datagrid column? How about if the time is appended. For...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
1
by: smdmca | last post by:
I have Julian date, I want to convert it into date. Is there any function in MySql to convert Julian date to date eg- Julian Date- 2455116 Date - Oct - 12, 2009
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.