Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the language for date in php

Tags:

date

php

I have this MySQL query, which returns two dates (which are both formatted as a-m-Y). Now I want to translate this date into my own language (Danish). How can I do that. I have tried both the setlocale() and strftime() functions, but it won't work. I know it's a very basic question, but i really need help :) Thanks a lot!

like image 917
Emil Avatar asked Aug 02 '11 10:08

Emil


People also ask

How convert date from yyyy-mm-dd to dd-mm-yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.

What does date () do in PHP?

Specifies the format of the outputted date string. The following characters can be used: d - The day of the month (from 01 to 31)

What is default format of date in PHP?

c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00) r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200) U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)


5 Answers

I found that setlocale isn't reliable, as it is set per process, not per thread (the manual mentions this). This means other running scripts can change the locale at any time. A solution is using IntlDateFormatter from the intl php extension.

  • Install intl if necesarry (ubuntu): sudo apt-get install php5-intl

  • Install the locale you want to use (I'm using italian as an example): sudo locale-gen it_IT

  • Generate a locally formatted date:

$fmt = new \IntlDateFormatter('it_IT', NULL, NULL);
$fmt->setPattern('d MMMM yyyy HH:mm'); 
// See: http://userguide.icu-project.org/formatparse/datetime for pattern syntax
echo $fmt->format(new \DateTime()); 
// Output: 6 gennaio 2016 12:10
like image 84
Simon Epskamp Avatar answered Sep 27 '22 22:09

Simon Epskamp


Use

http://php.net/manual/en/function.strftime.php

<?php
setlocale(LC_ALL, 'da_DA');

echo strftime("%A %e %B %Y");
?>
like image 24
Mudaser Ali Avatar answered Sep 28 '22 22:09

Mudaser Ali


Use setlocale and strftime together:

setlocale(LC_TIME, array('da_DA.UTF-8','da_DA@euro','da_DA','danish'));
echo strftime("%A"); // outputs 'tirsdag'

Works on my php installation on Windows.

strftime(): Warning! This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged.

like image 35
Sander Bakkum Avatar answered Sep 27 '22 22:09

Sander Bakkum


I don't think the date() function is quite evolved enough for you, here.

Instead, I would recommend you take a look at the IntlDateFormatter1 class (quoting) :

Date Formatter is a concrete class that enables locale-dependent formatting/parsing of dates using pattern strings and/or canned patterns.

There are a couple of examples on the manual page of IntlDateFormatter::format(), where that method is used to display a date in two different languages, by just setting the desired locale.


1.bundled with PHP >= 5.3

like image 20
Pascal MARTIN Avatar answered Oct 01 '22 22:10

Pascal MARTIN


This Will Surely works for you if you want norwegian date and month format

$date = '2016-11-16 05:35:14';
setlocale(LC_TIME, array('nb_NO.UTF-8','nb_NO@norw','nb_NO','norwegian'));
echo strftime("%e %b %Y",strtotime($date));

if you want to get other language locale ids like nb_NO then refer this site International Components for Unicode (ICU) Data

like image 33
Dhruv Avatar answered Sep 28 '22 22:09

Dhruv