Question

Hi @ll and greetings from Germany,

Perhaps I am too blind to see, but I am struggling with a localisation problem. I hope that someone has a solution for that.

In a function I have a german date string, assume it's today:

$datestring = '3. März 2014'

Second, i have the date format used in WP

$date_format = get_option( 'date_format' );

What I am trying to achive is to get a valid unix timestamp from $datestring.

I tried several different approaches like strtotime, date_parse_from_format, etc., I even tried to set the locale environment via setlocale. Of course it would be easy to parse the string against an array with the month names and get an english datestring, but I want to have this ready for all languages.

Any Idea how to get this done? Help is really appreciated.

No correct solution

OTHER TIPS

You need to use mktime($datestring)
and set the $datestring variable in accordance to:
http://pl1.php.net/manual/de/function.mktime.php
in wordpress:

   mktime(get_the_date("H"), get_the_date("i"), get_the_date("s"), get_the_date("n"), get_the_date("j"), get_the_date("Y"));

if you need to convert exact format shown above:

   $datestring = '3. März 2014';
   $dateelements = explode(" ", $datestring);
   $day = rtrim($dateelements[0], ".");
   $germanMonths = array(1 =>  "Januar", 2 => "Februar", 3 => "März", 4 => "April", 5 => "Mai", 6 => "Juni", 7 => "Juli", 8 => "August", 9 => "September", 10 => "Oktober " , 11 => "November", 12 => "Dezember");
   $month = array_search($dateelements[1], $germanMonths);
   $year = $dateelements[2];
   $unixtimestamp = mktime(0,0,0,$month,$day,$year);

and if you have a date format in wp option, and $datestring is in that format:

   $dateformat =  get_option('date_format');
   echo $dateformat;
   $date = date_create_from_format($dateformat, $datestring);
   $new_format = date_format($date, 'm,d,Y');
   $unixtimestamp = mktime(0,0,0,$new_format);
   echo $unixtimestamp;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top