azuyalabs / yasumi

The easy PHP Library for calculating holidays
https://www.yasumi.dev
Other
1.05k stars 155 forks source link

PHP / solution HELP #247

Closed pete1019 closed 3 years ago

pete1019 commented 3 years ago

Hi @stelgenhof,

again: thanks a lot for your great work. need your help on PHP code please. I am not good at all but would love to see your solution. As much detail as possible so i understand.

THANKS A LOT!

stelgenhof commented 3 years ago

Hi @pete1019 Sure! For starters to remove holidays from an existing provider, you can use a custom filter. Or more advanced you could create your own custom provider based on an existing one: https://www.yasumi.dev/docs/cookbook/custom_provider/

For the current date, you can use the standard PHP date functions for that: https://www.php.net/manual/en/function.date.php

You can use the isHoliday method to check if the current day is a holiday or not.

Hope this helps!

pete1019 commented 3 years ago

@stelgenhof

Thanks. Thought you might be able to post some code here for me to use right away.

To strip it down a little bit this might be enough for now: Print the name like "christmasDay" of the current day.

Again: Thanks lot!

EDIT: GOT IT

if (isset($_GET['c']) && isset($_GET['t'])) {

    $tz = $_GET['t'];

    if ($tz === 'Default Timezone') {
        $tz = `date +%:z`;
    }

    $dt = new \DateTime('now', new \DateTimeZone($tz));
    $c = str_replace('-', "\\", $_GET['c']);
    $c = str_replace('-', "//", $c);
    $c = str_replace('-', "/", $c);
    // $is_holiday = Yasumi\Yasumi::create($c, date('Y'))->isHoliday($dt);

    $d = $dt->format('Y-m-d');
    #$d = "2021-04-05";
    $ho = Yasumi\Yasumi::create($c, date('Y'));
    #$official = new Yasumi\Filters\OtherHolidaysFilter($ho->getIterator());

    foreach ($ho as $h) {
        $shortName = $h->shortName;
        if (strval($d) === strval($h))
            die($shortName);    
    }
    die("notholiday");
}
stelgenhof commented 3 years ago

@pete1019 Good! A few tips/pointers:

stelgenhof commented 3 years ago

Closing, as I assume your question has been answered :)

pete1019 commented 3 years ago

@stelgenhof yes thanks again!