Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Convert different date formats to yyyy-mm-dd
Old 05-10-2009, 10:13 PM Convert different date formats to yyyy-mm-dd
Average Talker

Posts: 17
Name: Gary
Trades: 0
Hello. Newbie asking for help on converting date formats. I have a working uery that will convert a date format from mm/dd/yyyy to yyyy-mm-dd. I need to be able to convert to yyyy-mm-dd the variations that occur when the months and days are single digits and the year is 2 digits (m/dd/yyyy, m/d/yyyy, mm/d/yyyy, m/d/yy, mm/d/yy, m/dd/yy, m/d/yy, etc.). Here is the code I have so far:


PHP Code:
mysql_query("UPDATE compsin SET date = CONCAT(SUBSTRING(date, 7, 4),'-',SUBSTRING(date, 1, 2),'-',SUBSTRING(date, 4, 2))"); 
Thanks in advance.

Last edited by gdaniels; 05-10-2009 at 10:14 PM.. Reason: Typo error
gdaniels is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-10-2009, 10:42 PM Re: Convert different date formats to yyyy-mm-dd
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I would convert the date string to a timestamp and then use date() to format it however I want
PHP Code:
$date 'mm/dd/yyyy';
$timestamp strtotime($date);

$newDate date('n/j/y' $timestamp); // m/d/yy 
http://us.php.net/manual/en/function.date.php
http://us.php.net/manual/en/function.strtotime.php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-10-2009, 10:59 PM Re: Convert different date formats to yyyy-mm-dd
Average Talker

Posts: 17
Name: Gary
Trades: 0
So it doesn't matter what format the date has in $date? It could be m/d/yy, or, mm/d/yyyy, mm/dd/yy, etc.? Then after converting $date to a timestamp, I can use DATE() to put it into the yyyy-mm-dd format? Thanks. I'll give it a try. I am using my query to convert all the dates in the date column of my database to yyyy-mm-dd. How would I include your code into a query? Looks like I might have to loop through the database or could this be done without looping?
gdaniels is offline
Reply With Quote
View Public Profile
 
Old 05-10-2009, 11:25 PM Re: Convert different date formats to yyyy-mm-dd
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by gdaniels View Post
So it doesn't matter what format the date has in $date? It could be m/d/yy, or, mm/d/yyyy, mm/dd/yy, etc.?
I believe the function assumes that the date is formatted as month/day/year with any number of digits. Double check the documentation if you have problems with it.

Quote:
Originally Posted by gdaniels View Post
Then after converting $date to a timestamp, I can use DATE() to put it into the yyyy-mm-dd format? Thanks. I'll give it a try.
Yes. Check the documentation for more information on formatting.

Quote:
Originally Posted by gdaniels View Post
I am using my query to convert all the dates in the date column of my database to yyyy-mm-dd. How would I include your code into a query? Looks like I might have to loop through the database or could this be done without looping?
If you want to do this in SQL only take a look at the date functions:
http://dev.mysql.com/doc/refman/5.1/...functions.html

I don't know how off the top of my head.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-13-2009, 10:38 AM Re: Convert different date formats to yyyy-mm-dd
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
As you can see from these examples, the strtotime function can take an extremely wide range of formats. It's a pretty awesome function

http://us3.php.net/strtotime#functio...otime.examples
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Reply     « Reply to Convert different date formats to yyyy-mm-dd
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.39207 seconds with 12 queries