How to get customer group collection in Magento 2?

In Magento 2 We can easily get customer group programmatically by using below code snippets. You are aware of How to create customer and new address programmatically in Magento 2

Below code snippets display all the customer group in the system,
Call groupCollectionFactory using __construct() in block file,

<?php
namespace Rbj\CustomerGroup\Block;

class CustomerGroup extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory,
        array $data = []
    ) {
        $this->groupCollectionFactory = $groupCollectionFactory;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve customer group collection
     *
     * @return GroupCollection
     */
    public function getCustomerGroupCollection()
    {
        if (!$this->hasData('customer_group_collection')) {
            $collection = $this->groupCollectionFactory->create();
            $this->setData('customer_group_collection', $collection);
        }

        return $this->getData('customer_group_collection');
    }

Call from template file,

<?php
$customerGroupCollection = $block->getCustomerGroupCollection();
foreach($customerGroupCollection as $customerGroup) {
	echo 'ID '.$customerGroup->getId();
	echo 'Code '.$customerGroup->getCode();
	echo 'Tax class Id '.$customerGroup->getTaxClassId();
	echo "<br>";
}

The output will be look like,

ID 0 Code NOT LOGGED IN Tax class Id 3
ID 1 Code General Tax class Id 3
ID 2 Code Wholesale Tax class Id 3
ID 3 Code Retailer Tax class Id 3

How to get Current GMT date time in Magento 2?

How to retrieve GMT Date Time from Magento 2?

Magento native supports DateTime PHP class, Magento\Framework\Stdlib\DateTime\DateTime is used for forms GMT date.

Magento provides GMT timezone by using the given function in the code snippet.

<?php
public function __construct(\Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateTimeFactory)
{
    $this->dateTimeFactory = $dateTimeFactory;
}

Call the above function at the required place and your work is done,

$dateModel = $this->dateTimeFactory->create();
echo $dateModel->gmtDate();

First Parameter is optional $format = null in gmtDate()
The default Format is Y-m-d H:i:s, You can add your time zone to convert the normal timezone to GMT Timezone.

Result: 2023-04-03 11:22:49

How to add Index to table column using Magento 2?

You can create your column as index key for better performance and speedy retrieval of data from a table.

An index is a performance optimization feature that enables data to be accessed faster compare to other nonindex columns. An index is used to more quickly find rows in the table based on the values which are part of the index and they don’t define the uniqueness of a column.

You can add your column as Index key using Magento 2 by below way using InstallSchema or UpgradeSchema.php file,

->addIndex(
    $installer->getIdxName('table_name', ['table_field']),
    ['table_field']
)

Here table_name is your table name of database and table_field is your field which you want to create index key.
You can check blog for Add foreign key to table, Add Foreign key to table using Magento 2