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

How to add Foreign Key in database table using Magento 2?

We can add a foreign key for a column of a table using Magento by the below code snippet.

Add foreign key in a column,
Example, you are creating a module and you want to add Store Filter in your module for that case you need to create a relation with store table.
Using below code snippet you can create a relationship with store table.

Here your custom table name is {my_table_name} and this table must contain field name store_id to a relation with store table.
In InstallSchema or UpgradeSchema File, you need to add below code snippet,

<?php
->addForeignKey(
    $installer->getFkName('my_table_name', 'store_id', 'store', 'store_id'),
    'store_id',
    $installer->getTable('store'), /* main table name */
    'store_id',
    \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)

“my_table_name” is your table name
“store_id” is your table field name to relate with parent table
“store” Main table to create relation with
“store_id” Main table field name
“ACTION_CASCADE”  used for the type of actions which executes on a matched row of the child table when a parent table row gets update or delete action.

/**
     * Actions used for foreign keys
     */
     ACTION_CASCADE = 'CASCADE';

     ACTION_SET_NULL = 'SET NULL';

     ACTION_NO_ACTION = 'NO ACTION';

     ACTION_RESTRICT = 'RESTRICT';

     ACTION_SET_DEFAULT = 'SET DEFAULT';

Using Above code You can add foreign key in your table which contain relation to store table of Magento.

You can refer blog for add Index to column by How to add Index to table column using Magento 2?