Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-04-27 10:44:24

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,596
Website

International from-to date formatter

This shortcode replaces the typical two sets of dates to a from-to style output in a specific language (regardless of what’s set in the admin area):

27 April 2022 – 28 April 2022       ->    27. – 28. April 2022
27 April 2022 – 3 May 2022          ->    27. April – 3. Mai 2022
30 December 2022 – 3 January 2023   ->    30. Dezember 2022 – 3. Januar 2023

Background
Given that strftime is deprecated as of PHP 8.1 and the method of using setlocale to force days and months to be output in a specific language has no effect on php’s date function, I rewrote my old from-to date formatter snippet to work with IntlDateFormatter.

Setup

Prelims

If you haven’t already, set up a shortcode form type in your forms:

  • Visit Admin › Preferences › Admin and the set Advanced Options to On.
  • Now visit Admin › Preferences › Advanced options and add the following snippet to Custom form template types and Save:
[shortcode]
title="Shortcode"

Create shortcode form

Create a new form at Presentation › Forms with the tag name of your choosing and assign it to the “Shortcode” form type. I called mine event_dates but it can be whatever you want. Paste the following in the Form code.

<txp:php>
    global $thisarticle;

    // If using custom fields for start and end dates, convert string to timestamp using strtotime()
    // e.g. $start_date = strtotime(custom_field(array('name'=>'start_date'))); where start_date is the custom field label
    $start_date = $thisarticle['posted'];
    $end_date   = $thisarticle['expires'];

    // Locale and timezone
    $locale = "de_DE";
    $timezone = "Europe/Berlin";

    // ISO formatting notation: see https://carbon.nesbot.com/docs/#iso-format-available-replacements
    $pattern_full = "d. MMMM YYYY";
    $pattern_day_month = "d. MMMM";
    $pattern_day_only = "d.";

    $separator = '&thinsp;–&thinsp;';

    // compare start and end dates: is same year?
    if (date('Y', $start_date) == date('Y', $end_date)) {

        if (date('m', $start_date) == date('m', $end_date)) {
            // … and same month
            $pattern = $pattern_day_only;

            if (date('j', $start_date) == date('j', $end_date)) {
                // same start and end day?
                $pattern = "";
                $separator = "";
            }
        } else {
            // … but not same month
            $pattern = $pattern_day_month;
        }
    } else {
        // is different year
        $pattern = $pattern_full;
    }

    // International date formatter
    $out = datefmt_create(
        $locale,
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        $timezone,
        IntlDateFormatter::GREGORIAN,
        $pattern_full
    );

    // Format end date (using $pattern_full)
    $end = $out->format($end_date);

    // Format start date using determined pattern
    $out->setPattern($pattern);
    $start = $out->format($start_date);

    // Output string
    echo $start . $separator . $end;
</txp:php>

Settings

The above code is set to the German locale, timezone and date formatting, but you can change all these by changing the variables at the top of the code. If you want to use custom fields rather than the Posted and Expires fields, you can set those as the basis for the start and end dates. In that case, you need to convert the 2022-04-27 text string from your custom field to a timestamp using strtotime.

Usage

In place of where you would normally put the following in your page template or forms:

<txp:posted /> – <txp:expires />

you can now simply put:

<txp::event_dates />

(or whatever you called your form). The double colons are necessary to denote the shortcode.


TXP Builders – finely-crafted code, design and txp

Offline

#2 2022-04-27 21:07:48

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: International from-to date formatter

Oooh, I’ve got a version of this as a block of PHP somewhere for a site, but this is a more robust (and modern) implementation. Imma steal it. Thank you for sharing!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2022-04-27 21:20:24

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,596
Website

Re: International from-to date formatter

As ever, something occurred to me after having made it: one could in theory make it more versatile by passing in the various settings as yield variables. It’s not something you’d need often within a single site, e.g. for multilingual sites, but perhaps good for reusability…


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB