Output of date/time in German in PHP - php

Got a bit of an odd question. After creating a PHP script for a friend who lives in Germany, he has decided he needs the time/date in German format, an example being:
Montag, 16.März 2014
Is there a date/time function that can do this in PHP?
Any help much appreciated, Thanks!

Use strftime() and set the locale to German
Example
<?php
setlocale(LC_ALL, "de_DE.utf8");
echo strftime("%A, %e.%B %Y");
?>
Which would output: Donnerstag, 7.August 2014

Related

date format - Regional settings - Months in lowercase

I'm a noob (I confess it) and can't manage to find a solution to my problem.
I'm setting up the date format on Drupal and it uses the PHP date format.
Right now it's "d F Y", so it appears as 07 Dicembre 2021 (in Italian), but in Italian months are written out in lowercase. Is there a way to transform Dicembre into dicembre? I couldn't find a proper way.
Thanks for your help!
Using strtolower() the output will be as you expected:
<?php echo strtolower(date("M"),2);?>
So to implement it only for Month, simply break the date format into 3 parts. Day , Month, Year.
Use drupal module:
https://www.drupal.org/project/simple_field_formatter
Go to:
/admin/structure/types/manage/[YOURTYPE]/display
For your date field, click the gearwheel on the right (format settings) and activate the checkbox for strtolower
Or create your own FieldFormatter: https://www.webwash.net/how-to-create-a-custom-field-formatter-in-drupal-8/
You should use the locale aware strftime, which formats according to the current locale.
setlocale(LC_TIME, "it_IT");
echo strftime("%d %B %Y"); // 07 dicembre 2021

Problem with LC_TIME translations on CakePhp

I have a cakephp 2.9 multilanguage app and using .po files for translating strings like __('example'). So far so good but I've been trying for a while now to translate this date from:
Fri, Aug 31st 2018, 16:03
to:
Vie, Ago 31 2018, 16:03
This is my code:
setlocale("LC_ALL", "es_ES.UTF-8");
App::uses('CakeTime', 'Utility');
echo CakeTime::nice();
I've downloaded the spanish files from the localized repo (https://github.com/cakephp/localized) and put them into app\Locale\spa\LC_TIME and still the date keeps showing in english.
Can someone help me? Maybe it's the wrong approach?
Well, didn't took me long after I posted the question to realize all the ways I have tried were ok, using caketime or strftime directly, the problem was with the setlocale:
Changed this:
setlocale("LC_ALL", "es_ES.UTF-8");
to this:
setlocale(LC_TIME, 'es_ES.utf8','esp');
So my two results are:
echo ucfirst(strftime('%A '.$number_of_the_day.' %B %Y'));
//Viernes 31 agosto 2018
echo __('Today is ').CakeTime::nice();
//Hoy es vie, ago 31st 2018, 16:29
It worked like a charm, however I'd very much like a table where all this language codes are explained so we can use the right syntax for each and not guess, just like I did.

How would I change language of days of week in php?

I am looking for a simple way to change the language of the days of the week. I use the simple code below to call out the current day Monday, Tuesday, but I need it in French, and I've tried setting locale etc but this is not working for me.
I think arrays would do the job, but again, I could not get it to work with my limited knowledge.
<?php echo date('l'); ?>
You should use strftime() function for this - http://php.net/manual/ru/function.strftime.php. date() is not locale dependent.
i hope this will help
<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A and");
for more info click here

How do you translate the timezone abbreviation in PHP

When using the PHP to return the timezone abbreviation with either date('T') or strftime('%Z') the result does not translate to the localized version of these abbreviations.
In french I have been told the following at the correct translations:
EDT == HAE
EST == HNE
I have tried the following code example:
<?php
setlocale(LC_ALL, 'fr_FR');
echo strftime('%Z');
echo date('T');
?>
All attempts produce EST/EDT rather than the translated versions.
I get the same on Mac and Linux.
I inspected the Gettext MO file for French, like this:
/usr/local/bin/msgunfmt /usr/share/locale/fr/LC_MESSAGES/texinfo.mo
This shows all the translations for things like months and days.
So you should get translations if you do:
echo strftime("%A %e %B %Y\n");
// gives "Vendredi 27 juin 2014"
But the translation file does not appear to contain translations for EST, or EDT - or in fact any others I looked for.
So I guess the answer is that these strings are simply not translated in the standard locale packages.

Translate date("d F Y (H:i) function php

I'm brazilian and there's a wordpress plugin that uses
" . date("d F Y (H:i)",$date) . "
Output: 16 January 2013 (00:54)
But it should be 16 Janeiro 2013 (00:54), in portuguese... How can I change it?
PS: I think maybe the date is set by an external file provided by the plugin creator :p I'm not sure though
WordPress has date_i18n to retrieve the date in localized format, based on timestamp.
Try:
echo date_i18n("d F Y (H:i)", $timestamp);
WordPress has an extensive page on how to format date and time.
For the french language I use this
setlocale(LC_ALL, 'fra');
echo strftime("%A %d %B %Y",time());
For in portuguese
setlocale(LC_ALL, 'ptg'); //
echo strftime("%A %d %B %Y",time());
see Language Strings Country/Region Strings.
The documentation for date already answers this:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
And strftime says that the way to do what is by using setlocale:
Format the time and/or date according to locale settings. Month and
weekday names and other language-dependent strings respect the current
locale set with setlocale().
That said, the C locale-aware functions do not provide sufficient functionality for languages that have cases. In such situations (i.e. most of the time) you need to roll your own.

Categories