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?
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');
Use date_default_timezone_set() :
date_default_timezone_set('Asia/Dubai');
echo "The time is " . date('Y/m/d H:i:s');
Related
i am trying to get the correct time with php
not the one that the user's computer gives is there a function that gives the correct "GMT" time as an example .
PHP gets the server date, not the user computer date.
If you need to get a different date timezone then you need set the timezone to your desired.
See: http://php.net/manual/en/function.date.php
like:
$tz = 'Europe/London';
$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('d.m.Y, H:i:s');
If you just want GMT time, you can use gmdate instead of date. For example,
echo date('Y-m-d H:i:s') . "\n";
echo gmdate('Y-m-d H:i:s') . "\n";
Output (for a server in Europe)
2019-02-04 03:34:05
2019-02-04 02:34:05
Demo on 3v4l.org
You will be getting the server time only, not the computer time
$now = new DateTime(null, new DateTimeZone('America/New_York')); //timezone format
echo $now->format("Y-m-d\TH:i:sO"); // Server time
In php how can I display the current date and time with the timezone offset such as this:
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s");
//
This displays 2019-01-09 19:30:19
but I want it to display 2019-01-09 07:00:00 because I am in Mountain Time Zone
When working with time zones and DateTime() objects you should be using DateTimeZone() instead of date_default_timezone_set()
$now = new \DateTime(null, new DateTimeZone('America/Denver'));
echo $now->format("Y-m-d h:i:s");
Demo
Try to paste second parameter your timezone
$date = new DateTime('NOW', new DateTimeZone('America/Denver));
echo $date->format('Y-m-d h:i:s');
You could define wich timezone is fitting for you by this answer.
List of US Time Zones for PHP to use?
for getting all timezones names in you country. Then you pick your timezone and paste
$date = new DateTime('NOW', new DateTimeZone('Your Country/Your timezone'));
you have right code, but you have want to show time in 12 hours format. then you have use something like that.
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s A");
How can a change times that are provided with a timezone (eg. 14:00:00+02:00) to a local time (the result would be 16:00:00) when the dates are not provided.
I've tried it with the DateTime class. But since I'm only handling times, not dates, it doesn't always provide the correct results:
$time = '14:00:00+02:00';
$datetime = new DateTime($date);
$result = $datetime->format("H:i:s");
Use DateTime's setTimezone method. It will automatically change the time to match the timezone.
$date = new DateTo,e("2017-02-02 10:00:00", new DateTimeZone("+0200"));
$date->setTimezone(new DateTimeZone("-0200"));
var_dump($date->format("H:i:s"));
Will output the following:
string '06:00:00' (length=8)
$utc = new DateTimeZone("UTC");
$zone = new DateTimeZone("YOUR_TIME_ZONE");
$date = new DateTime("14:00:00+02:00", $utc);
$date->setTimezone($zone);
echo $date->format('Y-m-d H:i:s');
You can use setTimezone() function
$time = '14:00:00+02:00';
$datetime = new DateTime($time);
$result = $datetime->format("H:i:s");
echo $result;
echo '<br>';
$datetime->setTimezone(new DateTimeZone('Your_local_timezone')); // ex: UTC (+00h00) or Europe/Moscow (+4h00)
$result = $datetime->format("H:i:s");
echo $result;
You should specific your time zone, and tell DateTime to use Time zones
for example:
$time = '14:00:00+02:00';
$datetime = DateTime::createFromFormat('H:i:s+T', $time);
$datetime->setTimezone(new DateTimeZone('Europe/Warsaw'));
$result = $datetime->format("H:i:s");
echo($result);
Use +T in format pattern to say php thats date contain a Time zone, in next step u should set your timezone using setTimezone. For example i set 'Europe/Warsaw' but nothing happen coz +02:00 is CEST thats equal to 'Europe/Warsaw'
Other way is changing +02:00 to naming time zone here is a example:
$time = '14:00:00 (UTC)';
$datetime = new DateTime($time);
$datetime->setTimezone(new DateTimeZone('Europe/Warsaw'));
$result = $datetime->format("H:i:s");
echo($result);
When I run the following code in codeigniter, it returns incorrect time and date for the India location. Please let me the solution.
'date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s', time());
echo $date;'
try this
$now = new DateTime();
$now->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $now->format('Y-m-d H:i:s');
In my PHP program, I'm using $_SERVER to log the page's date visited:
$dateStamp = $_SERVER['REQUEST_TIME'];
The result is that the $dateStamp variable contains a Unix timestamp like:
1385615749
What's the simplest way to convert it into a human-readable date/time (with year, month, day, hour, minutes, seconds)?
This number is called Unix time. Functions like date() can accept it as the optional second parameter to format it in readable time.
Example:
echo date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
If you omit the second parameter the current value of time() will be used.
echo date('Y-m-d H:i:s');
Your functional approch to convert timestamp into Human Readable format are as following
function convertDateTime($unixTime) {
$dt = new DateTime("#$unixTime");
return $dt->format('Y-m-d H:i:s');
}
$dateVarName = convertDateTime(1385615749);
echo $dateVarName;
Output :-
2013-11-28 05:15:49
Working Demo
<?php
$date = new DateTime();
$dateStamp = $_SERVER['REQUEST_TIME'];
$date->setTimestamp($dateStamp);
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
you can try this
<?php
$date = date_create();
$dateStamp = $_SERVER['REQUEST_TIME'];
date_timestamp_set($date, $dateStamp);
echo date_format($date, 'U = D-M-Y H:i:s') . "\n";
?>
this code will work for you
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d-M-Y H:i:s',strtotime($dateStamp));
REQUEST_TIME - It is unix timestamp - The timestamp of the start of the request.
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d m Y', $dateStamp);
OR
$date = new DateTime($dateStamp);
echo $date->format('Y-m-d');