Time is not converted propely on server ,
I'm converting timestamp , on local host it works well, but on server it's earlier two hours ,
$ToConvert = 1570080669;
$dt = new DateTime();
$dt->setTimestamp($ToConvert);
$EndTime = $dt->format('m/d/Y H:i');
echo $EndTime;
on local host :
10/03/2019 07:31
on Server :
10/03/2019 05:31
what might be the problem?
The DateTime class in PHP has a method called "setTimezone" which expects an instance of DatetimeZone as an argument. Using your code as an example, you just need to add one extra line as follows:
$ToConvert = 1570080669;
$dt = new DateTime();
$dt->setTimestamp($ToConvert);
$dt->setTimezone(new DatetimeZone('Europe/London'));
$EndTime = $dt->format('m/d/Y H:i');
echo $EndTime;
You can change the argument when instantiating a new timezone as desired to suit your needs.
More information is on the php.net website:
https://www.php.net/manual/en/datetime.settimezone.php
Related
I am currently working on displaying a moment. I want the view to use DateTime->format(..). The value I am getting from the API is 1502462223168. However, this is displayed as 1945-5-26 16:36 instead of 2017-8-11 16:37, since the original value exceeds the PHP_MAX_INT value on the system.
Is there a way I can use the original value, maybe as a String, to create the DateTime object?
since your timestamp value is in milliseconds, divide it by 1000 and then use DateTime, like:
$date = new DateTime();
$stamp = intval(1502462223168/1000);
$date = DateTime::createFromFormat("U", $stamp)->format("Y-m-d H:i:s");
echo $date;
Use this it will be working fine for your question
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
I am trying to get a timezone name from a UTC offset in Laravel 5.3 using Carbon. Code listed below any help would be much appreciated.
/* current code iteration */
$utcOffset = -5;
$timezone = Carbon::now($utcOffset)->timezone->getName();
echo $timezone;
// Result: -05:00
// Expected Result: EST
/* tried code */
$timezone = Carbon::now($utcOffset)->tzName;
// Result: -05:00
/* What I used prior to Carbon */
$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');'
What am I missing? I feel daft..
Preface:
The accepted answer works in most cases but as mentioned in the user contributed notes area of timezone_name_from_abbr(), there are issues with using the function, like returning false instead of actual timezone and returning a "historical" (i.e. deprecated) timezone identifier rather than the current standard one for a given location. Which are still valid to this date.
Also, the original code returns the value as expected, as long as you know that as per Carbon docs, if you look at https://carbon.nesbot.com/docs/#api-timezone
the original name of the timezone (can be region name or offset string):
One more thing to note here is that, it is considered not reliable to derive timezone off of offset value as it does not take into consideration the DST observed periods offset.
So, this all to actually say that deriving timezone off of offset is not always possible.
Answer:
But since the OP mentioned Carbon and timezone based on offset, as per the Carbon docs as of now, the answer should be
$date = Carbon::now('-5');
echo $date->tzName;
Tried updating Carbon to no evail ended up using the old datetime class.
$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');
In a new Carbon it is timezoneName property;
$now = Carbon::now(-5);
echo $now->timezoneName;
//or
echo $now->timezone->getName();
This works for me:
$now = Carbon::now(-5);
echo $now->timezone;
// prints 'America/Chicago'
I'm developing a PHP project and I'm using Parse SDK. What i want to do is adjust the time given by the Parse Database. It gives me time and date that 8 hours late to my timezone. Here's the code I'm using :
$query = new ParseQuery("TestObject");
$query->get("xWMyZ4YEGZ");
$dateTime = $query->getCreatedAt();
$sched = $dateTime->format("M d, Y - hA");
echo $sched;
How can i adjust it to specifically "GMT+8" TimeZone? Thanks!
You have to set the date_default_timezone_set before doing any thing as:
if(function_exists('date_default_timezone_set'))
date_default_timezone_set($timezone);
List of timezones are provided here...
Have a look at PHP date with TZ - See N.B.'s answer here is a code snippet he suggests (you may be able to use the parse date instead of the "now" parameter):
<?php
$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');
I am unable to set timezone for my DateTime objects.
Here is my code :
$dt = DateTime::createFromFormat('U',time(),new DateTimeZone('Asia/Kolkata'));
print_r($dt->getTimeZone());
Here is the output :
DateTimeZone Object
(
)
I also tried putting these lines at the top (one at a time) - without any success:
date_default_timezone_set('Asia/Calcutta');
ini_set('date.timezone', 'Asia/Calcutta');
date_default_timezone_set('Asia/Kolkata');
ini_set('date.timezone', 'Asia/Kolkata');
This is because you specified UNIX timestamp in the parameter. See what php manual says.
The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
What you are trying to do can be easily done by,
$dt = new DateTime("now", new DateTimeZone('Asia/Kolkata'));
If you have a variable that contains UNIX timestamp, first create a DateTime object with it. Then set the new TimeZone.
$dt = new DateTime("#$timestamp");
$dt->setTimezone( new DateTimeZone('Asia/Kolkata'));
http://codepad.viper-7.com/topBCR
try this
<?php
$dateTimeZoneAsia = new DateTimeZone("Asia/Kolkata");
$dateTimeAsia = new DateTime("now", $dateTimeZoneAsia);
$timeOffset = $dateTimeZoneAsia->getOffset($dateTimeAsia);
var_dump($timeOffset);
?>
Example, I have time zone is : "Europe/London" or "America/Los_Angeles"
In Perl, I can look up for Local Time by:
use DateTime;
use DateTime::TimeZone;
$timeZoneName = "MY TIME ZONE HERE";
$timeZone = DateTime::TimeZone->new(name => $timeZoneName);
$timeNow = time();
$dateTime = DateTime->from_epoch(epoch => $timeNow);
$offset = $timeZone->offset_for_datetime($dateTime);
$localTime = $timeNow + $offset;
print "$localTime";
But I don't know how to look up local time with PHP, anyone help?
You can do this in PHP:
$timezone = 'Europe/London'; //perl: $timeZoneName = "MY TIME ZONE HERE";
$date = new DateTime('now', new DateTimeZone($timezone));
$localtime = $date->format('h:i:s a');
echo "Local time is $localtime.";
If you don't have PHP 5.2, see also date_default_timezone_set() or use ini_set('date.timezone', $timezone); and then PHP will operate as if it were in that timezone. Any date you output will be in that of the given timezone.
PHP Date/Time Functions also have good references. There are other ways you could do this in PHP but the above is simple and most servers should have PHP 5.2 or greater.
See date() for a list of formats you can pass to DateTime::format and also the DateTime Class for references on other things you can do with DateTime.