PHP: why is unix gmt time an hour fast? - php

if I set my timezone to GMT and use the PHP date() function to get the current time/date stamp it gets the correct time. however when I used a unix generator to make a timestamp for 9am this morning it comes back as 10am even though I've used various different generators and made sure they are in GMT. why is it coming back an hour fast? here is my code:
<?php
date_default_timezone_set('Europe/London');
$timestamp_time = date("hi", 1377766800);
echo $timestamp_time;
?>

Related

PHP Date formatting DST

I generate a unix-timestamp in the adminpanel of my site with a modified version of the jquery datetime-picker.
This timestamp get's saved into a mySQL database and in the frontend I output it via the strftime() function.
As the site has multiple admins and everybody began to add content, I encountered an offset of 1h in the output time, due to the timezone we're in (GMT+1). Because content was already added, I couldn't edit the function which generates the date and writes it to the db, so I had to edit the output function in the frontend.
I thought it would be clever to just add (myTime+3600) as an argument of the strftime function. Everything worked fine and more content got added.
Now the problem is, that all content assigned a date in april 2013 is an hour to late because of the Daylight saving time.
I only have the timestamp to work with so I somehow need to get whether a date needs the 1h offset or not. Any ideas on how to do that without adding a timezone at the timestamp generating function?
There is a function.
Any recommendations are welcome!
Example:
frontend-code:
strftime("%d. %B %Y / %H:%M Uhr",new_timestamp+3600);
For 13.March 2013 16:30 I get the correct output.
For 13. April 2013 16:30 I get 17:30
set the application default timezone using this http://php.net/manual/en/function.date-default-timezone-set.php that way regardless of what timezone the client is in it should force the timestamps into your primary timezone.
You may also need to edit the datetimepicker inits to force jquery to use the server based timezone too.

php code to get the timezone and the difference in current system time with GMT

Please help me with the php code to get the timezone and the difference in current system time with the London time
I need to get the time zone from where the mail is sent.After that i need to get the current system time as well as the GMT difference of current system time in php
get the current system time zone offset
get the the london time zone offset(from mail is sent)
London time zone : date_default_timezone_set('Europe/London');
PHP is server-side, and calling date() will give the time of the server.
You can pass different arguments to date() to get the information you need. date('Z') will give you the timezone offset in seconds (offset from UTC/GMT). You can also get it in hours and minutes using date('P').
To get the timezone of your local machine, you'll have to use something client-side, like Javascript. You could use jsTimezoneDetect to do this.
Give Javascript access to the server date, and compare the two dates:
var server_date = new Date(<?= date("YYYY-MM-DD") ?>);
var local_date = new Date();
You'll need to replace YYYY-MM-DD with a more appropriate time-and-date format string.
This blog and this PHP class should be enough for your needs
Tested function to get timezone difference between UTC and User's Timezone (Asia/Kolkata) can found
TimeZone Difference

Strange issue in date in javascript and php

i m confuse in the date in javascript and php.
<?php
$mydate = date('2012-05-02 17:00:00');
echo 'Today PHP --'.$mydate;
$mytimstamp = strtotime($mydate);
echo '<br/>My PHP unix time stamp --'.$mytimstamp;
echo '<br/>'; ?>
<script type="text/javascript">
document.write('My Javascript unix time stamp --'+new Date(Number('<?php echo $mytimstamp;?>')*1000));
</script>
OUTPUT
Today PHP -- 2012-05-02 17:00:00
My PHP unix time stamp --1335978000
My Javascript unix time stamp --Wed May 02 2012 22:30:00 GMT+0530 (India Standard Time)
Why i m getting different time in javascript????
You are getting the same time, except in different time zones.
On PHP side you have a GMT time, and on JavaScript side you have India time.
It looks you are from India. Just make sure you understand the concept of time zones and you store the time with time zone information or in GMT/UTC time. This way you should avoid issues with incorrectly using timestamps from different time zones. Displaying such time in the form suited for user's time zone becomes trivial if you know the time zone in which it was generated.
if you do not change your timeZone than change your code like
<script type="text/javascript">
var d = new Date();
var offset = d.getTimezoneOffset();
document.write('My Javascript unix time stamp --'+new Date(Number(<?php echo (intval($mytimstamp));?> + offset*60 )*1000));
</script>
Your PC must have a different timezone set to that of the server running the PHP code.
The PHP date/time is calculated using the setting for the server whereas the JavaScript date/time is calculated using the clients PC settings.
In the javascript is depend on your browser. You would better decode date format on your javascript. You will get another different date on your different browser.

Converting UTS (UTC-0) times to BST in PHP or jQuery

I'm pulling the most recent listened tracks from last.fm and putting them on my website.
Problem is, the times are retrieved in UTC-0 uts format and appear to be an hour out when comparing them to BST times in order to calculate a fuzzy time stamp ("about 5mins ago", "about an hour ago" etc).
Is there any way solve this so the times always match BST/GMT and adjust when entering and leaving daylight saving time?
Here's a snippet of PHP code i'm using at the moment, which results in the times being an hour out.
$now = time(); // use this so all times are to the same second
$tz = getenv("TZ"); // save local setting so we can reset it later
putenv("TZ=Europe/London");
$trackPlayedAt = date('d M Y H:i:s', $track->date->uts);
date() automatically formats to the local timezone. The timezone depends on the configuration of the PHP server. If everything is set correctly, it should just work.
If you are running PHP 5.3 you have more options. Comment with which version of PHP you are running.

Problem in DATE AND TIME

I am using date() function it getting the date and time as per my given format but the time its showing me is 4 hours forward than my current local machine time:
This is my code
echo date("Y-m-d h:i:s", time());
Its showing me : 2009-10-28 08:47:42
Where as it should Disply : 2009-10-28 04:47:42
Any Idea whats wrong with this and why its showing different time.
it is likely giving you GMT, you need to set your timezone: e.g. date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Make sure your time zone is set correctly:
e.g.
date_default_timezone_set('UTC');
It's returning the timezone of your server, not your computer
try http://www.php.net/manual/en/function.date-default-timezone-set.php
Because you are in, probably, US/Eastern (America/New_York, currently EDT) time zone, but the PHP you are using is running in UTC. You need to ensure that the TZ variable is set in the PHP environment.

Categories