Forum Moderators: coopster
To further complicate this, I also would like to add 6 months to the outputted date! Is this easy or impossible?
Thanks!
You could simple modify the the DATE_FORMAT [dev.mysql.com].
SELECT DATE_FORMAT('2004-06-30', '%d %b %Y'); And to add 6 months, use the DATE_ADD function:
SELECT DATE_FORMAT(DATE_ADD('2004-06-30', INTERVAL 6 MONTH), '%d %b %Y');
First, I should mention that, you can alter the format of a date field when selecting it from the db.
SELECT DATE_FORMAT [dev.mysql.com](date_field_name ,'%e %b % %Y') as mydate FROM mytable WHERE...
That way the date is already formatted when you get it. Now, to add six months, this may work:
SELECT ADDTIME(DATE_FORMAT [dev.mysql.com](date_field_name ,'%e %b % %Y'), '0 6:0:0.000000') as mydate FROM mytable WHERE...
Anyhow, see the link above for way too much mysql date info ;)
As for a PHP solution, try this:
$date_array = explode("-", $date);
$formatted_date = date [php.net]("j M Y", mktime [php.net](0, 0, 0, $date_array[1], $date_array[2], $date_array[0]));
Too quick for me coopster!
SELECT DATE_FORMAT(DATE_ADD(lastchk, INTERVAL 6 MONTH), '%d %b %Y');
If you want to format it after running the query, have a look at the technique Birdman has shown.
I use the following query:
$result = mysql_query( "SELECT * FROM engineering ORDER BY reg" );
while ( $a_row = mysql_fetch_array( $result ) )
{
<?=$a_row[reg]?> ... and so on thru my table.
}
The lastchk field and one other I want to format dd-Mmm-yy and add 6 months and 12 months to respectively. Is the easiest way to do this by altering the way I query the table or to write some php where I want to display the result...?