Thursday, 1 February 2024

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody.

Please find below the complete code and modify it as per your need. 

Once the .ics file is generated, you can open it in Outlook and Save it.

<?php

class ICS {
    var $data;
    var $name;
    function ICS($start,$end,$name,$description,$location) {
        $this->name = $name;
        $this->data = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT
        \nDTSTART:".date("Ymd\THis\Z",strtotime($start)).
        "\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."
        \nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:"
        .date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".
        $description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M
        \nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR
        \n";
    }
    function save() {
        file_put_contents($this->name.".ics",$this->data);
    }
    function show() {
        header("Content-type:text/calendar");
        header('Content-Disposition: attachment;
        filename="'.$this->name.'.ics"');
        Header('Content-Length: '.strlen($this->data));
        Header('Connection: close');
        echo $this->data;
    }
}

$event = new ICS("2024-02-06 09:00","2024-02-06 21:00",
"Test Event", "This is an event made by Test User","Test Location");
$event->save();
$event->show();

Wednesday, 31 January 2024

Replace, Substring and Words Count in MySQL

Some useful string functions in MySQL.

If you want to replace a string in a given string.

SELECT name, REPLACE(name, 'Demo', 'Test') AS `replaced_string` FROM `table_names`;


If you want to get the first word in a given string.

SELECT name, SUBSTRING_INDEX(name, ' ', 1) AS first_word FROM `table_names`;


If you want to get the count of words in a string.

SELECT name, (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) AS word_count  FROM `table_names`;










Tuesday, 30 January 2024

Kafka

KAFKA is an open source developed by LinkedIn for data pipelining, data streaming, data processing, and data transferring with maximum throughput. It's managed by Apache.

We use Kafka on the AWS environment under Python and PHP technologies to manage a high volume of data to load in a fraction of a second.

Kafka helps in real-time data streaming, connecting to any sources like AWS S3, can be auto-scale and optimize data streams. Many big giants using Kafka.



Saturday, 11 June 2022

Laravel Trottle AddHeaders Error

Laravel Trottle AddHeaders Error

We sometimes face this error when new install Laravel and run composer udpate

 Type error: Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders()

must be an instance of Symfony\\Component\\HttpFoundation\\Response, null given


Solution:

For me, the issue is resolved by correcting database credentials in the .env file. Hope the same will work for

you as well.

Saturday, 12 June 2021

Test Web Apps on all browsers online

Test Web Apps on different browsers online


Browserstack is the best solution to test website UI/UX on different browsers simulators at single place online.



browserstack

Click here for more info:




I used this https://www.browserstack.com/ for my apps to test UI/UX and performance of my apps. We can get following browsers at one place:
  1. Google Chrome - All Versions
  2. IE - All Versions 
  3. Safari - All Versions
  4. Android - All Versions
  5. IOS - All devices, All Versions
  6. Opera browsers


Tuesday, 8 June 2021

Microsoft Graph API PHP

Microsoft Graph API PHP Tutorials

I am sharing some basic sample PHP code for Microsoft Graph API. May this will  helpful for someone.

For full reference, go to GIT URL:

PHP Framework Laravel Sample Code GIT URL:





















To start, you should have Outlook/Hotmail or any Microsoft Account.

First required to register App on Azure Active Directory. You can create App on Azure even if you have School or Organization account.

Graph API will generate Microsoft Authentication URL, where user will redirect and after successful login he will redirect back callback URL. Callback URL need to define in Azure App.

User can get Calendar events based on dates provided. User can create events on their Calendar.

Sharing Create Calendar Event using Microsoft Graph API.

<?php
// POST /me/events
$events_response = $graph->createRequest('POST', '/me/events')
    ->attachBody($newEvent)
    ->setReturnType(Model\Event::class)
    ->execute();


Get Calendar Events using Microsoft Graph API

<?php

$userEventsArr = $graph->createRequest('GET', $getEventsUrl)
// Add the user's timezone to the Prefer header
    ->addHeaders(array(
        'Prefer' => 'outlook.timezone="' . $userTimezone . '"',
    ))
    ->setReturnType(Model\Event::class)
    ->execute();

Sunday, 6 June 2021

Calculate Number of Days PHP

Calculate Number of Working Days Between Two Days in PHP

I am sharing my PHP code to get difference between two dates.

$workingDays = 0;

$startTimestamp = strtotime('2021-06-03');
$endTimestamp = strtotime('2022-07-09');
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
    if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
}

echo "no. of days: " . $workingDays;

Save code in nofdays.php.

Run program in terminal. Output will be:

$ php nofdays.php

no. of days: 287




Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...