1

I have a question about my code below. How can I format a German Date, for example "Di, 02.Okt 2012" to "2012-10-02"?

I already checked many sites for a solution but did not found something which could help me.

    $value ='Di, 02.Okt 2012';
    $tempdate = $value;
    $tempdate = substr($tempdate,-11);
    $tempdate = date('Y-m-d',$tempdate);

Output: 1970-01-01

Solved:

$value = 'comes from a foreach loop'; 

$tempdate = $value;
    $tempdate = substr($tempdate,-11);
    $tempdate = str_replace('.Jan ', '-01-', $tempdate);
    $tempdate = str_replace('.Feb ', '-02-', $tempdate);
    $tempdate = str_replace('.Mär ', '-03-', $tempdate);
    $tempdate = str_replace('.Apr ', '-04-', $tempdate);
    $tempdate = str_replace('.Mai ', '-05-', $tempdate);
    $tempdate = str_replace('.Jun ', '-06-', $tempdate);
    $tempdate = str_replace('.Jul ', '-07-', $tempdate);
    $tempdate = str_replace('.Aug ', '-08-', $tempdate);
    $tempdate = str_replace('.Sep ', '-09-', $tempdate);
    $tempdate = str_replace('.Okt ', '-10-', $tempdate);
    $tempdate = str_replace('.Nov ', '-11-', $tempdate);
    $tempdate = str_replace('.Dez ', '-12-', $tempdate);
    $tempdate = date('Y-m-d',strtotime($tempdate));

    echo $tempdate.'<br/>';

This is the solution for many Dates. ^^ i hope i can help with it

3 Answers3

5

Passing the $date_string variable containing German days and months in it to a function that uses strtr (string translate) along with an array of German days (and Months) will allow strtotime (string to unix timestamp) to properly parse:

any English textual datetime description

Because strtotime (string to unix timestamp) only parses English, manual intervention to translate those strings is necessary without Internationalization.

function germanStrtotime($date_string) {
  $germanDays = array('montag'=>'monday', 'mo'=>'monday', 'dienstag'=>'tuesday', 'di'=>'tuesday', 'mittwoch'=>'wednesday', 'mi'=>'wednesday', 'donnerstag'=>'thursday', 'do'=>'thursday', 'freitag'=>'friday', 'fr'=>'friday', 'samstag'=>'saturday', 'sa'=>'saturday', 'sonntag'=>'sunday', 'so'=>'sunday');
  $germanMonths = array('jnuar'=>'january', 'jan'=>'january', 'jän'=>'january', 'februar'=>'february', 'feb'=>'february', 'marz'=>'march', 'märz'=>'march', 'mai'=>'may', 'juni'=>'june', 'oktober'=>'october', 'okt'=>'october', 'sept'=>'september', 'dezember'=>'december', 'dez'=>'december');
  $str = strtr(strtolower($date_string), $germanMonths);
  $str = strtr(strtolower($str), $germanDays);
  return strtotime($str);
}

$value ='Di, 02.Okt 2012';
$tempdate = $value;
$tempdate = germanStrtotime($tempdate);
$tempdate = date('Y-m-d', $tempdate);

die('Output: '.$tempdate); // 2012-10-02
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
1

For internationalization tasks, I'd strongly recommend using the PHP intl extension. It contains several classes for common internationalization tasks such as date/time formatting, number formatting, string transliteration and more. Specifically, the IntlDateFormatter class is able to format (and parse) a datetime for any available locale.

Rahul
  • 18,271
  • 7
  • 41
  • 60
1

This should work for your example.
When u have another date, replace "Okt" with another month and "-10-" with the number of the month.

$tempdate = 'Di, 02.Okt 2012';
$tempdate = str_replace('Di,', '', $tempdate);
$tempdate = str_replace('.Okt ', '-10-', $tempdate);
$tempdate = date('Y-m-d', strtotime($tempdate));
echo $tempdate;

// output: "2012-10-02"
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55