Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set date format with existing records php mySQL. How?

I have a mySQL with a table. There are 30 records all with a date column.

How do I change all my existing records in my table to have today's date with is format?

date_default_timezone_set('America/Los_Angeles');
$date = date("m/d/y g:i A") ; 
like image 677
Erik Avatar asked Sep 25 '11 21:09

Erik


People also ask

How do I insert date in YYYY-MM-DD format in MySQL?

You can use str_to_date to convert a date string to MySQL's internal date format for inserting.

How can I change the date format of a column in MySQL?

Change the curdate() (current date) format in MySQL The current date format is 'YYYY-mm-dd'. To change current date format, you can use date_format().

How do I change the date format in a database?

We change the date format from one format to another. For example - we have stored date in MM-DD-YYYY format in a variable, and we want to change it to DD-MM-YYYY format. We can achieve this conversion by using strtotime() and date() function.


1 Answers

Here's the fix for the VARCHAR to DATETIME (this will erease the current value):

ALTER TABLE mytable modify column `mycolumn` datetime NOT NULL DEFAULT 0;
UPDATE mytable SET mycolumn = NOW() WHERE ...;

or

UPDATE mytable SET mycolumn = '2011-09-25 17:40:00' WHERE ...;

If you want to save the current value use:

ALTER TABLE mytable add column `newdate` datetime NOT NULL DEFAULT 0;
UPDATE mytable SET newdate = mycolumn;
ALTER TABLE mytable DROP COLUMN mycolumn;

If you want to select the date in the format you can:

SELECT DATE_FORMAT(mycolumn, '%m/%e/%y %h:%i %p') FROM mytable WHERE ...

Or in your PHP you can use:

date_default_timezone_set('America/Los_Angeles');

// query select ($row = mysql_fetch_assoc($query)...

$date = $date = date("m/d/y g:i A", strtotime($row['mycolumn']));
like image 120
Book Of Zeus Avatar answered Sep 23 '22 05:09

Book Of Zeus