Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 03-23-2007, 09:37 AM   #1
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
MySQL / PHP gurus help with this code?

I have tried a couple help forums and #php but no luck so far.

I have a varchar field (date) with a date in MM-DD-YYYY format. I need to convert this to my (date2) field which is DATE in the YYYY-MM-DD format. Here is the sql statement I have so far - and it doesn't work:

UPDATE b1datefix t1, b1datefix t2 SET t1.date2 = DATE_FORMAT(t1.date,'%Y-%m-%d') WHERE t1.id=t2.id;

Any ideas?
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 09:41 AM   #2
Masterchief
Confirmed User
 
Join Date: Jun 2006
Posts: 530
i dont work with mysql, but i'd think it would have some sort of globalization stuff built in so that it can natively accept the mm-dd-yyyy date format?
Masterchief is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 09:46 AM   #3
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
Quote:
Originally Posted by Masterchief View Post
i dont work with mysql, but i'd think it would have some sort of globalization stuff built in so that it can natively accept the mm-dd-yyyy date format?
The problem is that half of my DB is one format and half is the other. If sponsors used the same damn date format I wouldn't have this problem. So I am selecting queries based on a certain format and I don't want to run checks at the php level before running queries it just slows the process down.
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 09:58 AM   #4
RichCashMike
Registered User
 
Join Date: Jul 2004
Location: Montreal
Posts: 40
If you were to do it in PHP check out the strtotime() function, get the timestamp, then use date() to get it back into the format you need. Not the cleanest but it would work.
__________________
RichCashMike is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:00 AM   #5
darksoul
Confirmed User
 
darksoul's Avatar
 
Join Date: Apr 2002
Location: /root/
Posts: 4,997
You'll have to do it from the application.
The date_format and other functions expect a date in the default format %Y-%m-%d
__________________
1337 5y54|)m1n: 157717888
BM-2cUBw4B2fgiYAfjkE7JvWaJMiUXD96n9tN
Cambooth
darksoul is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:11 AM   #6
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
Quote:
Originally Posted by darksoul View Post
You'll have to do it from the application.
The date_format and other functions expect a date in the default format %Y-%m-%d
Thanks, that's what I was afraid of.
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:13 AM   #7
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Try something like this (I'm not awake yet so might have a typo in there heh)

UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date,FROM -7 FOR 2));

Enjoy
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:30 AM   #8
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
Keith I think that would work, only problem is I am getting a syntax error and this is too advanced for me to troubleshoot:

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM -7 FOR 2))' at line 1
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:48 AM   #9
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by BradM View Post
Keith I think that would work, only problem is I am getting a syntax error and this is too advanced for me to troubleshoot:

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM -7 FOR 2))' at line 1
Aha I knew I'd have a typo - Damn comma.

Try this:

UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date FROM -7 FOR 2));
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:48 AM   #10
Kaylum
Confirmed User
 
Industry Role:
Join Date: Aug 2005
Location: Costa Rica
Posts: 465
Quote:
Originally Posted by BradM View Post
Keith I think that would work, only problem is I am getting a syntax error and this is too advanced for me to troubleshoot:

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM -7 FOR 2))' at line 1
hey there

he wrote that he wasn't fully awake ;)

there is some garbage in the sql, (FROM and FOR).. the sql should read:

UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date, -7,2));

let us know how it goes ;)

peace
__________________
-- "You can't stop ingenuity, trust me.."
Kaylum is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:51 AM   #11
InfinityWebCreations
Registered User
 
Join Date: Feb 2007
Posts: 21

Quote:
Originally Posted by RichCashMike View Post
If you were to do it in PHP check out the strtotime() function, get the timestamp, then use date() to get it back into the format you need. Not the cleanest but it would work.
Not sure about that "not the cleanest" That method is actually quite common and is a good choice, IMO. to RichCashMike
InfinityWebCreations is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 10:54 AM   #12
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by Kaylum View Post
hey there

he wrote that he wasn't fully awake ;)

there is some garbage in the sql, (FROM and FOR).. the sql should read:

UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date, -7,2));

let us know how it goes ;)

