Set weekday to german in PHP

3.6k views Asked by At

I've go these simple MySQL Query:

$sql->setQuery("SELECT * FROM $db_table");

for ($i = 0; $i < $sql->getRows(); $i++) {
    $id   = $sql->getValue("id");
    $date = $sql->getValue("date");

    setlocale(LC_TIME, 'de_DE');

    $date =  date('l, d.m.Y');

    echo $date;

    $sql->next();
}

The Output is: Wednesday, 22.12.2016.

Is there a way to get the weekday in german? I'm still using setlocale(LC_TIME, 'de_DE');?!

2

There are 2 answers

0
DCR On

setlocale(LC_TIME, 'de_DE', 'deu_deu');

4
Thamilhan On

It might be due to windows machine, locale de_DE is not detected. Instead use deu_deu. Also, use strftime that formats a local time/date according to locale settings:

setlocale(LC_TIME, 'de_DE', 'deu_deu');
$date =  strftime('%A, %d.%m.%Y');
echo $date;

This prints:

Dienstag, 20.12.2016

You can see in the manual for reference


Update: To display date from DB

$date =  strftime('%A, %d.%m.%Y', strtotime($date));

So your code will be:

$sql->setQuery("SELECT * FROM $db_table");

for($i=0;$i<$sql->getRows();$i++)
{

$id = $sql->getValue("id");
$date = $sql->getValue("date");

setlocale(LC_TIME, 'de_DE', 'deu_deu');
$date =  strftime('%A, %d.%m.%Y', strtotime($date));
echo $date;

$sql->next();
}