0

Ok, there's tons of questions about this and i try most of the solutions i found there without success.

I have a form passing a date in this format to the PHP function: 26/11/2014

In the function i have to transform it in other forms and this is my code:

$date_1 = date('d F Y', strtotime($_REQUEST['date']));
setlocale (LC_TIME, 'de_DE');
$date_transl = strftime('%d %B %Y', strtotime($_REQUEST['date']));

In both case i'm having returned 01 January 1970 so i'm facing 2 problems:

1) date returned is wrong

2) strftime is not translating the date

Mariano
  • 221
  • 1
  • 4
  • 14
  • 1
    Try doing `mm/dd/yyyy` instead of `dd/mm/yyyy`. – Albzi Nov 26 '14 at 11:06
  • 2
    Or use `dd-mm-yyyy` instead of `dd/mm/yyyy`... the slash indicates that the date should be treated as US format, the dash indicates European format, as described in the [PHP docs](http://php.net/manual/en/datetime.formats.date.php) – Mark Baker Nov 26 '14 at 11:07
  • `there's tons of questions about this` you are correct, and there is also a tons of answers on this, and your problem is the most common one! – Glavić Nov 26 '14 at 11:12
  • possible duplicate of **[Strtotime() doesn't work with dd/mm/YYYY format](http://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format)** – Glavić Nov 26 '14 at 11:13

2 Answers2

2

Try

$date_1 = date('d F Y', strtotime(str_replace('/','-','26/11/2014')));
Samiul Amin Shanto
  • 1,397
  • 9
  • 19
2

Replace the / characters with - and it will do the job:

$_REQUEST['date'] = str_replace('/','-',$_REQUEST['date']);
$date_1 = date('d F Y', strtotime($_REQUEST['date']));
setlocale (LC_TIME, 'de_DE');
$date_transl = strftime('%d %B %Y', strtotime($_REQUEST['date']));
S.Pols
  • 3,414
  • 2
  • 21
  • 42
  • actually, i've noticed the month is not being translated anyway? even in my local language, it will be displayed in english while printing $date_1 (that is not being excplicitly localized) – Mariano Nov 26 '14 at 11:50
  • Yes it will, `$date_transl` should contain the translated month name. If this isn't true, then probably your server doesn't recognize the language `de_DE`. I tried `nl_NL` and that worked properly.\ – S.Pols Nov 26 '14 at 11:54