how can change (America/Chicago) time into indian(Asia/Kolkata) time - php

$row['created'] <- a variable get date/time into Database .
$row['created'] have time like 2016-10-07 01:31:42 . and time zone(America/Chicago) .
so I would like show time according Indian time zone . I am using code shown in below but is not working.
$timestamp = strtotime($row['created']);
date_default_timezone_set("Asia/Kolkata");
echo date('r', $timestamp);

Try this code to save as indian time.
<?php
$date=date_create('2016-10-07 01:26:56',timezone_open("America/Chicago"));
echo date_format($date,"Y-m-d H:i:sP")."<br>";
date_timezone_set($date,timezone_open("Asia/Kolkata"));
$indiatime = date_format($date,"Y-m-d H:i:sP") . "<br>";
echo $indiatime ;
?>

You can use DateTime class. Look at this DateTime doc example.
$date = new DateTime($row['created'], new DateTimeZone('America/Chicago'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:sP') . "\n";
This will let you jump between timezones.

Try this, use timezone_open and date_format
$timestamp = "2016-10-07 01:31:42";
$date = date_create($timestamp, timezone_open('Asia/Kolkata'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n"; //echo 2016-10-07 01:31:42+05:30
or
$timestamp = "2016-10-07 01:31:42";
$user_tz = 'Asia/Kolkata';
echo $timestamp. "\n"; // echo 2016-10-07 01:31:42
$schedule_date = new DateTime($timestamp, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn; // echo 2016-10-06 20:01:42
DEMO

If you are using mysql, you do this also:
SELECT CONVERT_TZ(created,'America/Chicago','Asia/Kolkata');
Instead of coding in php, when you fetch data from database you may be use the following query: select field1,field2,created from <tablename> where <yourCondition>. Instead of doinf so update your query this way:
select field1,field2,CONVERT_TZ(created,'America/Chicago','Asia/Kolkata') from <tablename> where <yourCondition>

Related

Get the current time and date by using DateTime::createFromFormat

How can I get the current date and time for this time zone 'America/phoenix'? I can't figure out how this can be done with the DateTime::createFromFormat.
My Code example
<?php
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, '/*Example: 6-12-2019 12:05am*/', new
DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
?>
You can change the second parameter of createFromFormat, but from your question, I guess you are not America/phoenix, therefore, use this code:
<?php
date_default_timezone_set('America/phoenix');
$currentTimeInPhoenix = date('n-j-Y g:iA');//gets the currenttime in phoenix
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, $currentTimeInPhoenix, new DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
date_default_timezone_set('YOUR OLD TIMEZONE');
?>

How to convert current IST time to UTC ? Also need to find the time one hour before that UTC time

Need to convert the current time in IST (India Standard Time), stored in a PHP variable to UTC time, and store it to another variable using PHP.
date_default_timezone_set('Asia/Kolkata');
$Present = date('Y-m-d H:i');
echo $Present; //to be converted to utc
$GMT = gmdate('Y-m-d H:i', strtotime($Present));
date_default_timezone_set('Asia/Kolkata');
$Present=date('Y-m-d H:i');
echo $Present; //to be converted to utc
$date = new DateTime($Present);
$date->setTimezone(new DateTimeZone('Africa/Abidjan'));
echo $date->format('Y-m-d H:i');
Use gmdate
echo gmdate('Y-m-d H:i', strtotime($Present));
following will decrease 1 hour
echo gmdate('Y-m-d H:i', strtotime($Present.'-1 hour'));
Try the php built in getTimezone and setTimezone,
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
Here is the simple and tested solution
$presentInst = new \DateTime(now('Asia/Kolkata'), new \DateTimeZone('Asia/Kolkata'));
$present = $presentInst->format("Y-m-d H:i:s e");
echo $present . "<br/>"; // your local time i.e. in IST
$presentInst->setTimezone(new \DateTimeZone("UTC"));
$utcTime = $presentInst->format("Y-m-d H:i:s e");
echo $utcTime; //UTC time
Hope this helps
Please read this, it should solve your problem
http://php.net/manual/en/datetime.gettimezone.php
example :
$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // your time
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // UTC

PHP converting UTC to Local Time

In my postgresql, the I have the following column named "created" that has the type timestamp with timezone
So I inserted the record according to the format as such which I believe is UTC.
2015-10-02 09:09:35+08
I am using php Carbon library so i did the following:
$date = Carbon\Carbon::parse('2015-10-02 09:09:35+08');
echo $date->->toDatetimeString();
//gives result as 2015-10-02 09:09:35
How can I use the library to echo the correct timezone which includes the adding of the +8 in the above datetime format? The timzezone that I am using is "Asia/Singapore".
The time should be printed to local timing which is 2015-10-02: 17:09:35:
Try this:
$timestamp = '2015-10-02 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Asia/Singapore');
Try this using standard PHP:
$raw = '2015-10-02 09:09:35+08';
$date = substr($raw,0,19);
$tzOffset = (strlen($raw) > 19) ? substr($raw,-3) : 0;
$timestamp = strtotime($date) + (60 * 60 * $tzOffset);
$localTime = date('Y-m-d H:i:s',$timestamp);
echo 'local time:['.$localTime.']';
The result is:
local time:[2015-10-02 17:09:35]
This will also work without a time zone offset or a negative one.
You can do this using native php without using Carbon:
$time = '2015-10-02 16:34:00+08';
$date = DateTime::createFromFormat('Y-m-d H:i:s+O', $time);
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Asia/Singapore'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Etc/UTC'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;

Adding date in mysql with timezone

My query is i am adding date in my MySQL table which is now added with CURRENT TIMESTAMP or BY NOW ()... but it takes server time ... i need date/time to be added in my timezone i.e asia/calcutta.
i am trying something but it seems i doing it all wrong
<?php
$today = NOW();
$date = new DateTime($today, new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
can anybody help ??

addition of gmt times in php

i have a time zone in 2010-05-04T05:27:00.000Z format which indicates the GMT time and i want to add GMT 10+ in to it using php.
i can do that thing using following code but how would i directly add 2010-05-04T05:27:00.000Z and GMT 10+ so that i can get a valid date and time.
$offset=10*60*60;
$dateFormat="d-m-Y H:i::m:s";
echo $timeNdate=gmdate($dateFormat, time()+$offset);
Maybe I'm missing the point but are you not really looking for DateTime::setTimezone?
$timezone = new DateTimeZone('Etc/GMT-10'); // GMT+10:00
$datetime = new DateTime('2010-05-04T05:27:00.000Z');
$datetime->setTimezone($timezone);
echo $datetime->format('r');
// Tue, 04 May 2010 15:27:00 +1000
Use DateTime class http://php.net/manual/en/book.datetime.php exacly DateTime::Add() http://www.php.net/manual/en/datetime.add.php
You have some example here:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
And another:
<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>

Categories