Jump to content

[SOLVED] Change mysql date format.


defeated

Recommended Posts

See that's where I come unstuck.

It doesn't go......

$result=mysql_query("SELECT * FROM mytable WHERE id=12")or die(mysql_error());
$row=mysql_fetch_array($result);
$mytimedatefield=$row["DATE_FORMAT( mytimedatefield, '%d-%m-%Y )"] ;
echo $mytimedatefield ; 

 

But you get the idea of what I'm trying to do here.

  • 2 weeks later...

I used Pauls solution.  But now I have found myself in a situation where Fenways would work better.  Only problem is that I have 3 date fields to be extracted and formatted per row.  So Fenway, how do you write your statement with mytimedatefield1, mytimedatefield2, mytimedatefield3?  I'm too embarrassed to show you what I tried!!!!

lol

$result=mysql_query("SELECT *, DATE_FORMAT( mytimedatefield1, '%d-%m-%Y ) as pretty1, DATE_FORMAT( mytimedatefield2, '%d-%m-%Y ) as pretty2, DATE_FORMAT( mytimedatefield3, '%d-%m-%Y ) as pretty3 FROM mytable WHERE id=12")or die(mysql_error());
$row=mysql_fetch_assoc($result);
$mytimedatefield1=$row["pretty1"] ;
$mytimedatefield2=$row["pretty2"] ;
$mytimedatefield3=$row["pretty3"] ;
echo "$mytimedatefield1 - $mytimedatefield2 - $mytimedatefield3" ;

Clearly Paul wasn't too embarrassed!  ;D

That's what I tried. Didn't work. My line at present is

$result=mysql_query("SELECT *, DATE_FORMAT(mytimedatefield1,'%d-%m-%Y') AS pretty1 FROM mytable WHERE TO_DAYS(NOW()) - TO_DAYS(mytimedatefield3) = 0") or die(mysql_error());

note the extra ' after %Y. It also didn't seem to like spaces. Still need to get mytimedatefield2 and mytimedatefield3 formatted in the same line though.

 

P.S. Thanks for the help so far.

That's neat.  Nice and short. Going to bang away at the sql solution but can't get the ddamn thing to work.  It's not really very important since it's only for an email to tell our consultants when a job has expired on the site.... but it's anoying me now so I WILL get it going.!!

  • 2 months later...

Fenway, I just got it to work.... I did something really stupid... put in the wrong field name for one of my date fields!

How much after the original post is this?? Still.... all's well that ends well. 

thanks for your help once again.... even if it's a little bit belated this time.

Ian.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.