Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current time of a UAE in PHP

Tags:

php

datetime

yii

I am using yii framework

I am using below to code to get time

<?php echo date('Y/m/d H:i:s') ?>

how can i current time of UAE?

like image 305
Sona Rijesh Avatar asked Dec 29 '15 11:12

Sona Rijesh


People also ask

What is the time zone code for UAE?

United Arab Emirates Standard Time or UAE Standard Time is the time zone for the UAE. It is given by Gulf Standard Time, being 4 hours ahead of GMT/UTC (UTC+04:00) and is co-linear with neighbouring Oman.

What is UTC timezone PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. To change the PHP timezone for an app, create a .user.ini file in the app's public directory with the following contents: date.timezone = America/Los_Angeles.

How can we convert the time zones using PHP?

It's really simple to convert a DateTime from one time zone to another in PHP. Just create a DateTime object using date & time to be converted as the first parameter and the original time zone as the second parameter. Then change the time zone to the desired one using the setTimezone method. That's all!


2 Answers

Try this:

$tz = 'Asia/Dubai'; // your required location time zone.
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('Y/m/d H:i:s');
like image 55
Mohammad Avatar answered Sep 19 '22 22:09

Mohammad


Use date_default_timezone_set() :

date_default_timezone_set('Asia/Dubai');
echo "The time is " . date('Y/m/d H:i:s');
like image 33
Insane Skull Avatar answered Sep 20 '22 22:09

Insane Skull