peace
Thanks, though FROM FOR works as well just I had a comma I shouldn't have
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:05 AM   #13
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
I ran the command and it affected all of the rows but it didn't pull in the date. All of the dates came in as: 0000-00-00
So something is wonky and I can't figure it out.
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:12 AM   #14
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by BradM View Post
I ran the command and it affected all of the rows but it didn't pull in the date. All of the dates came in as: 0000-00-00
So something is wonky and I can't figure it out.
SELECT CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date FROM -7 FOR 2)) from b1datefix limit 10;

run that to see what comes out.
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:15 AM   #15
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
Keith,
10 rows of

-02-
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:39 AM   #16
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by BradM View Post
Keith,
10 rows of

-02-
What version of mysql are you using ? I don't see why the first/last substrings wouldn't work unless it's a version thing.

I made a test case for oyu actually and on my server it works perfectly:

mysql> desc datefix;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| date1 | varchar(10) | YES | | NULL | |
| date2 | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from datefix;
+------------+-------+
| date1 | date2 |
+------------+-------+
| 03-23-2007 | NULL |
+------------+-------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT(SUBSTRING(date1,-4),'-',SUBSTRING(date1,1,2),'-',SUBSTRING(date1 FROM -7 FOR 2)) from datefix;
+-----------------------------------------------------------------------------------------+
| CONCAT(SUBSTRING(date1,-4),'-',SUBSTRING(date1,1,2),'-',SUBSTRING(date1 FROM -7 FOR 2)) |
+-----------------------------------------------------------------------------------------+
| 2007-03-23 |
+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:40 AM   #17
EDepth
Confirmed User
 
Join Date: Nov 2005
Location: Seattle, WA
Posts: 510
Or avoid mysql's functions, and just get the data then update the data
PHP Code:
$explode explode("-",$myrow['funkydatefield']);
$newDate $explode[2]."-".$explode[0]."-".$explode[1]; 
__________________
ICQ: 275335837
EDepth is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:46 AM   #18
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by EDepth View Post
Or avoid mysql's functions, and just get the data then update the data
PHP Code:
$explode explode("-",$myrow['funkydatefield']);
$newDate $explode[2]."-".$explode[0]."-".$explode[1]; 
Or just work with unix timestamps in the DB for all date/time fields - that'smy preference.

However I think for Brad's purpose here doing it in mysql would be preferred instead of in the code...he could even use mysql's schedule feature to have it run every 5 mins or whatever he wishes, so no need to alter any code anywhere or even setup any cronjobs.
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:48 AM   #19
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
MySQL 4.0.26-standard

That's probably why there is a problem. :/
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:52 AM   #20
EDepth
Confirmed User
 
Join Date: Nov 2005
Location: Seattle, WA
Posts: 510
Yah both ways work, would really depend on how often it needs to be done, if the source of the input could be switched/fixed.
__________________
ICQ: 275335837
EDepth is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:54 AM   #21
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by BradM View Post
MySQL 4.0.26-standard

That's probably why there is a problem. :/
I don't have any version 4.x around anymore so can't help you much there...I would say move to 5.1 the are a LOT of new features to like plus upgrading is like a 5-10 minute job if you know how to do it.

Otherwise, yeah you are probably stuck doing it on the php end.

Last try that might work for you since the middle one did work form your output:

CONCAT(SUBSTRING(date,7,4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date,4,2))
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 11:56 AM   #22
BradM
Confirmed User
 
Join Date: Dec 2003
Location: 1123,6536,5231
Posts: 3,397
Quote:
Originally Posted by Varius View Post
CONCAT(SUBSTRING(date,7,4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date,4,2))
OMFG you beautiful man that worked. Holy shitolskies!!!

Thanks guys I appreciate the help you just taught me a lot of new info. HUGSSSZZ!!
BradM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 12:01 PM   #23
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by BradM View Post
OMFG you beautiful man that worked. Holy shitolskies!!!

Thanks guys I appreciate the help you just taught me a lot of new info. HUGSSSZZ!!
Not sure why I didn't just use this option first instead of the fancier way

Guess this means I'm finally awake now and should head in to the office...later
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-23-2007, 12:05 PM   #24
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Quote:
Originally Posted by Varius View Post
Not sure why I didn't just use this option first instead of the fancier way

Guess this means I'm finally awake now and should head in to the office...later
Or you should take the day off and relax because your brain's "thinking" in the "non-fancy" way.

I would if I had an epiphany like that.

BTW - fun thread to watch... thanks geeks.
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.