I have a column which is a Timestamp.
It records something like: 2010-02-08 12:10:22
Then I use this in php:
$postdate = date( "j F", strtotime( $row['modify_date'] ) );
And it can output something like: 8 February
My Q is, how can I change the date-text so it outputs the month name in another language (swedish specifically)?
Ex: January is in Swedish Januari
Thanks
The native PHP function for that is strftime().
%B Full month name, based on the locale January through December
if the server is not in the swedish locale, use setlocale().
That said, I have had so many troubles with setlocale() in the past, especially on shared hosting, that I tend to keep an array of month names in whatever config file / dictionary file the project has:
$monthnames["de"] = array("Januar", "Februar", "März", "...");
$monthnames["fi"] = array("Tammikuu", "Helmikuu", "...");
echo $monthnames[$language][date("n", strtotime( $row['modify_date'] ))];
If you use setlocale() then you can output locale-specific names via strftime().
Related
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
I have a date field in my table material
id: int;
dat_operation : date;
name : varchar(255);
I would like to know how to translate date format in French
I tried with:
<?php echo date("F j Y",strtotime($var['date_operation']));?>
But i have this result
June 14 2016
First, you'll have to set the "locale information", to specify which language you want to use. Keep in mind, that even though you set that language, it needs to be installed on the server you're running on. It most likely is, but you'll notice if the setlocale has no effect (default is English).
The second thing you'll need to know, is that date() isn't affected by this, you'll have to use strftime() instead, which has a slightly different formatting, which you'll find on the documentation.
An example of using French dates with these two functions:
setlocale(LC_ALL, 'fr_FR');
echo strftime("%B %e %Y", strtotime($var['date_operation']));
Reference and documentation:
http://php.net/manual/en/function.setlocale.php
http://php.net/manual/en/function.strftime.php
The modern and rock-solid approach is the intl (from "Internationalization") extension, which offers e.g. the IntlDateFormatter class:
$date = new DateTime('2016-06-14');
$fmt = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE, 'Europe/Paris', IntlDateFormatter::GREGORIAN);
var_dump($fmt->format($date));
string(12) "14 juin 2016"
If you think it's overkill for your project, you can use the legacy strftime() function but you need to change current locale:
$date = strtotime('2016-06-14');
var_dump(setlocale(LC_TIME, 'fr_FR', 'fr')); // Need to try values until you get true
var_dump(strftime('%B %e %Y', $date));
You need to have French locale data installed. In my experience, this works better on Unix-like systems than on Windows.
I am bumping against a language barrier when I try to push out out meta data.
$string_date = implode (';', get_post_meta( get_the_ID(), 'dbt_opleverdatum' ) );
$format = get_option( 'date_format' );
$string_date = date($format, $string_date)
Since I save the meta data as a php time stamp, its being put away as a 1451865600 for example (which is january 4th 2016). I pick it up, and put it out via a date stamp which put it down for me as 4 January 2016
However, the whole site is run in Dutch and as I am looking through the code I notice the functions get_the_date() which retrieves the date of posting. But that will output 4 januari 2016 because that goes via the settings in Wordpress it self. It notices the wordpress is in Dutch and takes that in account.
Is there a function I can put my PHP date through that as well, so my date will be propperly shown in Dutch, instead of English?
From: http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
$ts = 1451865600;
$lang = get_option('WPLANG');
setlocale(LC_TIME, $lang);
$format2 = '%e %B %G';
$string_date = strftime($format2, $ts);
Unfortunately I don't know how to convert the date_format in the options to the format string of strftime, but if it's your plugin, then you can change it to mean the format for strftime, or if you use it in other places as well, then add another option for this.
Notice: using setlocale changes the locale globally, so it might be necessary to revert the chage after the call to strftime:
$current_locale = setlocale(LC_TIME, "0");
setlocale(LC_TIME, $lang);
...
setlocale(LC_TIME, $current_locale);
I am taking a value entered by a user in a jQuery UI datepicker and converting it to a to a php date for insertiion in a database like so:
$date_from = post('date_valid_from');
$date_from = date('Y-m-d', strtotime($date_from));
This works fine if the input is formatted in US format (mm/dd/yyyy), however as my system will be used by people in the UK it needs to be input in the UK format (dd/mm/yyyy). When converting the UK dates to a php date, the month and day are switched around (Feb 1 2014 - 01/02/2014 - becomes January 2 2014 and Feb 28 2014 - 28/02/2014 - becomes January 1 1970).
Is there anyway I can overwrite the default date format to work with UK format?
(This project is using Code Igniter if there is a CI config setting I can set)
Use DateTime() instead. It's more flexible than date() and strtotime():
$date_from = DateTime::createFromFormat('d/m/Y', $date_from);
$date_from = $date_from->format('Y-m-d');
If you want to support both formats (mm/dd/yyyy and dd/mm/yyyy) just swap out the first parameter of DateTime::createFromFormat(). Using a variable would make that easy to do.
This is a good example case for localization in PHP.
To format a local time/date according to locale settings you can use strftime
http://php.net/strftime
Then can parse a string using a specific format with strptime
http://php.net/manual/en/function.strptime.php
For more information about localization you can check this link as well.
http://php.net/manual/en/function.setlocale.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.