Ty Yt Ty Yt - 1 month ago 10
PHP Question

Translate and sort French date php

I have an array with dates to convert into date and sort them.
Here is the string extracted from the array : Samedi 14 janvier 2017 - 13h45

I've started to delete the hour :

$only_date = strstr(strtolower($date),' - ', true);


The result is : Samedi 14 janvier 2017

Then I tried to convert it :

strftime(" %A %B %Y",strtotime($only_date));


But it doesn't work, I obtain 01-01-1970.

How can I handle this?

Thanks.

EDIT :

I removed the week day :

$days = array('lundi','mardi','mercredi','jeudi','vendredi','samedi','dimanche');
$only_date = str_replace($days,"",$date_string);
echo strftime(" %A %B %Y",strtotime($only_date));


And I obtain : jeudi Janvier 1970 (Thursday January 1970)

Answer

Parsing the string and converting it to date would solve the problem. Here is a function which might help you.

<?php
date_default_timezone_set('UTC');

function convert_date($date) {
  $date_arr = explode(" ",$date);
  $time = explode("h",$date_arr[5]);
  $months = array("janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre");
  return date('c',mktime($time[0],$time[1],0,array_search($date_arr[2], $months)+1,$date_arr[1],$date_arr[3]));
}

$date = "Samedi 14 janvier 2017 - 13h45";
echo convert_date($date);
?>

Here is a piece of code to sort the date array.

$date_arr[0] = convert_date("Samedi 14 janvier 2017 - 13h45");
$date_arr[1] = convert_date("Samedi 12 mars 2017 - 13h45");
$date_arr[2] = convert_date("Samedi 18 juin 2013 - 13h45");

print_r($date_arr);
arsort($date_arr);
echo "After sorting";
print_r($date_arr);