PHP DB array in Zend framework - date issue

Soldato
Joined
18 Oct 2002
Posts
7,617
Location
Sutton Coldfield, Birmingham
Not sure how to explain this, i'm no expert with PHP apart from the basics and I want to have the easiest solution possible.

I have a mysql table of which there is a number of rows and I want to give each row a date. I have a field called "date" which is setup as a datetime field and when using phpmyadmin to insert a row it works fine, I end up with a date of, for example 2013-01-04 11:23:55. When using the code below I can select the date field and it prints the date fine, in the same format as phpmyadmin shows.

Now I want to format this date in php so that I end up with, lets say, "4-Jan-2013" and I used to use this small bit of code when I was doing things 'my way'

PHP:
$date = date('d-m-Y', $date);

and that worked fine however I can't do things my way, i'm stuck with having to use array's as part of the Zen framework as i'm trying to customise part of a forum called Xenforo. As such I have to use the following array and I can't for the life of me work out how to format the date. Whether it's even possible or whether it's a case of storing the date differently in a int field or such like.

This is the code I currently have

PHP:
    public function getModule()
    {
        $mystuff = $this->_getDb()->fetchAll('SELECT * FROM `matches` ORDER BY `date` DESC LIMIT 10');
 
        foreach ($mystuff as &$stuff)
        {

            $stuff = array(
            'opponent' => $stuff['opponent'],
            'date' => $stuff['date'],
            'event' => $stuff['event'],
            'score' => $stuff['score'],
            'result' => $stuff['result']
            );
 
        }
 
        return $mystuff;
    }

It selects everything I need and prints everything to the screen, how and where do I enter the ability to alter the format of the date field?
 
Last edited:
I'm not sure that you are aware of what is going on in your code. You are re-setting the array's contents with the existing values. You're literally doing "$a = $a;"

The entire foreach loop is completely redundant.

However, if this is really the only place you can change the format, and not closer to where the date is printed to screen, then use:
PHP:
foreach ($mystuff as &$stuff) {
  $stuff['date'] = date("d-m-Y", $stuff['date']);
}
 
tbh I haven't got the foggiest, it's not really my code as like I said normally I can do this without an issue but it's the way I was told to do it as it's the way the forum software works or something? Array's aren't my strong point anyway. What would you alter the foreach loop into to make it 'work better'?
 
Last edited:
I'd delete it entirely :)

The entire function can be contracted to:
PHP:
public function getModule()
    {
        return $this->_getDb()->fetchAll('SELECT * FROM `matches` ORDER BY `date` DESC LIMIT 10');
    }

And at the point where the date is echo'd to the screen, apply the formatting.
 
I don't think I can format the date where it's outputted, im stuck with using their template system in which I have to use {$mystuff.date} or {$mystuff.opponent} in order for it to be displayed on the page. It really is confusing the hell out of me at the moment.

As you can see - http://www.just-survive.co.uk/ - on the left hand side the date prints in the datetime format.

On a totally seperate page i've used the following code to change the format and it works fine

PHP:
   $row[date] = strtotime ($row[date]);
   $row[date] = date ('d M y - H:i', $row[date]);

tried using what you suggested and got an error list as long as my hand :( stupid Xenforo software

To give you guys an idea, the template for displaying the data on the left hand block is as follows

Code:
<xen:if hascontent="true">
<xen:contentcheck>
<xen:foreach loop="$MatchesList" value="$mystuff">

<li><xen:if is="{$mystuff.result} != ''"><span class="{$mystuff.result}"><small>{$mystuff.score}</small></span><xen:else />
<span class="draw"><small>{$mystuff.date}</small></span></xen:if>
<b><xen:if is="{$mystuff.link} != ''"><a href="{$mystuff.link}" target="_blank">{$mystuff.opponent}</a><xen:else />{$mystuff.opponent}</xen:if></b>
<br /><small><span style="color: #999;">{$mystuff.event}</span></small></li>
 
</xen:foreach>
</xen:contentcheck>
<xen:else />
<li>Coming Soon..</li>
</xen:if>
 
Does the template engine support this?
Code:
<xen:if hascontent="true">
<xen:contentcheck>
<xen:foreach loop="$MatchesList" value="$mystuff">

<li><xen:if is="{$mystuff.result} != ''"><span class="{$mystuff.result}"><small>{$mystuff.score}</small></span><xen:else />
<span class="draw"><small>{date("d-m-Y", strtotime($mystuff.date))}</small></span></xen:if>
<b><xen:if is="{$mystuff.link} != ''"><a href="{$mystuff.link}" target="_blank">{$mystuff.opponent}</a><xen:else />{$mystuff.opponent}</xen:if></b>
<br /><small><span style="color: #999;">{$mystuff.event}</span></small></li>
 
</xen:foreach>
</xen:contentcheck>
<xen:else />
<li>Coming Soon..</li>
</xen:if>
Note the addition of strtotime and date.
 
Back
Top Bottom