#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    118
    Rep Power
    7

    Better way to format the date?


    I'm using "timestamp" in my MYQSL and it's a nice output but I want to improve it. I want to split up the date/time on certain pages on my website. Here is what I want. I want to output date and time to look like this


    September 14th 2011.
    16:34:36

    ^ Those are two different strings.


    Here's my old site for better examples..just look at all the dates and times.

    http://openzelda.thegaminguniverse.org/news.php


    What's the best way to output it like I want?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    DATE_FORMAT() is the MySQL way of reformatting dates and times. For PHP use strtotime() followed by date().
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    118
    Rep Power
    7
    Not 100% sure how to use Date_format. So far, I'm getting an output of "array"

    Code:
    function GetNews($last = false)
    {
    	$sql = mysql_query("SELECT * FROM News ORDER BY id DESC");
    	if($last) {
    		$rows = mysql_fetch_array($sql);
    		$out['title'][] = $rows['title'];
    		$sqldate = mysql_query(DATE_FORMAT($rows['datetime'],'%M %D %Y'));
    		$row2 = mysql_fetch_array($sqldate);
    		$out['date'][] = ($row2[DATE_FORMAT($rows['datetime'], '%M %D %Y')]);
    		$out['news'][] = $rows['news'];
    	}
    	else {
    		while($rows = mysql_fetch_array($sql)) {
    			$out['title'][] = $rows['title'];
    			$out['date'][] = DATE_FORMAT($rows['datetime'],'%M %D %Y');
    			$out['news'][] = $rows['news'];
    		}
    	}
    	return $out;
    }
    Code:
    $news = GetNews(true);
    	echo "<div id=\"title\"><h2>".$news['title'][0]."</h2></div>";
    	echo $news['date'][0] . "<br>";

    The title returns just fine, but the date returns nothing.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    DATE_FORMAT() is a MySQL function.

    Manual (which has examples)

    [edit] Maybe you need a little more help than just that.
    Code:
    SELECT *, DATE_FORMAT(`datetime`, '%M %D %Y') FROM News ORDER BY `datetime` DESC
    Also,
    1) Don't use an ID to sort things. Especially when you have a more appropriate field available.
    2) If you only want one row then do a LIMIT 1. Yes, that means two separate queries. (Or one string that you add " LIMIT 1" to before executing.)
    Last edited by requinix; March 25th, 2013 at 03:06 AM.

IMN logo majestic logo threadwatch logo seochat tools logo