title: generate ics calendar file with php
body: <p>When I open this calendar.php file, it's source code is a ics (calendar). I've cut out some code for event description, id, etc. to make shorter post. How can I generate such file by opening this php file? I think fopen but could You please help me with that?</p>
  <p>opening calendar.php should generate calendar.ics (with all the data generated by this script)</p>
  <pre><code>    &lt;?php
  $servername = &quot;&quot;;
  $username = &quot;&quot;;
  $password = &quot;&quot;;
  $dbname = &quot;&quot;;
  
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);
  }
  $conn-&gt;set_charset(&quot;utf8&quot;);
  $sql = &quot;SELECT * FROM calendar WHERE id_employee='0100000001'&quot;;
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
  
      echo &quot;BEGIN:VCALENDAR \n&quot;;
      echo &quot;PRODID:-//Google Inc//Google Calendar 70.9054//EN \n&quot;;
      echo &quot;VERSION:2.0 \n&quot;;
      echo &quot;CALSCALE:GREGORIAN \n&quot;;
      echo &quot;METHOD:PUBLISH \n&quot;;
      echo &quot;X-WR-CALNAME:Calendar \n&quot;;
      echo &quot;X-WR-TIMEZONE:Europe/Warsaw \n&quot;;
      
      while($row = $result-&gt;fetch_assoc()) {
  
          $h_start = $row['h_start'];
          $h_end = $row['h_end'];
          $schedule_notes = $row['schedule_notes'];
  
          echo &quot;BEGIN:VEVENT\n&quot;;
          echo &quot;DTSTART:&quot;; //20211227T190000
          echo DateTime::createFromFormat('Y-m-d H:i:s', $h_start)-&gt;format('Ymd');
          echo &quot;T&quot;;
          echo DateTime::createFromFormat('Y-m-d H:i:s', $h_start)-&gt;format('His');
          echo &quot;\n&quot;;
          echo &quot;DTEND:&quot;; //20211227T190000
          echo DateTime::createFromFormat('Y-m-d H:i:s', $h_end)-&gt;format('Ymd');
          echo &quot;T&quot;;
          echo DateTime::createFromFormat('Y-m-d H:i:s', $h_end)-&gt;format('His');
          echo &quot;\n&quot;;
      
          echo &quot;END:VEVENT\n&quot;;
      }
      
      echo &quot;END:VCALENDAR\n&quot;;
      
  } else {
      echo &quot;no data&quot;;
  }
  $conn-&gt;close();
  </code></pre>