strange timezone issue EDT/EST - php

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

Related

how to get the correct time with php

i am trying to get the correct time with php
not the one that the user's computer gives is there a function that gives the correct "GMT" time as an example .
PHP gets the server date, not the user computer date.
If you need to get a different date timezone then you need set the timezone to your desired.
See: http://php.net/manual/en/function.date.php
like:
$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');
If you just want GMT time, you can use gmdate instead of date. For example,
echo date('Y-m-d H:i:s') . "\n";
echo gmdate('Y-m-d H:i:s') . "\n";
Output (for a server in Europe)
2019-02-04 03:34:05
2019-02-04 02:34:05
Demo on 3v4l.org
You will be getting the server time only, not the computer time
$now = new DateTime(null, new DateTimeZone('America/New_York')); //timezone format
echo $now->format("Y-m-d\TH:i:sO"); // Server time

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 ??

PHP - change timezone display for whole site

I have a website which displays dates from database using date() function. Now I have to add support for various timezones. Time needs to be changed only for display purposes in the front end. I used date_default_timezone_set('Europe/London'), but it did not affect the date output - it stays the same as in the database. Function date_default_timezone_get() shows that the timezone is set successfully, but the date output stays the same. How can I change the global output of date? Can it be done without editing all the date() functions in the site?
Full example:
date_default_timezone_set('Europe/London');
echo date_default_timezone_get(); //shows Europe/London
$date_from_db = '2014-02-02 12:34:05'; //this is generally taken from database
echo date('Y-m-d H:i:s',strtotime($date_from_db));
Outputs the same as before.
If your dates are stored in GMT, you can let strtotime() know by appending the timezone:
echo date('Y-m-d H:i:s', strtotime("$date_from_db GMT"));
Of course, setting Europe/London as the timezone, the results are likely the same.
Or, in your case:
echo date('Y-m-d H:i:s', strtotime("$date_from_db +0100"));
// 2014-02-02 11:34:05
Try to use DateTime class - see examples on this page http://www.php.net/manual/en/datetime.settimezone.php
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

How to show local singapore time using php?

I wants to display singapore time. I have tried out like this,
<?php echo date("l dS of F Y h:i:s A");
but when inserting this value to a column of data table (with datatype-datetime ) it shows the time as 0000-00-00 00:00:00. Please help
You need to insert with mysql date time format. In PHP which is equivalent to,
date("Y-m-d H:i:s")
Note: Issue is not related to Time Zone, but you can set default time zone to Singapore using
date_default_timezone_set('Asia/Singapore'); on top of your file.
date_default_timezone_set('Asia/Singapore');
$todayDate=date("Y-m-d H:i:s");
date_default_timezone_set('Asia/Singapore');
look at this for default_time_rimezone
to get the timezone.
<?php
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
?>
There are two methods to display current timestamp. Either you use "localtime" PHP method to display the current local time in an Array OR you can use DateTimeZone to display the specific timezone date-time. Here is the code ..
<?php
// Echo the local time in an array.
print_r(localtime(time(),true));
// Set the manual time zone if you wants to display other time zone dateTime (For Example : Singapore).
$from = new DateTimeZone('GMT');
$to = new DateTimeZone('Asia/Singapore');
$currDate = new DateTime('now', $from);
$currDate->setTimezone($to);
echo $currDate->format('Y/m/j H:i:s');
?>
write date_default_timezone_set('Asia/Singapore'); before displaying the date.
for inputting the value into database, use the date("Y-m-d H:i:s") format in php to match the column, or make the column type as varchar(50) to store date as string.
You can use this
http://ir2.php.net/manual/en/datetime.construct.php
// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Asia/Singapore'));
echo $date->format('Y-m-d H:i:sP') . "\n";
try with date_default_timezone_set()
<?php
date_default_timezone_set( 'Europe/Paris' ) ;
echo date( "l dS of F Y h:i:s A" ) ;
date_default_timezone_set( 'Asia/Singapore' ) ;
echo date( "l dS of F Y h:i:s A" ) ;
?>

IST time is not accurate 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.

Categories