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

Coverting php text string to mysql data format ?

I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?
Jun 27 '08 #1
8 3043
On 26 Jun, 15:23, "Tony B" <tag...@yahoo.co.ukwrote:
I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?
Do it in MySQL instead. Look at the DATE_FORMAT function in the MySQL
manual:

http://dev.mysql.com/doc/refman/5.0/...on_date-format

Furher questions of this sort should be in comp.databases.mysql
Jun 27 '08 #2
Tony B schreef:
I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?

Hi,

I always do this 'by hand', eg:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

If you think the above method sucks, you can also do this:

You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:
http://nl.php.net/manual/en/function.strtotime.php

date:
http://nl.php.net/manual/en/function.date.php

Regards,
Erwin Moller

Jun 27 '08 #3
On 26 Jun, 16:04, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Tony B schreef:
I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?

Hi,

I always do this 'by hand', eg:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

If you think the above method sucks, you can also do this:

You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:http://nl.php.net/manual/en/function.strtotime.php

date:http://nl.php.net/manual/en/function.date.php

Regards,
Erwin Moller
Erwin that all seems an awful lot of work when MySQL supplies the
STR_TO_DATE function specifically to do this job!
http://dev.mysql.com/doc/refman/5.0/...on_str-to-date
Jun 27 '08 #4
Captain Paralytic schreef:
On 26 Jun, 16:04, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>Tony B schreef:
>>I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?
Hi,

I always do this 'by hand', eg:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

If you think the above method sucks, you can also do this:

You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:http://nl.php.net/manual/en/function.strtotime.php

date:http://nl.php.net/manual/en/function.date.php

Regards,
Erwin Moller

Erwin that all seems an awful lot of work when MySQL supplies the
STR_TO_DATE function specifically to do this job!
http://dev.mysql.com/doc/refman/5.0/...on_str-to-date
Yes I know. Or more honest: I expected MySQL had it (I don't use MySQL,
only Postgresql, which of course has it all.)
I can only say I don't trust datehandling.
I live in Europe/Netherland, and over here 05/06/2008 means 5 june 2008
for example.
My server however is configured USA style.
Next country has different notation.

So when I throw the string "2008/06/05" at you, what does it mean?
And what does it mean in Bulgary? Or India?
I always found working with dates very confusing.

When I work on dates I am always double/triple check, especially when
users provide the strings, or they come for some external source.

I once extended an existing employee-time-declare system so their bosses
could accept or reject declared hours for Philips. The users where
traveling all over the world, through timezones, etc. It was a total
confusing disaster, especially because the employees entered their
dates/times themself.
I guess I picked up the habbit of handling dates myself in that time.

Anyway, all excuses. You are right of course.
I don't have to impose my date-paranoia on others. ;-)

Regards,
Erwin Moller

PS: I know my explode-like solution doesn't solve the 2008/06/05
month/day problem.
Nowadays, when I get dates in from clients, I simply avoid using e TEXT
field, but use dropdowns with months/days/years, so they cannot do it
wrong. Oh well, it is HARDER for them to do it wrong. ;-)
Jun 27 '08 #5
"Erwin Moller"
<Si******************************************@spam yourself.comwrote in
message news:48***********************@news.xs4all.nl...
Tony B schreef:
>I have a string in a existing php script which is in the form
"dd/mm/yyyy" and I need to convert it into a suitable format for mysql
which is "yyyy-mm-dd" Is there a neat way of doing this in php ?


Hi,

I always do this 'by hand', eg:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

If you think the above method sucks, you can also do this:

You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:
http://nl.php.net/manual/en/function.strtotime.php

date:
http://nl.php.net/manual/en/function.date.php

Regards,
Erwin Moller
Hi,
Thanks for the info. I also found split function which can replace explode
in your code fragment, though slower as use regex.
Tony

Jun 27 '08 #6

"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:ee**********************************@y21g2000 hsf.googlegroups.com...
On 26 Jun, 16:04, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>Tony B schreef:
I have a string in a existing php script which is in the form
"dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?

Hi,

I always do this 'by hand', eg:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

If you think the above method sucks, you can also do this:

You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:http://nl.php.net/manual/en/function.strtotime.php

date:http://nl.php.net/manual/en/function.date.php

Regards,
Erwin Moller

Erwin that all seems an awful lot of work when MySQL supplies the
STR_TO_DATE function specifically to do this job!
http://dev.mysql.com/doc/refman/5.0/...on_str-to-date
I also tried this solution, and again it works well. I guess this solution
is clearer and can be extended easily if needed to other formats.
Thanks
Jun 27 '08 #7
On Jun 26, 4:04 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Tony B schreef:
I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?
<snip>
You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:

parse a date to UTS:http://nl.php.net/manual/en/function.strtotime.php
Every implementation of strtotime I've used insists on parsing dates
as US format (mm/dd/yy[yy]) rather than dd/mm/yy[yy], regardless of
any locale / TZ settings, to the point where I now avoid using the
function.

Are you saying it now works with dd/mm/yy?

C.
Jun 27 '08 #8
On 27 Jun, 13:29, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
On Jun 26, 4:04 pm, Erwin Moller

<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Tony B schreef:
I have a string in a existing php script which is in the form "dd/mm/yyyy"
and I need to convert it into a suitable format for mysql which is
"yyyy-mm-dd" Is there a neat way of doing this in php ?
<snip>
You can also make a Unix Time Stamp of the original date, and use date()
to parse it to a format you need:
parse a date to UTS:http://nl.php.net/manual/en/function.strtotime.php

Every implementation of strtotime I've used insists on parsing dates
as US format (mm/dd/yy[yy]) rather than dd/mm/yy[yy], regardless of
any locale / TZ settings, to the point where I now avoid using the
function.

Are you saying it now works with dd/mm/yy?
No he didn't say that. That is why he had the lines:
$orgDate = "22/06/2008";
$partsArr = explode("/",$orgDate);
$newDate = $partsArr[2]."-".$partsArr[1]."-".$partsArr[0];

in his post!

But as I have already said, the MySQL function STR_TO_DATE() is the
correct one to use for this scenario.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: atse | last post by:
Hi, I am still stick on the text file being imported to database. Can anybody help? I have just done the csv format files. And then I work on text and DAT formats, but I have problem to import...
0
by: adrian GREEMAN | last post by:
When I try to import a text file with new data for an existing table I get the error "1148 - the used command is not allowed with this MySQL version." I have tried with both PHPMyAdmin2.3 and...
4
by: news | last post by:
Our production database in an exported textfil runs about 60 MB. Compressed that's about 9 MB. I'm trying to import the export into another machine running FC3 and mySQL 11.18, and it appears as...
6
by: Kevin Chambers | last post by:
Hi all-- In an attempt to commit an Access MDB to a versioning system (subversion), I'm trying to figure out how to convert a jet table's metadata to text, a la SaveAsText. The end goal is to...
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
3
by: adtvff | last post by:
Hi, Given a large ascii file (delimited or fixed width) with one ID field and dimensions/measures fields, sorted by dimensions, I'd like to "flatten" or "rollup" the file by creating new...
12
by: mantrid | last post by:
Hello Can anyone point me in the right direction for the way to read a text file a line at a time and separate the fields on that line and use them as data in an INSERT to add a record to a mysql...
0
by: santoshsri | last post by:
Hello, I have written a Console application in C#, it basically imports data from tab delimited text files and save in SQl Server database. I am converting currency format of Mortgage value into...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
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...

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.