phpdatephp-7mktime

how to use mktime with "I" (summertime option)?


I wanted to code a script which returns true if the given date is in the summertime. So I found date("stuff" mktime) in the php documentation. And there is a list of parameter strings. There it says: "I (capital i) Whether or not the date is in daylight saving time". Some lines below I found this example:

echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

That's why I wrote this in my script:

function summertime(string $date) : bool {
    $pieces = explode('-', $date);
    $year = (int) $pieces[0];
    $month = (int) $pieces[1];
    $day = (int) $pieces[2];

    return date("I", mktime(0, 0, 0, $month, $day, $year));
}

echo summertime("1981-07-07");

No matter which date I put in, it returns always false/0 what ever... I can't find the difference or mistake... FYI: I am using PHP 7.0.


Solution

  • As noted in Markus Laire's comment, you probably need to set your timezone correctly. I'd also recommend using the DateTime class. The examples below show how to set a specific timezone when creating a new date:

    Example 1:

    $tz = new DateTimeZone('Europe/Berlin');
    $date = new DateTime('1981-07-07', $tz);
    echo $date->format('I');
    // outputs '1'
    

    Example 2:

    $tz = new DateTimeZone('Europe/Berlin');
    $date = new DateTime('1981-01-01', $tz);
    echo $date->format('I');
    // outputs '0'