We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Format date on custom field from custom module’s DB query

Development and Programming

welded's avatar
welded
24 posts
16 years ago
welded's avatar welded

I have a module which queries the database and collects various info and returns the results as EE tags. This is working great, except I can’t add the ‘format’ attribute to datetime fields. Case in point:

{event_date} displays 1242252649

{event_date format='%d'} displays {event_date format='%d'} but I expect it should display 14

I would just format the date in the module itself, but this is too restrictive when I need to display the date differently in other contexts. The last problem I posted was swiftly answered pointing out my own boneheaded oversight, hopefully this is the same sort of problem. 😉

Thanks for any help!

=======

Seriously? No help for a paying customer?

       
Robert Wallis's avatar
Robert Wallis
36 posts
16 years ago
Robert Wallis's avatar Robert Wallis

You have to replace the {event_date} yourself from the tag data. You are probably using $TMPL->swap_var_single() and passing it “event_date” which means “{event_date}” which is different than “{event_date format=""}”. So what you need to do, is preg_match() and preg_replace() the $TMPL->tagdata manually and make something like this (untested pseudo code)

if (preg_match("/{event_date\\s+format=\"([^\"]*)\"}/", $tagdata, $matches))
{
    $date = date($matches[1]);
    $tagdata = preg_replace("/{event_date \\sformat=\"([^\"]*)\"}/", $date, $tagdata);
}
else
{
    $tagdata = $TMPL->swap_var_single('event_date', date(), $tagdata);
}
       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

Robert is right on, as always.

Although it’s not quite clear from your post if you’re having trouble grabbing the formatting or doing the parsing.

What we do in our own modules, is to grab the various date fields before looping through the data, so that regular expression only runs once, instead of running for every entry:

if (preg_match_all("/".LD."(event_date)\s+format=(\042|\047)([^\\2]*?)\\2".RD."/s", $TMPL->tagdata, $matches))
{
    for ($i = 0; $i < count($matches['0']); $i++)
    {
        $matches['0'][$i] = str_replace(array(LD,RD), '', $matches['0'][$i]);
        $event_date[$matches['0'][$i]] = $LOC->fetch_date_params($matches['3'][$i]);
    }
}

Now that you have that event_date array with keys that look exactly like your tags, you can do a simple str_replace inside your main loop:

foreach($some_data as $count => $data)
{
    foreach($TMPL->var_single as $var_key => $var_val)
    {
        if (isset($event_date[$var_key]))
        {
            foreach ($event_date[$var_key] as $dvar)
            {
                $var_val = str_replace($dvar, $LOC->convert_timestamp($dvar, $data['event_date'], TRUE), $var_val);
            }

            $tagdata = $TMPL->swap_var_single($var_key, $var_val, $tagdata);
        }
    }
}
       
welded's avatar
welded
24 posts
16 years ago
welded's avatar welded

Thanks for the replies, although to be honest I am not following you at all. 😉 What I’ve done is to forget about relying exclusively on template tags and just enabled PHP to echo the date out with date(). It may not be the ‘proper’ way, but at least I have been able to get results.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

The problem is essentially what Robert outlined above. EE can easily replace {variable} with a value, but it has no way of knowing what the format parameter will look like, so it can’t guess at replacing it. If you’re not familiar with regular expressions this is going to be difficult to implement. However, if you can give us a reduced (!) version of the code you’re struggling with I’m sure we can help you make it work.

-Pascal

       
welded's avatar
welded
24 posts
16 years ago
welded's avatar welded

I’m going to have to do more research on the EE classes, but unfortunately it’ll have to wait. This is my first project and it’s been a lot to take in overall and at this particular moment I just need have a working alpha and I can adjust the code later. Case in point, I’m not familiar with $TMPL->swap_var_single()… A quick read of the template class page makes it look like the official way to replace tags with data, right? I basically made my own version of this as I muddle through the last couple weeks.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete
A quick read of the template class page makes it look like the official way to replace tags with data, right?

That’s right =) . I know it’s a bit overwhelming at first, it takes some time to sink in.

Remember that there is a lot of first and third party code out there that you can look at if you need inspiration.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.