Convert timestamp to Wordpress default language - php

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);

Related

PHP Printing date variable in other language

I'am trying to change dates locale.For example ive got date variable, 2015/Jun/01, 2015/Jun/19/ 2015/Mar/14 and etc. this variable called $startDate. Im trying to change english to my locale lets say lt_LT. Here is the code which i tried to use:
$startDate = ($start_date ? $start_date->format($df) : '-');
setlocale(LC_ALL, 'lt_LT.UTF-8');
echo strftime($startDate);
But it still prints dates in english, what am i doing wrong?
You need to specify output date format in strftime and also pass the $startDate as timestamp.
$startDate = "2015/Mar/14";
$startDate = strtotime(str_replace('/', '-', $startDate));
setlocale(LC_ALL, 'lt_LT.UTF-8');
echo strftime("%Y/%b/%d", $startDate);
Result
2015/Kov/14
More information about strftime on php.net
Asume, that lt_LT.UTF-8 is missing on your system, as it's working very well on my system. When using unknown locales, it will print in english too.
When using Debian, try:
dpkg-reconfigure locales

How can I change mysql date format language [duplicate]

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().

What format of a timestamp is this?

I have to make some changes on an wordpress plugin.
The plugin gives back this timestamp format: 1364495828.1500
What's the name of this format and how can I change it to DD/MM/YYYY - HH:MM???
I am using PHP
Thanks!
This is a UNIX timestamp to a resolution of 100 microseconds.
Try this to get it into a DATETIME format in your SQL statement.
FROM_UNIXTIME(1364495828.1500)
You can also give a second parameter to the function containing a DATE_FORMAT string, and get the date string you want. For example, this gets you a timestamp string like 16/06/2013 14:25
FROM_UNIXTIME(1364495828.1500, '%d/%m/%Y - %H:%i')
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
But, if you're working in WordPress, you may prefer to do this in PHP. If you use PHP code like this you'll use the WordPress date format and time format settings chosen by the user, and get the the kind of result your user expects.
$timestring = date_i18n( get_option( 'date_format' ), '1364495828.1500' ) .
' ' .
date_i18n( get_option( 'time_format' ), '1364495828.1500' );
These are chosen on the general settings page in WordPress's administrative interface.
It's just the result of:
$ts = microtime(true); // 1368731778.7964 as I write this
a standard UNIX timestamp with microseconds included.
try this(didn't test it):
date('d-m-Y - H:i',strtotime('1364495828.1500'));
this code tries to find out time it is en set it to a data and the date function format it for you

Change date helper language Codeigniter

I'm using Codeigniter for display dates.
I have this but it's echoed in english, how do I set it to other language? I already have the spanish language pack, but I can't figure it out how to load it.
$this->load->helper('date');
echo mdate("%F %d, %Y", strtotime(now()));
Thanks
// Set locale to Spanish Argentina, change to your installed language
setlocale(LC_TIME, 'es_AR');
echo strftime("%B %d, %Y", time());
First you do not need to use the date helper to accomplish this. mdate() is essentially the same as strftime() where just the vars are slightly different. Also your code strtotime(now()) is wrong. now() is the exact same as time() as indicated in the CI documentation. It returns a UNIX timestamp, strtotime() converts a string to a UNIX timestamp. So what you were doing was trying to convert a timestamp to a timestamp, which of course is wrong. I changed the code so it doesn't require the helper and correctly will output MONTHNAME DATE, YEAR in the specified language. Make sure you change the es_AR to whatever Spanish language you installed. It will be the in the format es_COUNTRYCODE
The traductions dates in "calendar_lang.php"
$this->config->set_item('language', 'spanish');
$this->load->library('calendar');
echo 'Mes:'.$this->calendar->get_month_name(date('m'));
echo 'Día:'.$this->calendar->get_day_names(date('d'));

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