spatie / calendar-links

Generate add to calendar links for Google, iCal and other calendar systems
https://spatie.be/opensource
MIT License
921 stars 150 forks source link

Wrong date when allDay is set to TRUE in some Generators. #175

Closed koposzek closed 1 year ago

koposzek commented 1 year ago

Context

  1. PHP Version: 7.4
  2. Package version: 1.8.0

Describe the Bug

In some Generators (in Google & Outlook exactly) there is a problem with the wrong start date when we want to create an all day event for this specific case:

$start_date = 15/09/2023 01:00:00 (GMT +2) // Start date which I pass to the Link class.
$end_date = 17/09/2023 22:00:00 (GMT +2)

// Pass it to the Link class, which uses the Google generator. Where we have:

protected $dateFormat = 'Ymd';
protected $dateTimeFormat = 'Ymd\THis\Z';

public function generate(Link $link): string
    {
        $url = 'https://calendar.google.com/calendar/render?action=TEMPLATE';

        $utcStartDateTime = (clone $link->from)->setTimezone(new DateTimeZone('UTC'));
        // Because of this my start date equals now 14/09/2023 23:00:00
        $utcEndDateTime = (clone $link->to)->setTimezone(new DateTimeZone('UTC'));
        // Which then is formatted to Ymd => 14/09/2023
        $dateTimeFormat = $link->allDay ? $this->dateFormat : $this->dateTimeFormat;
        $url .= '&dates='.$utcStartDateTime->format($dateTimeFormat).'/'.$utcEndDateTime->format($dateTimeFormat);

        // Add timezone name if it is specified in both from and to dates and is the same for both
        if (
            $link->from->getTimezone() && $link->to->getTimezone()
            && $link->from->getTimezone()->getName() === $link->to->getTimezone()->getName()
        ) {
            $url .= '&ctz=' . $link->from->getTimezone()->getName();
        }
        (...)
        return $url;
    }

In the Yahoo generator the allDay parameter seems to be taken into account and it works great, why were these generators made different ? Or did I misunderstood the concept and should transform my date before passing it to Link to some specific format ? I have checked the commit history and before this commit it worked fine.

Expected Behavior

It generates the event start from 14/09/2023 and not 15/09/2023.

parisiam commented 1 year ago

I have the same problem.

$title = 'My event';
$tz = 'Europe/Paris';
$dateStart = '2023-07-25';
$start = new DateTime($dateStart, new DateTimezone($tz));
$days = 1;
$link = Link::createAllDay($title, $start, $days);

The result is correct only in Yahoo: it generates an all-day event on 25 July 2023. It is wrong in Google and Outlook: with $link->google(), $link->webOutlook() and $link->webOffice(), it generates an all-day event on 24 July 2023.

If I try and create an event of more than one day, the result is wrong in ALL calendars. For example:

...
$dateStart = '2023-07-25';
$days = 2;
...

It generates the following results: Google and outlook: 2-day event on 24 & 25 July 2023. Yahoo: 3-day event from 25 to 27 July 2023.

If I use the create method instead of the createAllDay method, the problem persists.

alies-dev commented 1 year ago

@parisiam what is your PHP timezone?

spatie-bot commented 1 year ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

alies-dev commented 10 months ago

@koposzek @parisiam it should be probably fixed by https://github.com/spatie/calendar-links/releases/tag/1.8.6