On my setup I had no access to some PHP extension to help with formatting date. Maybe it’s imperfect or not the way it should be done but it worked for me, as I had the exact same need as you. So here it is, for anyone looking for different dates implementations :
Created file site/models/default.php
class DefaultPage extends Page {
public function formatDate( $dateField = 'published' ) {
// Check if the date is empty or invalid
if (!$dateField) return '';
// Create a DateTime object from the date field (published is assumed)
$dateTime = new DateTime($dateField);
// Define translations for month names
$months = [
'en' => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
'fr' => ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
// Add more languages as needed
];
// Get the current language/locale from Kirby (fr, en, etc)
$locale = kirby()->language()->code();
// Ensure the locale exists in the months array (defaults to fr)
if (!array_key_exists($locale, $months)) {
$locale = 'fr';
}
$monthIndex = (int)$dateTime->format('n') - 1; // Get the month index (0-based)
$monthName = $months[$locale][$monthIndex]; // Get the corresponding string
$formattedDate = $dateTime->format('j') . ' ' . $monthName . ' ' . $dateTime->format('Y');
return $formattedDate;
}
}