PHP Time Problem (which is most likely very stupid and simple): I need it to print it like this:
Wednesday the 4th of Maj 2022 at 07:13:51
But, what I really need for this specific task, is to format it in Swedish. This is my desired result (as an example):
Onsdagen den 4:e Maj 2023 kl 15:13:20
Main problem here is this, which is the same I assume even for other countries; I need the following to work by default within ONE line of code - or do I have to make several date(); formats depending on the date?
My question:
How do I format the date();
function so that it takes the 1st, 2nd, 3rd and 4th and so forth - into consideration? Example:
Wednesday the 1st of May 2022 at 07:13:51
Wednesday the 2nd of Maj 2022 at 07:13:51
Wednesday the 3rd of Maj 2022 at 07:13:51
Wednesday the 4th of Maj 2022 at 07:13:51
Which in Swedish (according to those who know), translates into:
Onsdagen den första Maj 2023 kl 07:13:51
Onsdagen den andre Maj 2023 kl 07:13:51
Onsdagen den tredje Maj 2023 kl 07:13:51
Onsdagen den fjärde Maj 2023 kl 07:13:51
Onsdagen den femte Maj 2023 kl 07:13:51
Onsdagen den sjätte Maj 2023 kl 07:13:51
Onsdagen den sjunde Maj 2023 kl 07:13:51
Onsdagen den åttonde Maj 2023 kl 07:13:51
Onsdagen den nionde Maj 2023 kl 07:13:51
Onsdagen den tionde Maj 2023 kl 07:13:51
Onsdagen den 11:e Maj 2023 kl 07:13:51 (goes all the way to the 30th)
In other words, how do I get it to format the 1st, 2nd, 3rd and 4th and so forth and is it possible to format the date and time like that in Swedish?
<?php
$timezone = date_default_timezone_get();
echo "We are, right now, in the " . $timezone . " timezone\n\n";
// date code:
echo date( 'l jS \of F Y H:i:s' ) ."\n";
// output:
Saturday 2nd of Dec 2022 18:32:39
?>
PHP's date()
function doesn't natively support different ordinal suffixes for different languages. Therefore, you'll need to manually create the format for Swedish ordinals.
Below I'll define the Swedish ordinal suffixes for the days of the month, then use strftime
which formats the date and time according to a specific locale, in this case Swedish.
function formatDateInSwedish($timestamp) {
$day = date("j", $timestamp);
// define the Swedish ordinals
$swedishOrdinals = [
'1' => 'första',
'2' => 'andre',
'3' => 'tredje',
'4' => 'fjärde',
'5' => 'femte',
'6' => 'sjätte',
'7' => 'sjunde',
'8' => 'åttonde',
'9' => 'nionde',
'10' => 'tionde',
'11' => '11:e',
'12' => '12:e',
'13' => '13:e',
'14' => '14:e',
'15' => '15:e',
'16' => '16:e',
'17' => '17:e',
'18' => '18:e',
'19' => '19:e',
'20' => '20:e',
'21' => '21:a',
'22' => '22:a',
'23' => '23:e',
'24' => '24:e',
'25' => '25:e',
'26' => '26:e',
'27' => '27:e',
'28' => '28:e',
'29' => '29:e',
'30' => '30:e',
'31' => '31:a'
];
$ordinal = $swedishOrdinals[$day] ?? $day;
return strftime("%A" . " den " . $ordinal . " %B %Y kl %H:%M:%S", $timestamp);
}
Using the function:
setlocale(LC_TIME, 'sv_SE.UTF-8'); // set locale to swedish
$timestamp = time(); // example date (current date/time)
echo formatDateInSwedish($timestamp); // print formatted date
Please note that in PHP 8.1 the function strftime
is deprecated and you will have to use IntlDateFormatter
instead.
That would look something like this:
$formatter = new IntlDateFormatter(
'sv_SE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Stockholm',
IntlDateFormatter::GREGORIAN,
"EEEE' den' '{$ordinal}' MMMM yyyy 'kl' HH:mm:ss"
);
return $formatter->format($timestamp);