IST time is not accurate php - php

In php I have set the follwing
date_default_timezone_set('Asia/Kolkata');
but it is not displaying accurate time.
date_default_timezone_set('Asia/Kolkata');
this is the link of my website http://www.adsnjobs.com/
here I have echo the time zone and current date time by using
echo date_default_timezone_get() . date('Y-m-d h:i:s a');

First, you should use the newer DateTime class, just as good practice:
$date = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d h:i:s a');
Second, have you tested whether the issue is with your server clock, not the IST time zone? It looks about 10 minutes off to me. Try this:
$date = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d h:i:s a :: T');
$date->setTimezone(new DateTimeZone('Etc/UTC'));
echo $date->format('Y-m-d h:i:s a :: T');
This will show you the date in UTC and in IST so you can see if there is a 5:30 offset. If there is, it's your server clock.

Related

Convert the UTC datetime to any other time zone dynamically in php

I want to convert the UTC datetime to any other time zone.its working fine.
But it showing only for one time zone,so to view the other time zone need to change the code every time.
i want to convert the UTC datetime to any other time zone dynamically.may be by selecting drop down or some other way.
<?php
$date = new DateTime('2017-11-15 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:s');
?>
You can timezone by this code where you can change the $timezone variable value from your dropdown.
$timezone= 'Asia/Calcutta';
$date = new DateTime('2017-11-15 01:00:00 +00');
$date = new DateTime($date, new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:s');

How to get current date and time with respect to Time Zone in Codeigniter

When I run the following code in codeigniter, it returns incorrect time and date for the India location. Please let me the solution.
'date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s', time());
echo $date;'
try this
$now = new DateTime();
$now->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $now->format('Y-m-d H:i:s');

strange timezone issue EDT/EST

I have checked my mysql system timezone with SELECT ##system_time_zone; and its return EDT in production server. Now I want to generate same timezone's (i.e EDT) current timestamp which is not working. This is what i have tried so far is
date_default_timezone_set('EST');
$ts = strtotime('now');
echo date('Y-m-d H:i:s',$ts);
and
$date = new DateTime(null, new DateTimeZone('EST'));
$ts = $date->getTimestamp();
echo date('Y-m-d H:i:s',$ts);
its output is 2015-09-07 05:38:43 but my mysql date (timestamp) is 2015-09-07 06:39:53 so its not same. (tested by inserting same time)
So how can I get current timestamp based on mysql's system time zone (EDT/EST)?? any help from proessional appricated
The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT ##global.time_zone, ##session.time_zone;
And try to use this code to set time zone and Location of time zone
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?
Note second php code is just an example.
I hope I helped you

Cannot convert DateTime to Hours

I' am trying to covert date time to hours so that today's date and time matches with the database date and time.
date_default_timezone_set("Pacific/Fiji");
today = date('Y-m-d H:i'); // Returns 2014-05-01 11:01
$date = DateTime::createFromFormat('Y-m-d h:i', '2014-05-02 11:05');
echo $date->format('Y-m-d H:i'); // Returns 2014-05-02 11:05
What am I doing wrong in this code that its returning 2014-05-02 11:05 whereas it should return 2014-05-01 23:05
You saved 2014-05-02 11:05 in Fiji timezone to $date. Afterwards you display the date in Fiji timezone, so of course no change is made.
I don't really understand your question but from the code you provide I guess that what you probably want to do is the following:
$tzFiji = new DateTimeZone('Pacific/Fiji');
$tzBerlin = new DateTimeZone('Europe/Berlin');
$tzCurrent = new DateTimeZone(date_default_timezone_get());
$date = DateTime::createFromFormat('Y-m-d h:i', '2014-05-02 11:05', $tzFiji);
echo $date->format('Y-m-d H:i'); # date in Fiji's timezone
$date->setTimezone($tzBerlin);
echo $date->format('Y-m-d H:i'); # date in Berlin's timezone
$date->setTimezone($tzCurrent);
echo $date->format('Y-m-d H:i'); # date in php's currently set timezone

how to convert php date formats to GMT and vice versa?

i am new to php.
i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.
Here's code to switch to GMT
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
e.g.
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
Explicit example of converting from a timezone to GMT
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Use the gmdate function to convert to GMT time.
For example
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
// Convert local time to gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}

Categories