I'm using the AWS PHP SDK and Amazon's API is returning this timestamp below.
2013-04-23T13:18:00Z
The timestamp they are giving me seems to be in my timezone already. Since it doesn't specify which timezone it is, I guess it's assumed to be my local time. The wiki site for ISO 8601 also states...
If no UTC relation information is given with a time representation, the time is assumed to be in local time.
But when I try to use it in PHP like this it is converting it into my local timezone. PHP's strtotime page says this...
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
My question is why is strtotime converting it into my timezone if it's already in my timezone?
The "Z" at the end of the date returned by Amazon means it's UTC. You will need to convert the timezone to have it be localized. You can do this by doing the following:
<?php
$date = new DateTime('2013-04-23T13:18:00Z'); // Z = UTC
echo $date->format('H') . PHP_EOL;
//> 13
$date->setTimezone(new DateTimeZone('America/Los_Angeles')); // -0700
echo $date->format('H') . PHP_EOL;
//> 06
Update: Read all the comments for the full solution.
Related
I am facing the following problem when converting a date value using strtotime().
If I do: strtotime('1/1/2019') the output would be 1546293600 -> 31.12.2018 # 10:00pm (UTC)
If I do: date_default_timezone_get() the output is Europe/Bucharest
In my php.ini file (of my hosting account), the timezone is set to date.timezone="Europe/Helsinki"
I have following two questions.
Shouldn't date_default_timezone_get() output what it is configured
in my hosting accounts php.ini file? (the
date_default_timezone_set() is not being used)?
Why the strtotime() conversion result is on GMT-2 timezone? It is
my understanding that if there is no timestamp supplied, the
conversion should be done on the current time (which should be
GMT+2, either Helsinki or Bucharest)?
If I do date_default_timezone_set('UTC') the conversion result of strtotime('1/1/2019) is done correctly (on GMT+2).
Thank you.
There may be some reasons why this is happening and we can't tell you why, because we don't know your application, since it may somewhere change the time zone at runtime.
Instead you should stop using strtotime and embrace object oriented DateTime functions.
Using DateTime you can define your date along with its time zone:
// local time
$date = new \DateTimeImmutable('1/1/2019', new \DateTimeZone('Europe/Bucharest'));
echo $date->format('U > d.m.Y (e)') , '<br>';
// convert to UTC
echo $date->setTimeZone(new \DateTimeZone('UTC'))->format('U > d.m.Y (e)');
The library is quite powerful and much more verbose than operating on strings and unix timestamps.
Provide hour and timezone to the function using the full IS0 8601 format 2019-01-01T12:00:00+00:00. That way you will get the correct timestamp in UTC. If you won't use the hour I recommend to use 12 AM.
In the following example I will use a numeric date array to build this string
// [Y,m,d]
$date = [2019,1,1];
for($i=1; $i<3; $i++){
$date[$i] = str_pad($date[$i],2,'0',STR_PAD_LEFT);
}
$t1 = strtotime(implode('-',$date).'T12:00:00+00:00');
echo date('r',$t1);
//=>Tue, 01 Jan 2019 12:00:00 +0000
My php web servers timezone is EDT (Eastern Day Light Time (US)). My current timezone is GMT+05.30. I need to enter current timestamp into my mysql database with the timestamp in my current timezone. By the way, I'm using a free php web server for my use. So I will not be having any previleges for modifying the server. Can some one suggest me some way of converting it to my GMT+05.30 from EDT in php using any script.Thanks in advance.
Have a look at the following example
$timezone = new DateTimeZone('Europe/Berlin');
$date = new DateTime('#' . $yourTimestamp, $timezone);
echo $date->format('c');
So through the DateTimeZone Object your Time will be formatted by the DateTime object.
I suggest you use the date_default_timezone PHP function.
You can read about it here and here is the list of Supported Timezones
Example:
<?php
date_default_timezone_set('Europe/Amsterdam');
?>
Use this:
echo gmdate("Y:F:l h:i:s",time()+19800);
I am trying to convert 1355657846 and 1355677646 unix timestamp to Y-M-D H:i:s format.
The problem is in H:i:s . It should be
11:37:24 and 17:07:26 respectively but it is showing 12:37:24 and 18:07:26.
<?php
echo date('Y-m-d H:i:s','1355657846');//2012-12-16 12:37:26,must be 11:37:26
echo date('Y-m-d H:i:s','1355677646');//2012-12-16 18:07:26,must be 17:07:26
?>
It should be 11:37 and 17:07 because I checked it in unix timestamp conversion
and also it is the time I had received mail in gmail account. And I got these unix timestamp from gmail( using php imap function...$overview->udate)
I am testing this on local xampp server.
Can anyone suggest me where I am going wrong here?
PS: I checked related question in stackoverflow, but here I want to convert timestamp to datetime, which I think should be constant irrespective of timezone.
It is definetily the timezone setting in your local web server.
Check the php.ini for date.timezone value.
It also may be overridden by htaccess file or you PHP script.
strtotime was included once DateTime was created. The parsing is dependant of the server default timezone.
If you want get your date with the good timezone, there are two ways :
default_timezone_set('Europe/Paris');//for example
or :
$date = DateTime::createFromFormat('U',$timestamp,new DateTimezone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s);
you can find more informations here .
This is probably due to the TimeZone setting. Learn about PHP date_default_timezone_set() and make sure your local time zone matches the timezone you really want. All Unix Timestamps are the same world-wide. But the local DATETIME values vary around the globe. This article may be visible to you (hope so):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Best regards, ~Ray
I am working on an iPhone app where I am getting date and time(separate) from user and submitting it to my php web service. php web service is then supposed to take this date and time and save it to utc timezone. I have read that I will also require local timezone in php file to convert it to utc. I am getting 'Asia/Kolkata (IST) offset 1980' as my timezone value in my iPhone application and can send it to php file. Now how will I convert date, time and timezone to utc date.
I have to convert the date to utc format so that I can query my table to get me data which is not past local time.
Thanks in advance
Regards
Pankaj
Use setTimeZone to convert the timezone from whatever the user sends to UTC:
$utz = new DateTimeZone($input['timezone']);
$udt = new DateTime($input['date'].' '.$input['time'], $utz);
$udt->setTimezone(new DateTimeZone('UTC'));
echo $input['date'].' '.$input['time'].' '.$input['timezone'].' is '.$udt->format('Y-m-d H:i:s')." UTC\n";
check out PHP DateTime();
Essentially you'll want to handle it somewhat like below.
$date = new DateTime($dateFromiPhone);
$date->setTimeZone(new DateTimeZone("UTC"));
And then use $date->format(); to format for your desired usage
Using PHP 5.2.5 I was working with some DateTime objects and noticed a time that seemed off. The problem I'm having may be related to timezones, but I'm not sure - I'm creating a DateTime from a Unix Time Stamp and getting different/unexpected results depending on how I output it.
I created the following to easily illustrate the "issue":
$timezone = new DateTimeZone('America/Chicago');
$now = time();
$now_datetime = new DateTime('#' . $now, $timezone);
echo phpversion() . "\n\n";
echo $now . "\n";
echo $now_datetime->format('U') . "\n\n";
echo date('g:i:sa', $now) . "\n";
echo $now_datetime->format('g:i:sa') . "\n\n";
This outputs the following:
5.2.5
1287676530
1287676530
10:55:30am
3:55:30pm
I'm currently in the correct timezone, and the server shows the "right" time (10am) when using the date() function to output a formatted date, as well as 'America/Chicago' being the default timezone on that machine. But, when outputting values via DateTime::format(), the times are very different.
I added the ->format('U') just to verify that it was holding the correct timestamp.
So, I'm probably doing something wrong or I have the wrong expectations. So what am I missing?
It seems like a timezone issue with DateTime, but if that's the case, why does it show "now" in America/Chicago as ... wrong?
The timezone you 'insert' into DateTime is the timezone of the string, which may not be your current timezone, so PHP can calculate the actual datetime, not the timezone it uses to format your output. If you want it to output the time in a specific timezone, use $now_datetime->setTimezone($timezone).
The manual on DateTime's constructor has the answer:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. #946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
This means that your timestamp is treated as a GMT one, explaining the 7 hours' difference.