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'));
Related
I configured my timezone to Europe/Paris in php.ini. When executing date_default_timezone_get() I do get the correct value.
Then, I expect strftime('%x', date()) to output something like 16 novembre 2018 which is the French format. But instead, I get 11/16/2018 which looks like the US format.
Any idea why?
The time zone has no effect on how dates and times are presented, for that you need to set the locale. There are no standards for locale names, but fortunately PHP's setlocale() function will take multiple locale names, stopping at the first successful one.
// just a few common name formats
setlocale(LC_TIME, ["fr_FR.utf8", "fr_FR#euro", "fr_UTF8", "fr_FR", "french"]);
echo strftime("%d %B %Y", time());
I tried:
setlocale(LC_ALL, 'fr_utf8');
echo strftime("%d %B %Y", time());
and got:
16 novembre 2018
after setting the timezone to Europe/Paris, Use echo date('d-M-Y') and it will display your desired time. it worked for me
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'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.
Hi pretty much what it says on the tin.
I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.
I've tried:
<?php
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring,$row->created);
?>
But I'm getting an error:
Message: A non well formed numeric value encountered
Any help most appreciated!
Cheers,
Billy
Try:
echo date ("d/m/Y h:ia",strtotime($row->created));
The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));
The only difference between mdate() and date() is, as the CodeIgniter docs say:
This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.
The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.
Got this to work using treeface's solution, with one minor change:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created));
//strtoDATE didn't work but strtoTIME did
Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.
HTH
I'm using:
mdate(date_string,mysql_to_unix($row->created))
That should work.