Im struggling to find a solution about this,
is there anyway I can use a php or js/jquery
function/plugin to get the local time of a city
or state based on it's timezone code?
For example lets say Rome is timezone 2
Thank you alot.
Please look at the gmstrftime():
<?php
$offset = 2;
$timestamp = time() + ( $offset * 60 * 60 );
echo gmstrftime("%b %d %Y %H:%M:%S", $timestamp) . "\n";
?>
This function formats time against GMT, so you can directly use time zone offsets.
PHP Manual here - http://php.net/manual/en/function.gmstrftime.php
But a more elegant solution could be to use DateTime::setTimezone():
<?php
// From PHP manual
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
I would suggest to look into DateTime class and supported timezones in php.
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
I'm currently storing times using the 'time()' function in the database. However, it's using the timezone of the server, and I'd like for each user to see the time according to their timezone (set in their profile).
How do I do the timestamp conversion? (and I mean from timestamp to timestamp, not to readable time)
As Joonas said, UNIX timestamps are by definition UTC, but you could hack something like this together to mimic timezone-specific timestamps if you really need to:
// PHP 5.3 - OO Code
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp;
$dt = DateTime::createFromFormat('U', $timestamp);
$dt->setTimeZone(new DateTimeZone('America/New_York'));
$adjusted_timestamp = $dt->format('U') + $dt->getOffset();
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
// PHP 5.3 - Procedural Code
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp;
$dt = date_create_from_format('U', $timestamp);
date_timezone_set($dt, new DateTimeZone('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
Really you should not be hacking the timestamps themselves to change the date within, you should just be applying the timezone to the timestamp before presenting the formatted datestamp to the user.
This is a modified version of Mike's code that should work for PHP 5 >= 5.2.0
as found on php.net
// OO Code
$st = 1170288000 // a timestamp
$dt = new DateTime("#$st");
$dt->setTimeZone(new DateTimeZone('America/New_York'));
$adjusted_timestamp = $dt->format('U') + $dt->getOffset();
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
// Procedural Code
$st = 1170288000 // a timestamp
$dt = date_create("#$st");
date_timezone_set($dt, timezone_open('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
UNIX timestamps are by definition in UTC, which means that all conversion should be done just prior to printing out rather than with actual timestamps.
How to do this however depends on how you're formatting them currently. I believe PHP has built-in timezone handling.
I have a module in zend framework and I want to execute it as per database date time value difference. You can think that I want to create a birth day reminder which will display a message to my users on his birth day. How can i create it in zend framework? any suggestion or any idea is mostly welcome.
First you have to get the date time and the time zone difference from the database (probably using a model).
Then you can use gmstrftime() if the time zone difference is in numeric representation (eg. +1, +2 or -1, -2 etc) :
<?php
$offset = 2;
$timestamp = time() + ( $offset * 60 * 60 );
echo gmstrftime("%b %d %Y %H:%M:%S", $timestamp) . "\n";
?>
PHP Manual here - http://php.net/manual/en/function.gmstrftime.php
But a more elegant solution could be to use DateTime::setTimezone():
<?php
// From PHP manual
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.
ASKT
CDT
EDT
HST
MDT
MST
PDT
How can i convert? Please help
DateTime::setTimezone would help you.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Use the DateTime Class
$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display date
$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again
This, way you don't have to give the same timestamp as new argument every time like mishu's answer.
See the class written by "the_dark_lord12001 at yahoo dot com" this will help you to get timezones from abbreviations & then you can use it
with either date_default_timezone_set or DateTime class
http://www.php.net/manual/en/datetimezone.listabbreviations.php
Hope this help.
~K
I'm currently storing times using the 'time()' function in the database. However, it's using the timezone of the server, and I'd like for each user to see the time according to their timezone (set in their profile).
How do I do the timestamp conversion? (and I mean from timestamp to timestamp, not to readable time)
As Joonas said, UNIX timestamps are by definition UTC, but you could hack something like this together to mimic timezone-specific timestamps if you really need to:
// PHP 5.3 - OO Code
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp;
$dt = DateTime::createFromFormat('U', $timestamp);
$dt->setTimeZone(new DateTimeZone('America/New_York'));
$adjusted_timestamp = $dt->format('U') + $dt->getOffset();
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
// PHP 5.3 - Procedural Code
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp;
$dt = date_create_from_format('U', $timestamp);
date_timezone_set($dt, new DateTimeZone('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
Really you should not be hacking the timestamps themselves to change the date within, you should just be applying the timezone to the timestamp before presenting the formatted datestamp to the user.
This is a modified version of Mike's code that should work for PHP 5 >= 5.2.0
as found on php.net
// OO Code
$st = 1170288000 // a timestamp
$dt = new DateTime("#$st");
$dt->setTimeZone(new DateTimeZone('America/New_York'));
$adjusted_timestamp = $dt->format('U') + $dt->getOffset();
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
// Procedural Code
$st = 1170288000 // a timestamp
$dt = date_create("#$st");
date_timezone_set($dt, timezone_open('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo ' Timestamp adjusted for America/New_York: ' . $adjusted_timestamp;
UNIX timestamps are by definition in UTC, which means that all conversion should be done just prior to printing out rather than with actual timestamps.
How to do this however depends on how you're formatting them currently. I believe PHP has built-in timezone handling.