Jump to content

Update a column


rondog

Recommended Posts

Hey guys..my mind is like mush right now and cant think of how to do this..

 

I have a table and a column with a date that looks like: 4/25/10

 

basically I need to take that date and do a strtotime() on it and update it..so rather than 4/25/10 I need it to be the strtotime version. Any help? thanks!

 

 

edit::the entire column by the way, not just 1 row.

Link to comment
Share on other sites

ok well my next question then is is mysql able to determine something like

 

$today = "4/21/2010";

 

SELECT * FROM table WHERE date_column >= '$today'  ?

 

 

I always just convert it using strtotime so its a number and then I format it with date() when I display it.

Link to comment
Share on other sites

The reason why the mysql (and other database) dates are in the YYYY-MM-DD format is because you can do greater-than/less-than comparisons and sorting on dates in that format because the fields making up the date are left-to-right, most significant digit (year) to least significant digit (day.)

Link to comment
Share on other sites

1) Alter your table and add a DATE data type column.

2) Execute an UPDATE query to populate the new column from your existing column using the STR_TO_DATE() function.

3) After you are sure the new column has the correct data and you have changed any existing queries to work with the new column, delete the old column. You can use the mysql DATE_FORMAT() function to retrieve a DATE data type in any format you need.

 

Ref: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

Link to comment
Share on other sites

I am trying this, but it doesnt seem to be working. The newdate column still says 0000-00-00

$q = mysql_query("SELECT id,date FROM de_events");
while ($result = mysql_fetch_array($q))
{
$query = mysql_query("UPDATE de_events SET newdate = STR_TO_DATE('".$result['date']."','%Y-%m-%d') WHERE id = '".$result['id']."'");
}

Link to comment
Share on other sites

echo your query to make it is contains proper values.

 

They look ok to me..?

UPDATE de_events SET newdate = STR_TO_DATE('4/17/2010','%Y-%m-%d') WHERE id = '1'
UPDATE de_events SET newdate = STR_TO_DATE('4/17/2010','%Y-%m-%d') WHERE id = '2'
UPDATE de_events SET newdate = STR_TO_DATE('4/21/2010','%Y-%m-%d') WHERE id = '3'
UPDATE de_events SET newdate = STR_TO_DATE('4/22/2010','%Y-%m-%d') WHERE id = '4'
......

Link to comment
Share on other sites

The format specifier '%Y-%m-%d' does not match the starting format of 4/17/2010.

 

Also, if you are updating all the rows, you don't need a WHERE clause in the query and you don't need to select anything or loop over anything. Just execute the UPDATE query directly against the database.

Link to comment
Share on other sites

The format specifier '%Y-%m-%d' does not match the starting format of 4/17/2010.

 

Also, if you are updating all the rows, you don't need a WHERE clause in the query and you don't need to select anything or loop over anything. Just execute the UPDATE query directly against the database.

 

Ahh that worked! I was thinking the  '%Y-%m-%d' was the format I wanted it input as. Thanks so much!

Link to comment
Share on other sites

hey one more question. Now when I SELECT the new date column, how can I get it back in the format of dd/mm/yyyy?

 

I tried:

$today = date("Y-m-d");

"SELECT * FROM de_events WHERE date >= DATE_FORMAT('$today','%d/%m/%Y')";

 

and it isnt working

 

Link to comment
Share on other sites

I kinda figured it out, but not sure if im doing it the right way

 

$sql = "SELECT *,DATE_FORMAT(date,'%m/%d/%Y') FROM de_events WHERE date >= '$today'";

 

The thing is, its returning the column name as DATE_FORMAT(date,'%m/%d/%Y'), although I get both versions of the date, the yyyy-mm-dd version and the m/d/Y version.

Link to comment
Share on other sites

ok I did:

$sql = "SELECT *,DATE_FORMAT(date,'%m/%d/%Y') AS date FROM de_events WHERE date >= '$today'";

 

and its returning the formatted date as the date column...this'll work for now, but if anyone has a better way or the correct way to do it, please let me know, thanks.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.