|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Hpatoio Smarty Rookie
Joined: 29 Apr 2003 Posts: 25 Location: Italy
|
Posted: Wed Apr 30, 2003 7:15 pm Post subject: Use "default" modifier with a date |
|
|
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 ??
Simone |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun May 04, 2003 5:01 am Post subject: |
|
|
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 |
|
Hpatoio Smarty Rookie
Joined: 29 Apr 2003 Posts: 25 Location: Italy
|
Posted: Sun May 04, 2003 7:11 pm Post subject: Are you sure ? |
|
|
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"
I have the standard modifier for "default" ...
tell me where I'm wrong !!!
Simone |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun May 04, 2003 7:31 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sun May 04, 2003 7:37 pm Post subject: |
|
|
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 |
|
Hpatoio Smarty Rookie
Joined: 29 Apr 2003 Posts: 25 Location: Italy
|
Posted: Sun May 04, 2003 7:43 pm Post subject: Ok ... I wrote mime ... |
|
|
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
Simone |
|
Back to top |
|
|
|
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
|