473,395 Members | 1,639 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,395 software developers and data experts.

Change date to MySql format

code green
1,726 Expert 1GB
I know this is simple but I have been using mssql for so long
I have forgotten how to do it in MySql.

I am reading a date from a CSV file of the format
dd/mm/yyyy
How do I convert this to ISO 8601 standard
yyyy-mm-dd
The value is an array element of which I am looping through removing
spaces and currency symbols and hopefully correcting the date
Expand|Select|Wrap|Line Numbers
  1. foreach($row as &$element)
  2. {
  3.     $element = trim($element);
  4.     if(strpos($element,'£'))
  5.         $element = ltrim(($element),'£');
  6.     if(strpos($element,'date'))
  7.           /*******How do I convert to MySql **************/
  8. }
May 12 '09 #1
2 2285
hoopy
88
There are quite a few ways to do it, explode it into an array and rebuild it I guess or what about something like this:

Expand|Select|Wrap|Line Numbers
  1. $date = "12/05/2009";
  2. $date = preg_replace("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", '$3-$2-$1', $date);
  3. echo $date;
Would output:

Expand|Select|Wrap|Line Numbers
  1. 2009-05-12
So just use that when you insert into the MySQL Date field?
May 12 '09 #2
code green
1,726 Expert 1GB
Thanks for the reply Hoopy.
But woke up this morning and remembered.
This is my code
Expand|Select|Wrap|Line Numbers
  1. foreach($row as $key=>&$element)
  2. {
  3.     if(stripos($key,'date')!==false)
  4.         $element = date('Y-m-d',strtotime($element));
Or 'Y-m-d' could be replaced with 'c' for PHP 5 but this adds an empty time value

This works for an assoc array where any date element has 'date' as part of the key name
May 13 '09 #3

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

Similar topics

0
by: Karam Chand | last post by:
Hello Is there anyway to change the default date format (possibly in a .ini or .cfg file) from YYYY-MM-DD to another format ? (ie, mm/dd/yyyy or any user-defined format). The useage that I'm...
1
by: teqnow | last post by:
I created a table with a timestamp field called "date_added", and it defaults to the NOW function. Any way to change the format to get it to default to the current date instead? I really don't want...
4
by: MyOracle | last post by:
Hi everybody, I just curious about change date(0000-00-00) to date (00-00-0000) in mysql.Can anyone tell me about that. Thanks. izmanhaidi.
2
by: Paul | last post by:
I am trying to insert a date into a mysql table but everytime I attempt it the date field is always empty. The field is of Date type and the date in code is in European format dd/mm/yyyy. ...
1
by: Rotsj | last post by:
Hi, i'm using visual foxpro 9 with a mysql 5 database, for direct access to my database i use navicat. In visual foxpro i've set my date format to dd-mm-yyyy, also i did this in navicat. However...
3
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the...
5
by: ajos | last post by:
hi friends, i imported my database from ms access to mysql, there is a problem though the date in mysql has the format (yyyy-mm-dd) and i want to change it to dd/mm/yyyy.how can i do it?i am using...
20
by: ajos | last post by:
Hi to all, Ok the situation that i have is im adding things in my database(something similar).The database that i was using was access and i had to change it to MySQL.As i came to know the date...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one...
0
by: modolamara | last post by:
Hi people, im importing a date column from a mysql database to a datagridview but it automatically changes the date format from 'YYYY-MM-DD' (Mysql format) to 'MM/DD/YYYY' since the user has...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.