I have this date string in Dutch for example:

wo, 14 mei 2014 09:42:16

Is it possible to convert it to a timestamp? I was thinking about using the dutch language in mktime() but that returns FALSE ofcourse.

有帮助吗?

解决方案

I would imagine http://php.net/manual/en/function.strptime.php or http://php.net/manual/en/function.date-parse-from-format.php (for php > 5.3) combined with setlocale would solve the problem.

其他提示

You could use regular expressions to search for your data. For exemple :

$day = preg_replace('/^[a-zA-Z], (\d{1,2}) .$/', '$1', $date);
$year = preg_replace('/^[a-zA-Z]+, \d{1,2} [a-zA-Z]+ (\d{4}) .$/', '$1', $date);
$hour = preg_replace('/^[a-zA-Z]+, \d{1,2} [a-zA-Z]+ \d{4} (\d{2}):.$/', '$1', $date);
$minutes = preg_replace('/^[a-zA-Z]+, \d{1,2} [a-zA-Z]+ \d{4} \d{2}:(\d{2}):.$/', '$1', $date);
$seconds = preg_replace('/^[a-zA-Z]+, \d{1,2} [a-zA-Z]+ \d{4} \d{2}:\d{2}:(\d{2})$/', '$1', $date);

For the month you can retrieve the month name and then use a lookup table.

To play with regular expressions : Debuggex

Edit:

Here you can get all your data in one line :

$date = json_decode(preg_replace(
    '/^([a-zA-Z]+), (\d{1,2}) ([a-zA-Z]+) (\d{4}) (\d{2}):(\d{2}):(\d{2})$/',
    '{"day":"$2","month":"$3","year":"$4","hour":"$5","minutes":"$6","seconds":"$7"}',
    $text_date);

Here it is an example : http://sandbox.onlinephpfunctions.com/code/2e976e3813945877a8d096f42d08910e4ec67a30

Using the sources that Mikk3lRo recommended I came to this solution:

    setlocale(LC_TIME, 'nl_NL');
    $format = '%a, %d %B %Y %H:%M:%S';
    $strf = 'wo, 14 mei 2014 09:42:16';

    $a = strptime($strf, $format);
    $timestamp = mktime($a['tm_hour']+2, $a['tm_min'], $a['tm_sec'], $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
    echo $timestamp;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top