FORSMILE
JA
Smarty2016/02/19

[Smarty] Changing the Display of Numbers and Dates

There are many situations where variables need to be formatted directly within the template.

Back to Blog

There are many situations where variables need to be formatted directly within the template.

For instance, when you need to manage image files or other filenames using a three-digit format like 001, 002, etc.

I'd like to introduce you to the variable modifiers available for use directly within Smarty templates.

STRING_FORMAT--CHANGING NUMBER FORMATS FOR VARIABLES

php
{$num}
{$num|string_format:"%.2f"}
{$num|string_format:"%03d"}
{$num|string_format:"%02d"}
{$num|string_format:"%d"}
text
2.4362
2.43
002
02
2

You'll likely find yourself frequently using the method of adding leading zeros, or formatting numbers as integers.

text
% - A percent sign. No argument is required.
b - The argument is treated as an integer and presented as a binary number.
c - The argument is treated as an integer and presented as the character corresponding to its ASCII value.
d - The argument is treated as an integer and presented as a decimal number.
e - The argument is treated as scientific notation (e.g., 1.2e+2). The precision specifier indicates the number of digits after the decimal point in PHP 5.2.1 and later versions. In earlier versions, it meant the number of significant digits (one less than the specified value).
E - Same as %e, but uses uppercase (e.g., 1.2E+2).
f - The argument is treated as a double and presented as a floating-point number.
F - The argument is treated as a float and presented as a floating-point number (not locale-dependent). Available in PHP 4.3.10 and PHP 5.0.3 or later.
g - A shortened form of %e and %f.
G - A shortened form of %E and %f.
o - The argument is treated as an integer and presented as an octal number.
s - The argument is treated as a string and presented.
u - The argument is treated as an integer and presented as an unsigned decimal number.
x - The argument is treated as an integer and presented as a hexadecimal number (lowercase).
X - The argument is treated as an integer and presented as a hexadecimal number (uppercase).

DATE_FORMAT--FORMATTING DATES

This formats dates and times. `smarty.now` is a reserved variable that represents the current timestamp.

php
{$smarty.now|date_format:"%H:%M:%S"}
{$smarty.now|date_format:"%H時%M分"}
text
09:45:30
09時45分

Reference: http://www.smarty.net/docsv2/ja/language.modifier.date.format.tpl

date_format conversion specifiers

text
%a - Abbreviated weekday name based on the current locale.
%A - Full weekday name based on the current locale.
%b - Abbreviated month name based on the current locale.
%B - Full month name based on the current locale.
%c - Appropriate date and time representation based on the current locale.
%C - Century (year divided by 100, rounded to an integer. 00 to 99).
%d - Day of the month as a decimal number (01 to 31).
%D - Same as %m/%d/%y.
%e - Day of the month as a decimal number. A single-digit day is preceded by a space ('1' to '31').
%g - Last two digits of the year [00,99].
%G - Year [0000,9999].
%h - Same as %b.
%H - Hour as a decimal number using a 24-hour clock (00 to 23).
%I - Hour as a decimal number using a 12-hour clock (01 to 12).
%j - Day of the year as a decimal number (001 to 366).
%k - Hour (24-hour clock) with a leading space for single digits (0 to 23).
%l - Hour (12-hour clock) with a leading space for single digits (1 to 12).
%m - Month as a decimal number (01 to 12).
%M - Minute as a decimal number.
%n - Newline character.
%p - 'am' or 'pm' based on the given time, or the corresponding string for the current locale.
%r - Time expressed in a.m. and p.m. notation.
%R - Time expressed in 24-hour notation.
%S - Second as a decimal number.
%t - Tab character.
%T - Current time. Equivalent to %H:%M:%S.
%u - Day of the week as a decimal number in the range [1,7], with 1 being Monday.
%U - Week number of the year as a decimal number. The first Sunday of the year is counted as the first day of the first week.
%V - ISO 8601:1988 week number of the current year as a decimal number, in the range 01 to 53. Week 1 is the first week with at least four days in the current year. Weeks begin on Monday.
%w - Day of the week as a decimal number. Sunday is 0.
%W - Week number of the current year as a decimal number. The first Monday of the year is counted as the first day of the first week.
%x - Date representation without the time, based on the current locale.
%X - Time representation without the date, based on the current locale.
%y - Year as a decimal number without the century (range 00 to 99).
%Y - Year as a decimal number, including the century.
%Z - Time zone or its name or abbreviation.
%% - Literal '%' character.

Recommended Smarty Books

While Smarty resources are limited to older books and Kindle editions, the following is recommended for beginners.

📦
Amazon で関連書籍・ツールを検索
PHP template engine development
Amazonで探す →(アソシエイトリンク)