version=pmwiki-2.3.33 ordered=1 urlencoded=1 author=Petko charset=UTF-8 csum=[[#ftimeformatsend]] (+39) ctime=1176718061 name=PmWiki.MarkupExpressions rev=113 targets=Cookbook.LocalTimes,PmWiki.BasicVariables,PmWiki.LayoutVariables,PmWiki.WikiStyles,PmWiki.PageListTemplates,PmWiki.PageLists,PmWiki.ConditionalMarkup,Cookbook.MarkupExpressionSamples,Cookbook.MarkupExprPlus text=(:Summary:String and formatting operations:)%0a(:Audience: authors (advanced) :)%0aThe %25pmhlt%25[@{(...)}@] "expression markup" allows for a variety of string and formatting operations to be performed from within markup. Operations defined by this recipe include ''[[#substr|substr]]'', ''[[#ftime|ftime]]'', ''[[#strlen|strlen]]'', ''[[#rand|rand]]'', ''[[#mod|mod]]'', ''[[#to|toupper / tolower]]'', ''[[#ucfirst|ucfirst]]'', ''[[#ucwords|ucwords]]'', ''[[#pagename|pagename]]'' and ''[[#asspaced|asspaced]]''.%0aMarkup expressions can be [[#nesting|nested]], using the markup [@{(...(...)...)}@].%0a%0a[[#substr]]%0a!! ''substr''%0a%0aThe "substr" expression extracts portions of a string. The arguments are%0a# the string to be processed. Always quote the string to be processed.%0a# the initial position of the substring. Note that the initial position argument is zero-based (i.e., the first character is referenced via a "0").%0a# the number of characters to extract%0a(:markup class="horiz":)%0a {(substr "PmWiki" 2 3)}%0a {(substr "PmWiki" 2)}%0a {(substr "{*$Group}" 2)}%0a {(substr "PmWiki" 0 1)}%0a {(substr "PmWiki" 0 -3)}%0a {(substr "PmWiki" -3)}%0a(:markupend:)%0a%0aTo obtain the last n characters of a string use %25pmhlt%25[@{(substr "string" -n)}@]%0a[[%3c%3c]]%0aTo truncate the last n characters of a string use %25pmhlt%25[@{(substr "string" 0 -n)}@]%0a%0a[[#ftime]]%0a!! ''ftime''%0a%0a"Ftime" expressions are used for date and time formatting. The generic form is%0a%0a->%25pmhlt%25[@{(ftime "fmt" "when")}@]%0a->%25pmhlt%25[@{(ftime fmt="fmt" when="when" tz="tz" locale="locale")}@]%0a%0awhere ''fmt'' is a formatting string and ''when'' is the time to be formatted. The arguments can be in either order and may use the optional "fmt=" and "when=" labels. %0a%0aThe "tz" (time zone) argument allows the setting of a different time zone, only available on installations running PHP 5.1 or newer. See [[https://www.php.net/timezones|the list of supported time zones]], any value not found there will be silently ignored.%0a%0aThe "locale" (language locale) argument allows the time format to be printed in a different language for names of weekdays and months, as long as that language locale is available on the server. You can set different locales to try, separated with commas or spaces, for example "fr_FR.utf8,fr_FR,fr". If none of the listed locales is available, the argument is silently ignored. %0a%0aWith international locales, if you see weird characters, try adding or removing the ".utf8" or ".UTF-8" part of the locale, e.g. try both "fr_FR.utf8" and "fr_FR" to see which one works for you. Wikis with UTF-8 enabled are more likely to need the ".utf8" part.%0a%0aExamples:%0a(:markup class="horiz":)%0a {(ftime)}%0a {(ftime fmt="%25F %25H:%25M")}%0a {(ftime %25Y)}%0a {(ftime fmt=%25T)}%0a {(ftime when=tomorrow)}%0a {(ftime fmt="%25Y-%25m-%25d" yesterday)}%0a {(ftime "+1 week" %25F)}%0a {(ftime fmt=%25D "+1 month")}%0a {(ftime fmt="%25a%25e %25b" when="next week")}%0a {(ftime fmt="%25A, %25d %25B %25Y, %25Hh%25M" locale=fr_FR.utf8 tz=Europe/Paris)}%0a(:markupend:)%0a%0aThe ''fmt'' parameter is whatever is given by "fmt=", the first parameter containing a '%25', or else the site's default. The formatting codes are described at PHP's %25newwin%25 [[https://www.php.net/manual/en/function.strftime.php | strftime]] function. In addition to those, '[=%25o=]' produces the ordinal suffix ("st" for 1st), '[=%25F=]' produces ISO-8601 dates like 2022-10-30, '[=%25s=]' produces Unix timestamps, and '[=%25L=]' produces a format like %25pmhlt%25[@@2022-10-30T01:27:34Z@] which can be displayed in the timezone of the visitor per Cookbook:LocalTimes.%0a%0aSome common formatting strings:%0a%0a [= %25F =] # ISO-8601 dates "{(ftime %25F)}"%0a [= %25s =] # Unix timestamp "{(ftime %25s)}"%0a [= %25H:%25M:%25S =] # time as hh:mm:ss "{(ftime %25H:%25M:%25S)}"%0a [= %25m/%25d/%25Y =] # date as mm/dd/yyyy "{(ftime %25m/%25d/%25Y)}"%0a [= "%25A, %25B %25e%25o, %25Y"=] # in words "{(ftime "%25A, %25B %25e%25o, %25Y")}"%0a%0a[[#ftimeformats]]%0a(:details summary="All formats, click to toggle":)%0a|| class="simpletable filterable"%0a||! Category ||! Format ||!Description ||!Example ||%0a|| Day || [@%25a@] ||An abbreviated textual representation of the day ||@@Sun@@ through @@Sat@@ ||%0a|| Day || [@%25A@] ||A full textual representation of the day ||@@Sunday@@ through @@Saturday@@ ||%0a|| Day || [@%25d@] ||Two-digit day of the month (with leading zeros) ||@@01@@ to @@31@@ ||%0a|| Day || [@%25e@] ||Day of the month, with a space preceding single digits. ||"@@ 1@@" to "@@31@@" ||%0a|| Day || [@%25j@] ||Day of the year, 3 digits with leading zeros ||@@001@@ to @@366@@ ||%0a|| Day || [@%25u@] ||ISO-8601 numeric representation of the day of the week ||@@1@@ (for Monday) through @@7@@ (for Sunday) ||%0a|| Day || [@%25w@] ||Numeric representation of the day of the week ||@@0@@ (for Sunday) through @@6@@ (for Saturday) ||%0a|| Day || %25red%25[@%25o@]%25%25 ||Ordinal suffix of the date (PmWiki-specific)||@@st@@ in January 1st ||%0a|| Week || [@%25U@] ||Week number of the given year, starting with the first Sunday as the first week ||@@13@@ (for the 13th full week of the year) ||%0a|| Week || [@%25V@] ||ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week ||@@01@@ through @@53@@ (where @@53@@ accounts for an overlapping week) ||%0a|| Week || [@%25W@] ||A numeric representation of the week of the year, starting with the first Monday as the first week ||@@46@@ (for the 46th week of the year beginning with a Monday) ||%0a|| Month || [@%25b@] ||Abbreviated month name, based on the locale ||@@Jan@@ through @@Dec@@ ||%0a|| Month || [@%25B@] ||Full month name, based on the locale ||@@January@@ through @@December@@ ||%0a|| Month || [@%25h@] ||Abbreviated month name, based on the locale (an alias of [@%25b@]) ||@@Jan@@ through @@Dec@@ ||%0a|| Month || [@%25m@] ||Two digit representation of the month ||@@01@@ (for January) through @@12@@ (for December) ||%0a|| Year || [@%25C@] ||Two digit representation of the century (year divided by 100, truncated to an integer) ||@@19@@ for the 20th Century ||%0a|| Year || [@%25g@] ||Two digit representation of the year going by ISO-8601:1988 standards (see [@%25V@]) ||@@09@@ for the week of January 6, 2009 ||%0a|| Year || [@%25G@] ||The full four-digit version of [@%25g@] ||%25red%25@@2008@@%25%25 for the week of January 3, %25red%252009%25%25 ||%0a|| Year || [@%25y@] ||Two digit representation of the year ||@@09@@ for 2009, @@79@@ for 1979 ||%0a|| Year || [@%25Y@] ||Four digit representation for the year ||@@2038@@ ||%0a|| Time || [@%25H@] ||Two digit representation of the hour in 24-hour format ||@@00@@ through @@23@@ ||%0a|| Time || [@%25k@] ||Hour in 24-hour format, with a space preceding single digits ||"@@ 0@@" through "@@23@@" ||%0a|| Time || [@%25I@] ||(Uppercase 'i') Two digit representation of the hour in 12-hour format ||@@01@@ through @@12@@ ||%0a|| Time || [@%25l@] ||(Lowercase 'L') Hour in 12-hour format, with a space preceding single digits ||"@@ 1@@" through "@@12@@" ||%0a|| Time || [@%25M@] ||Two digit representation of the minute ||@@00@@ through @@59@@ ||%0a|| Time || [@%25p@] ||UPPER-CASE 'AM' or 'PM' based on the given time ||@@AM@@ for 00:31, PM for 22:23 ||%0a|| Time || [@%25P@] ||lower-case 'am' or 'pm' based on the given time ||@@am@@ for 00:31, pm for 22:23 ||%0a|| Time || [@%25r@] ||Same as "[@%25I:%25M:%25S %25p@]" ||@@09:34:17 PM@@ for 21:34:17 ||%0a|| Time || [@%25R@] ||Same as "[@%25H:%25M@]" ||@@00:35@@ for 12:35 AM, @@16:44@@ for 4:44 PM ||%0a|| Time || [@%25S@] ||Two digit representation of the second ||@@00@@ through @@59@@ ||%0a|| Time || [@%25T@] ||Same as "[@%25H:%25M:%25S@]" ||@@21:34:17@@ for 09:34:17 PM ||%0a|| Time || [@%25X@] ||Preferred time representation based on locale, without the date ||@@03:59:16@@ or @@15:59:16@@ ||%0a|| Time || [@%25z@] ||The time zone offset. ||@@-0500@@ for US Eastern Time ||%0a|| Time || [@%25Z@] ||The time zone abbreviation. ||@@EST@@ for Eastern Time ||%0a|| Date-Time || [@%25c@] ||Preferred date and time stamp based on locale ||@@Tue Feb 5 00:45:10 2009@@ for February 5, 2009 at 12:45:10 AM ||%0a|| Date-Time || [@%25D@] ||Same as "[@%25m/%25d/%25y@]" ||@@02/05/09@@ for February 5, 2009 ||%0a|| Date-Time || [@%25F@] ||Same as "[@%25Y-%25m-%25d@]" ||@@2009-02-05@@ for February 5, 2009 ||%0a|| Date-Time || [@%25s@] ||Unix Epoch Time timestamp ||@@305815200@@ for September 10, 1979 08:40:00 AM ||%0a|| Date-Time || [@%25x@] ||Preferred date representation based on locale, without the time ||@@02/05/09@@ for February 5, 2009 ||%0a|| Date-Time || %25red%25[@%25L@]%25%25 ||Human-readable UTC timestamp which will be displayed formatted either as $TimeFmt or in the local time zone of the visitor per $EnableLocalTimes (PmWiki-specific) ||%25pmhlt%25[@@2022-09-25T11:49:08Z@] ||%0a|| Miscellaneous || [@%25n@] ||A newline character ("@@\n@@") ||-- ||%0a|| Miscellaneous || [@%25t@] ||A Tab character ("@@\t@@") ||-- ||%0a|| Miscellaneous || [@%25%25@] ||A literal percentage character ("@@%25@@") ||-- ||%0a%0aThe "preferred" shortcut formats may show different values depending on whether the IntlDateFormatter PHP functions are enabled, or not -- for consistent results, use the full format you need. The names of months and days may be translated with the @@locale=@@ argument, if that locale is installed on the server.%0a%0a(:detailsend:)%0a[[#ftimeformatsend]]%0a%0aNote that if you use an undefined format string such as [@%25q@] or [@%25E@], it will be left unchanged by ''ftime'', but later PmWiki may process it as a [[WikiStyle(s)]] which may be unexpected.%0a%0aThe ''when'' parameter understands many different date %25newwin%25[[https://www.php.net/manual/en/datetime.formats.php|formats]]. The when parameter is whatever is given by "when=", or whatever parameter remains after determining the format parameter. Some examples:%0a%0a 2007-04-11 # ISO-8601 dates%0a 20070411 # dates without hyphens, slashes, or dots%0a 2007-03 # months%0a @1176304315 # Unix timestamps (seconds since 1-Jan-1970 00:00 UTC)%0a now # the current time%0a today # today @ 00:00:00%0a yesterday # yesterday @ 00:00:00%0a "next Monday" # relative dates%0a "last Thursday" # relative dates%0a "-3 days" # three days ago%0a "+2 weeks" # two weeks from now%0a%0a'''Note:''' If you want to convert a Unix timestamp you '''must''' prefix with the @. Thus, %25pmhlt%25[@"{(ftime "%25A, %25B %25d, %25Y" @1231116927)}"@].%0a%0aThe ''when'' parameter uses PHP's %25newwin%25[[https://www.php.net/strtotime | strtotime]] function to convert date strings according to the GNU [[https://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html|date input formats]]; as of this writing it only understands English phrases in date specifications.%0a%0aThe variable $FTimeFmt can be used to override the default date format used by the "ftime" function. The default $FTimeFmt is $TimeFmt.%0a%0a[[#strlen]]%0a!! ''strlen''%0a%0aThe "strlen" expression returns the length of a string. The first argument is the string to be measured.%0a%0a(:markup class="horiz":)%0a {(strlen "{$:Summary}")}%0a(:markupend:)%0a%0a[[#rand]]%0a!! ''rand''%0a%0aThe "rand" expression returns a random integer. The first argument is the minimum number to be returned and the second argument is the maximum number to be returned. If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use (rand 5 15).%0a%0a(:markup class="horiz":)%0a {(rand)}%0a {(rand 1 99)}%0a(:markupend:)%0a%0a[[#mod]]%0a!! ''mod''%0a%0aThe advanced "mod" expression returns the modulo (remainder) of the division of two numbers. It may be used in advanced [[PageList templates]] together with %25pmhlt%25[@{$$PageCount}@] to insert markup every (modulo) entries, for example to create alternate styled "zebra" table rows, or to insert a line/row break. (See also [[PageLists]], [[WikiStyles]] and [[ConditionalMarkup]].)%0a%0a(:markup class="horiz":)[@%0a>>comment%3c%3c%0a%25define=bg1 item bgcolor=#f88%25%0a%25define=bg2 item bgcolor=#ff8%25%0a%25define=bg0 item bgcolor=#8f8%25[=%0a[[#altrows]]%0a* %25bg{(mod {$$PageCount} 3)}%25 {=$Name} ({$$PageCount})%0a[[#altrowsend]]=]%0a>>%3c%3c%0a(:pagelist fmt=#altrows group=PmWiki count=10:)%0a@]%0a%0a[[#to]]%0a!! ''toupper'' / ''tolower''%0a%0aThe "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(toupper "{$:Summary}")}%0a {(tolower "{$:Summary}")}%0a(:markupend:)%0a%0a[[#ucfirst]]%0a[[#ucwords]]%0a!!''ucfirst'' / ''ucwords''%0a%0aThe "ucfirst" expression converts to uppercase the first character of the string, and "ucwords", the first character of each word. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(ucfirst "{$:Summary}")}%0a {(ucwords "{$:Summary}")}%0a(:markupend:)%0a%0a%0a[[#pagename]]%0a!! ''pagename''%0a%0aThe "pagename" expression builds a pagename from a string. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(pagename "{$:Summary}")}%0a(:markupend:)%0a%0a[[#asspaced]]%0a!! ''asspaced''%0a%0aThe "asspaced" expression formats wikiwords. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(asspaced "{$FullName}")}%0a(:markupend:)%0a%0a[[#nesting]]%0a!! Nesting expressions%0a%0aMarkup expressions can be nested. Omit the curly braces for the inner expressions:%0a%0a(:markup class="horiz":)%0a {(tolower (substr "Hello World" 2))}%0a(:markupend:)%0a%0a[[#notes]] %0a!! Notes%0a%0a* For PmWikis version 2.2.33 or older, the string-processing expressions may not work properly on multibyte UTF-8 characters. Newer versions should work fine.%0a%0a!! See also%0a(:pagelist group=PmWiki name=PageVariables,PageTextVariables,ConditionalMarkup fmt=#titlesummary:)%0a* Cookbook:MarkupExpressionSamples — '-custom markup expression samples-'%0a* Cookbook:MarkupExprPlus time=1714124964