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.

Link to comment
Share on other sites

No, it goes:

 

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

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

$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" ;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This should be fine:

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")

Link to comment
Share on other sites

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.!!

Link to comment
Share on other sites

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

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.