0

I have a string representing a date: 21 marzo 2017. I want to convert it to a Unix timestamp.

  • echo strtotime('21 march 2017'); works because the month is in English.
  • echo strtotime('21 maggio 2017'); doesn't work because the month is in Italian.

How can I get a Unix timestamp from that Italian string?

user4157124
  • 2,809
  • 13
  • 27
  • 42
andrea
  • 396
  • 2
  • 12

2 Answers2

1

From the PHP docs on strtotime: http://php.net/strtotime

strtotime — Parse about any English textual datetime description into a Unix timestamp

So the only way to achieve what you're asking is to translate your month to English. How you achieve that will depend on the context of your application.

If you only need to support one language you might be able to create a simple string replacement system for Italian to English months.

If you need a more robust translation option you might want to look at something like the Google Cloud Translation API: https://cloud.google.com/translate/docs/

Mike S
  • 1,636
  • 7
  • 11
  • 1
    Yes, I knew that strtotime works only in English. But I wanted to know if there is any php function for the dates that solves my problem, without making a literal replacement of the month's name. – andrea Sep 08 '17 at 22:13
  • 1
    PHP does not have any built in translation services. – Mike S Sep 08 '17 at 22:14
0

You should first convert your months in english with str_replace:

str_replace('marzo','march',"21 marzo 2017");

Then you can use echo strtotime('21 march 2017');

If you have to convert other date, consider using a function and a switch-case.

sclero
  • 173
  • 1
  • 1
  • 8