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
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'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.
How can I convert the time zone of a date string in php without changing the default time zone. I want to convert it locally to display only. The php time zone settting should not be modified.
EDIT:
My source time is a UTC string, I want to convert it to a different format, retaining the time zone as UTC, but php is converting it to local timezone.
The code I used was:
date('Y-m-d H:i::s',strtotime($time_str));
How do I retain timezone?
$src_tz = new DateTimeZone('America/Chicago');
$dest_tz = new DateTimeZone('America/New_York');
$dt = new DateTime("2000-01-01 12:00:00", $src_tz);
$dt->setTimeZone($dest_tz);
echo $dt->format('Y-m-d H:i:s');
Note that if the source time is UTC, you can change the one line to this:
$dt = new DateTime("2000-01-01 12:00:00 UTC");
Edit: Looks like you want to go to UTC. In that case, just use "UTC" as the parameter to the $dest_tz constructor, and use the original block of code. (And of course, you can omit the $src_tz parameter if it is the same as the default time zone.)
I am having an issue with php date() function.
When I save the date in mysql datebase the hours shows 4 hours less than my current time.
My php code is below: $add_date = date("Y-m-d H:i:s");
It saves the time in database as
2011-08-03 07:51:26
But is should show
2011-08-03 13:22:26
Can anybody tell me how to fix it
Thanks
You have to set a valid time zone - it seems you don't have an appropriate time zone set up in php environment for your location.
Check out
http://www.php.net/manual/en/function.date-default-timezone-set.php
It should give you a clue how to set up the correct time zone in php.
#Mujahid If the printed time and the saved time are the same it probably means your server is not in the same timezone as you. With that said, you have to manually set the default timezone for your PHP script at the top of the file, or get the DateTime by defining your timezone explicitly. Here's the code:
$dateTime = new DateTime("now", new DateTimeZone('America/Los_Angeles'));
$add_date = $dateTime->format("Y-m-d H:i:s");
echo $add_date;
Here's a list of time zones that PHP supports, just find yours and replace America/Los_Angeles with it.
http://php.net/manual/en/timezones.php
Here's a good tutorial on PHP DateTime and DateTimeZone...
http://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/
Hope this helps. Good luck.
Make the time zone setting of the MySQL server and the web server the same.
this will add hosted server time not local time
$add_date = date("Y-m-d H:i:s");
use strtotime() to add hours and minutes
$newdate = date("Y-m-d H:i:s",strtotime('+ 5 hours 30 minutes'.$row['db_date']));