Search code examples
phpsetlocale

How to solve a php strtotime language issue not sincronyzed with server bash language


I have a server which is running a German local language for Bash. When I try to change maillog time in this server in Unix timestamp using ..

$time1="Mai-05-20 17:22:36";
$unix_time=strtotime($time1);

$unix_time returns empty, I think because bash is using German but PHP is running english (?).

How can I set the php script to run in the same language of Bash local language ?


Solution

  • From docs (emphasis mine):

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

    In case you don't need the full power of strtotime() (after all, you seem to be parsing logs from one single program) you can try IntlDateFormatter::parse(). Here's a quick and dirty demo:

    $fmt = new IntlDateFormatter('de_DE', null, null);
    $fmt->setPattern('M-dd-yy hh:mm:ss');
    $log_time = "Mai-05-20 17:22:36";
    $unix_time = $fmt->parse($log_time);
    echo date('r', $unix_time);
    

    Tue, 05 May 2020 17:22:36 +0200

    Note that a Unix time is a fixed moment in time, thus unaffected by time zones. I get +0200 when casting to local time because my PHP default time zone is currently CEST.