Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Use "default" modifier with a date

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Hpatoio
Smarty Rookie


Joined: 29 Apr 2003
Posts: 25
Location: Italy

PostPosted: Wed Apr 30, 2003 7:15 pm    Post subject: Use "default" modifier with a date Reply with quote

Ok, Suppose that U need to display the message "Not specified" when a date is not set (i.e. 0000-00-00)

fisrt I modified the "default" modifier like this :

Code:

function smarty_modifier_default($string, $default = '')
{
    if (!isset($string) || $string == '' || $string == '0000-00-00')
        return $default;
    else
        return $string;
}


But it works just if U don't use the "date_format" modifier !!

Because if U write in your template:
Code:

{$result.birth_date|default:"Not Specified"|date_format:"%d %B %Y"}


Smarty display today's date !!!

Any idea ?? Question

Simone
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 5:01 am    Post subject: Reply with quote

Don't re-write the modifier and write your template expression as:
Code:
{$result.birth_date|date_format:"%d %B %Y"|default:"Not Specified"}


cheers
Back to top
View user's profile Send private message
Hpatoio
Smarty Rookie


Joined: 29 Apr 2003
Posts: 25
Location: Italy

PostPosted: Sun May 04, 2003 7:11 pm    Post subject: Are you sure ? Reply with quote

Sorry ... but I don't think it works !!

Because if I have '0000-00-00' as date and I use your code ...

I get : "Birthdate : 30 November 1999" Shocked

I have the standard modifier for "default" ...

tell me where I'm wrong !!!

Simone
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun May 04, 2003 7:31 pm    Post subject: Reply with quote

date_format uses strtotime() to parse a date. When you pass 0000-00-00 to this, you get back a date representing 1999/11/30. I'm not sure why, but obviously it does not interpret 0000-00-00 the way you want.

If you want to make this work right, don't use 0000-00-00 to represent an empty date, just use an empty string.

Monte
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun May 04, 2003 7:37 pm    Post subject: Reply with quote

i guess (but don't know 100%):
0000-00-00 is equal to 2000-00-00
thats 2000-01-01 minus one month and one day
thats 1999-12-01 minus one day
thats 1999-11-30
thats where the funny date comes from.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Hpatoio
Smarty Rookie


Joined: 29 Apr 2003
Posts: 25
Location: Italy

PostPosted: Sun May 04, 2003 7:43 pm    Post subject: Ok ... I wrote mime ... Reply with quote

I use '0000-00-00' as default "empty" date becuse MySQL give that to me.

I've modified the plugin like this :

Code:

require_once $this->_get_plugin_filepath('shared','make_timestamp');
function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null, $empty_msg="Not specified")
{

   if ($string == '0000-00-00' || trim($string) == '') {
       if (isset($default_date) && $default_date != '') {      
       return strftime($format, smarty_make_timestamp($default_date));
       } else {
          return $empty_msg;
       }      
   }
      
   return strftime($format, smarty_make_timestamp($string));

}


I have also put an extra parameter $empty_msg so that you can specify the message you want when don't give a date.

... do you think can works Question

Simone
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP