Jump to content

[SOLVED] Need to reformat standard numerical date (Y-m-d) to words


vynsane

Recommended Posts

I'm 95% done/happy with a news article index page script I've built, but I want to change the way the dates are formatted:

 

http://www.wallcrawlersweb.com/news/

 

right now, it's formatted in the "standard" (Y-m-d) configuration in the DB, but I want to output something like "February 13, 2007" instead of "2007-02-13". How can I reformat the data that's already there?

Link to comment
Share on other sites

k, I tried working with that, but I just got every single news item displayed as being posted in December 1969... Obviously not correct, so I need a little more guidance...

 

How would I use the above snippet here?

 

   while ($row = mysql_fetch_array($newsobject)) {
      echo "<h3>".$row['date']."</h3>";

Link to comment
Share on other sites

*sigh*

 

yeah... been to the PHP Manual site many times. Thing of it is, I'm at a PHP help forum because I didn't understand that bit and figured I could get PHP help here. Not links and cryptic code with no explanation what-so-ever. What I've gotten here so far is the interweb equivalent to asking someone in the real world a question and having them not even acknowledge me more than with a point in the broad vicinity of what I asked about.

 

Basically, if you want to help, help. If not, don't bother posting.

Link to comment
Share on other sites

<?php

//time stamp made

$date=time();

//time stamp converted to readable use.

$result=date("d-m-y",$date);

echo $result;

//now the other way ok.

//the date of today.

$date=date("y-m-d");

//get the current date in a timesatamp.
$result=strtotime($date);

echo $result;

//now convert to readable use.

$result=date("d-m-y",$result);

?>

 

now learn mktime and coversions ok.

 

 

Link to comment
Share on other sites

If you read it and think about it for a while, you might notice that date() takes a TIMESTAMP as an argument, and strtotime takes a STRING and returns a TIMESTAMP. You have a STRING you need to format into ANOTHER STRING.

Let's try some logic. You have two functions offered to you which deal with string and timestamp conversion.

 

Don't be a smartass, leave that up to the people giving the help :-P

Link to comment
Share on other sites

If you read it and think about it for a while, you might notice that date() takes a TIMESTAMP as an argument, and strtotime takes a STRING and returns a TIMESTAMP. You have a STRING you need to format into ANOTHER STRING.

Let's try some logic. You have two functions offered to you which deal with string and timestamp conversion.

 

Don't be a smartass, leave that up to the people giving the help :-P

 

Well, it was late last night and I didn't have the time to think about it for a while. I know you guys are trying to teach a dude to fish instead of giving him a fish, but I must not be using the correct bait or something - I'm just not getting it. I understand the gist of what you're saying, but when I try to implement, all my dates end up as December 31, 1969.

 

I'm going to go out on a limb here and guess you are pulling those dates from a mysql database.

 

Correct, hence:

 

...right now, it's formatted in the "standard" (Y-m-d) configuration in the DB...

 

If so, just format it while you are doing the query instead of doing it from php.

 

Try

SELECT DATE_FORMAT(myDateField, '%M %D, %Y')

 

This is SOOO close, and outputs the correct date... only since I'm comparing distinct dates to the articles posted on that date, when I change the format BEFORE I compare the dates, it confuses my second query and I only get dates - no articles:

 

http://www.wallcrawlersweb.com/news/indexdate.php

 

Here's my code for the original news index page:

 

if ($category !=null)
  $newsobject = mysql_query("SELECT DISTINCT date FROM NewsItem WHERE category = '".$category."' ORDER BY id DESC LIMIT $offset, $rowsPerPage");
else
  $newsobject = mysql_query("SELECT DISTINCT date FROM NewsItem ORDER BY id DESC LIMIT $offset, $rowsPerPage");
    while ($row = mysql_fetch_array($newsobject)) {
      echo "\t\t\t<div class=\"newsday\">\n";
      echo "\t\t\t\t<h3>".$row['date']."</h3>\n";
    if ($category !=null)
      $newsitems = mysql_query("SELECT id, category, title, preview FROM NewsItem WHERE date = '".$row['date']."' AND category = '".$category."'");
    else
      $newsitems = mysql_query("SELECT id, category, title, preview FROM NewsItem WHERE date = '".$row['date']."'");
         while ($articles = mysql_fetch_array($newsitems)){
               echo ""STUFF"";
            }

 

if I do it your way:

 

if ($category !=null)
$newsobject = mysql_query("SELECT DISTINCT DATE_FORMAT(Date, '%M %D, %Y') as date FROM NewsItem WHERE category = '".$category."' ORDER BY id DESC LIMIT $offset, $rowsPerPage");
else
$newsobject = mysql_query("SELECT DISTINCT DATE_FORMAT(Date, '%M %D, %Y') as date FROM NewsItem ORDER BY id DESC LIMIT $offset, $rowsPerPage");
   while ($row = mysql_fetch_array($newsobject)) {
      echo "\t\t\t<div class=\"newsday\">\n";
      echo "\t\t\t\t<h3>".$row['date']."</h3>\n";
    if ($category !=null)
      $newsitems = mysql_query("SELECT id, category, title, preview FROM NewsItem WHERE date = '".$row['date']."' AND category = '".$category."'");
    else
      $newsitems = mysql_query("SELECT id, category, title, preview FROM NewsItem WHERE date = '".$row['date']."'");
         while ($articles = mysql_fetch_array($newsitems)){
            echo ""STUFF"";
           }

 

it can't compare .$row['date']. in the second query to the reformatted date in the first one...   

 

Anyway, guess I'll just have to keep fishing. Hopefully I don't starve to death before I catch one.

Link to comment
Share on other sites

right now, it's formatted in the "standard" (Y-m-d) configuration in the DB, but I want to output something like "February 13, 2007" instead of "2007-02-13". How can I reformat the data that's already there?

 

This is SSSSSOOOOO easy! (Sorry if I seem to be gloating...)

 

Code --> http://code-bin.homedns.org/10

 

PS You might also see http://code-bin.homedns.org/8 to calculate the difference in days between 2 YMD values.

Link to comment
Share on other sites

okay, so i may be a little slow with this whole strtotime thing, but i have to do another conversion that i'm having a problem with. is it possible to just use a month integer and get the month name using strtotime? on this page:

 

http://www.wallcrawlersweb.com/database/comicsbydate.php?month=02&year=2007

 

i'm getting all titles between the first and last of the month in the URL parameter... i need to get that "month=02" and turn it into "February". obviously i'm doing it wrong, because if you change the month to "03":

 

http://www.wallcrawlersweb.com/database/comicsbydate.php?month=03&year=2007

 

i get the correct date in the table, but the text version of the month remains "February" - so obviously it's only getting the current month. i've tried it many different ways, but i can't get it to spit out the correct month as prescribed from the URL parameter. this is what i've got right now:

 

    $month = $_GET['month'];
    $year = $_GET['year'];
    $fullMonth = date("F", mktime($month));

 

but i've tried it as this, too:

 

    $month = $_GET['month'];
    $year = $_GET['year'];
    $fullMonth = date("F", strtotime($_GET['month']));

 

or

 

    $month = $_GET['month'];
    $year = $_GET['year'];
    $fullMonth = date("F", strtotime($month));

 

and still get the current month instead of the month in the URL parameter when i try to echo "$fullMonth" in either the title tag or the red header bar above the table.

Link to comment
Share on other sites

nevermind, i finally figured it out... it's a bit complicated, and there's probably a better way of doing it, but this is what i came up with:

 

$month = $_GET['month'];
$year = $_GET['year'];

$montharray = mysql_query("SELECT releaseDate FROM ComicIssues WHERE releaseDate BETWEEN '".$year."-".$month."-01' AND '".$year."-".$month."-31' LIMIT 1");
       while ($dates = mysql_fetch_array($montharray)){
         $fulldate = $dates['releaseDate'];
         $fullMonth = date("F", strtotime($fulldate));

echo ""$fullMonth"";

Link to comment
Share on other sites

Here's a rather simple solution.

 

$month_num = (int) $_GET['month'];

$months["1"] = "January";
$months["2"] = "February";
$months["3"] = "March";
$months["4"] = "April";
$months["5"] = "May";
$months["6"] = "June";
$months["7"] = "July";
$months["8"] = "August";
$months["9"] = "September";
$months["10"] = "October";
$months["11"] = "November";
$months["12"] = "December";

echo "$months[$month_num]";

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.