I am bringing in data that looks like this via PHP:
Sun Jun 26 02:00:01 EDT 2016
It becomes this variable:
$arr_dates[] = date("m/d/Y H:i", strtotime($datestr, time()));
And becomes used in Highcharts:
series: [{
name: '# users online',
data: [<?php echo "\n"; foreach ( $arr_num_users as $i ) { echo "[Date.parse('".$arr_dates[$ct]."'),".$i."],\n";$ct++; } echo "\n]"; ?>
}
So when I (EDT timezone) view it, the graph shows up correctly and the times are displayed as GMT+0.
But anyone else using a different timezone, the times are off. So someone in PDT will see a graph that is skewed by 3 hours which is incorrect. It should always be a set time, or at least accurate.
How can I fix this? setUTC did not seem to help.
Highcharts.setOptions({
global: {
useUTC: true
}
});
$('#container').highcharts({
chart: {
zoomType: 'x'
},
rangeSelector : {
selected : 1
},
.....
I've also tried without any setOptions at all, nothing changes.
This is under Highcharts v3.0.10
To be clear, the time on the chart is changing based on the users' timezone. I do not want that to happen.
You can view the issue here: http://observit.org/rffxiv.php (switch to EDT for your timezone which displays correctly, then to something else and refresh; issue will be shown)
This worked for me.
Highcharts.setOptions({
global: {
timezoneOffset: 5 * 60 //EST offset
}
});
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm creating a PHP script for a Christmas calendar which is supposed to only load the respective content (some text and images) based on the current (December) date.
For example, on the 1st December (based on the PHP script) only the specified content should be shown. On the 2nd December, the specific content for that day/date should be shown.
My problem now is to ensure that in Germany we, obviously, have a different time zone than in (for me important) Vancouver, Canada.
How can I get the script working with the right request/checks on loading for the timezone/date that in these two particular timezones the content is visible at all times (while it is for instance the 1st of December in one of these timezones)??
This answer is based on the assumption that you use a webpage to show the user timezone.
First note that you can not get the user timezone in PHP. But what you can do is tell the server what the user date is via JavaScript.
You can do something like this:
var currentdate = new Date();
if(currentdate.getMonth() === 11){ // note that January = 0, Feb = 1 etc. So December = 11
$.get( "//christmasServer.com/timezone.php?user_day=" + currentdate.getDate(), function( data ) { // send to server, getDate() will show the day, December 14 will just show 14
$('.Christmas').html( data ); // do something with the data, for example replace the div .Christmas
});
} else {
// It's not even december in your timezone :-o
}
In timezone.php you can now determine what the day is.
As addition:
You can set the timezone in the $_SESSION so that you only have to set it once.
timezone.php:
if(!isset($_SESSION['user_timezone'])){ // if timezone isn't set
session_start(); // start session
$_SESSION['user_timezone'] = $_GET['user_day'];
}
if($_SESSION['user_timezone']===1){ // if it's the first of December
echo 'I\'m shown December first';
} else if($_SESSION['user_timezone']===2){ // if it's the second of December
echo 'I\'m shown December second';
}
// etc...
You can now always use $_SESSION['user_timezone'] to get the users' timezone.
Update:
In my of index.php:
<script type="text/javascript">
var currentdate = new Date();
if(currentdate.getMonth() === 10){ // for testing with november (=10)
$.get( "/timezone.php?user_day=" + currentdate.getDate(), function( data ) { // correct for localhost testing version, so no domain yet?!
$('.Christmas').html( data ); // do something with the data, for example replace the div .Christmas
});
} else {
// It's not even december in your timezone :-o
}
</script>
Later in the index.php:
...
<div class="Christmas">
</div>
...
In my timezone.php (in same folder):
if(!isset($_SESSION['user_timezone'])){ // if timezone isn't set
session_start(); // start session
$_SESSION['user_timezone'] = $_GET['user_day'];}
if($_SESSION['user_timezone']===1){ // if it's the first of December
echo 'Im shown December first';} else if($_SESSION['user_timezone']===2){ // if it's the second of December
echo 'Im shown December second';}
i'm using jquery countdown with php. i have given an end date which is going to the countdown. my problem is lets suppose 1 hour left is showing in countdown but when a user change its system time the countdown changes. like if a user back his time 1 hour then the counter will display the 2 hours left. is there any way to get the server time for more accurate time not the user system time. please help.
how can i get server time not user system time?
below is my jquery code
if($(pluginsArray[6]).length){
$(pluginsArray[6]).each(function(){
var $this = $(this),
dateObj = $this.data();
var finalDate = new Date(dateObj.year, dateObj.month, dateObj.day, dateObj.hours, dateObj.minutes);
$this.countdown({
timezone: +4,
until : finalDate,
expiryText: '<div class="over">Closed.</div>',
onExpiry : function(){
setTimeout(function( ) { location.reload(); }, 5000);
},
format :'DHMS',
layout : '<b>{dn}</b> <span class="fs_medium d_inline_b m_right_5">days</span> <b>{hn}</b> <span class="fs_medium d_inline_b m_right_5">hrs</span> <b>{mn}</b> <span class="fs_medium d_inline_b m_right_5">min</span> <b>{sn}</b> <span class="fs_medium">sec</span>'
});
});
}
and here is what i did in php
<div class="countdown color_redc d_inline_m fs_large second_font lh_small f_xs_20" style="font-size:26px;" data-year="<?= $aDate[0] ?>" data-month="<?= ($aDate[1] - 1) ?>" data-day="<?= $aDate[2] ?>" data-hours="<?= $aDate[3] ?>" data-minutes="<?= $aDate[4] ?>"></div>
Solution without PHP
What you can do, without coding any server side is using a public API to get current time.
Found a similar topic on StackoverFlow : Free Rest API to get current time as string (timezone irrelevant)
TimezoneDb provides a free API: http://timezonedb.com/api
GenoNames also has a RESTful API available to get the current time for
a given location: http://www.geonames.org/export/ws-overview.html.
You can use Greenwich, UK if you'd like GMT.
GenoNames looks to be US only, TimezoneDb works you just need to register for a free public key.
Few people recommend timeapi.org but looks like they do not accept CROSS-DOMAIN request in Ajax, and the exemple they provide is no longer available.
Solution with PHP and jQuery Countdown configuration
Also you can ask jQuery CountDown to synchronyze with your server using serverSync option
$(selector).countdown({
until:liftoffTime, serverSync: serverTime});
function serverTime() {
var time = null;
$.ajax({url: 'http://myserver.com/serverTime.php',
async: false, dataType: 'text',
success: function(text) {
time = new Date(text);
}, error: function(http, message, exc) {
time = new Date();
}});
return time;
}
PHP file : serverTime.php
<?php
$now = new DateTime();
echo $now->format("M j, Y H:i:s O")."\n";
?>
BUT
Keep in mind your user will always be able to change your code and fake it ... so if you need to implement some security this is not enough and you will need to code some backend stuff.
highcharts gets my hour time wrong
I'm from venezuela just in case. I doing a real time system where I get in my db the time,seconds and miliseconds like
10:39:09:2
I apply the strtotime($time) then I sended by json to charted
and in my highcharts i got in the
xAxis: {
type: 'datetime',
title: {
text: 'Tiempo'
}
the utc function is already false
Highcharts.setOptions({
global: {
useUTC: false
}
});
and my function to get the json is
function requestData_Frecuencia() {
$.ajax({
url: 'Datos_Frecuencia.php',
success: function (point) {
var series = chart_Frecuencia.series[0],
shift = series.data.length > 400;
//add point
chart_Frecuencia.series[0].addPoint(eval(point), true, shift);
setTimeout(requestData_Frecuencia, 500);
},
cache: false
});
}
PS. is been a while since I write in english so forgive me if I write something wrong o that isn't clear.
You should check the raw values in the database. I know that MySQL stores dates in GMT. Not sure about the DB you are using, but the reason that this is done is:
GMT is an absolute time reference & doesn't change with the seasons.
You will have to convert the time to your locale, which is UTC–04:30
Have a look at: Convert date to another timezone in JavaScript
Highcharts doesn't include timezones and only what you can do is disabling UTC,aas you have. THen you need to translate your data or use label formatter / tooltip formatter to display correct data.
I found out what I was doing wrong (found the answer here ). I used to get my time in php like:
while($r2 = pg_fetch_array($sth)) {
$hora = $r2['hora'];
$Frecuencia = $r2['frecuencia'];
}
$x=strtotime($hora);
I needed to multiplied by 1000 the time. I think, is because I need to suggested that date is in form of miliseconds (that is integer)
while($r2 = pg_fetch_array($sth)) {
$hora = $r2['hora'];
$Frecuencia = $r2['frecuencia'];
}
$x=strtotime($hora)*1000;
PD: thanks to everybody anyway for the suggestion and responses given. I really appreciated
I have Kendo UI grid when I save a new date value, the value send as POST request like
Wed Jun 19 2013 17:48:32 GMT+0200 (Egypt Standard Time)
I am trying to take this value and send to the web service which waiting value with type "DateTime"
I want to convert the sending value date even in Kendo UI before send as POST value or
in the PHP file when I receive this value
Any Help ?
kendo ui allow set culture and date format adding
please checkout http://docs.kendoui.com/api/framework/kendo#culture
http://docs.kendoui.com/getting-started/framework/globalization/dateformatting
Reading i found this:
http://www.kendoui.com/forums/framework/globalization/kendo-fails-to-parse-or-format-utc-dates.aspx
parameterMap: function (options, operation) {
if (operation != "read") {
var d = new Date(options.Date);
options.Date = d.toString("yyyy-MM-dd");
return options;
}
}
{ field: "Date", title: "Date ", type: "date", format: "{0:dd/MM/yyyy}" }
result : 30/08/2012
kamal , I think this would be the solution and this how I solve it
parameterMap: function (options, operation) {
if (operation != "read") {
//change the fieldDate variable to your field date
var d = new Date(options.models[0].fieldDate);
options.models[0].fieldDate = kendo.toString(new Date(d), "yyyy-MM-dd");
return options;
}
}
it might help someone like me ...
I'm trying to create a countdown for an event. I'm using Jquery Countdown
I have this code:
$(function () {
var fecha = new Date("July 30, 2011 00:00:00");
$('#defaultCountdown').countdown({
until: fecha,
format: 'DHMS',
expiryUrl: "http://www.google.com",
serverSync: serverTime,
timezone: -4
});
});
function serverTime() {
var time = null;
$.ajax({
url: 'serverTime.php',
async: false,
dataType: 'text',
success: function (text) {
time = new Date(text);
},
error: function (http, message, exc) {
time = new Date();
}
});
return time;
}
The script is working fine, but when I try to change the clock date, the countdown changes.
Any idea why?
I imagine you created the serverTime.php file on your server?
http://keith-wood.name/countdown.html
Tab Timezones has the PHP code you'll need to add to serverTime.php for your script to use that. Also may want to fully qualify that to something like url: 'http:yourdomain.com/serverTime.php' But using that it should use your server time not your local PC time. If your server is on your local PC, then well... it would change.
I went to their site, with their example, and changing my system time affects their countdown as well. Their code relies on local system times.
What I see when you get the server time form the ajax call, it is not creating the JavaScript date object.
I searched and below worked for me.
// Split timestamp into [ Y, M, D, h, m, s ]
var t = "2010-06-09 13:12:01".split(/[- :]/);
// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
alert(d);
// -> Wed Jun 09 2010 13:12:01 GMT+0100 (GMT Daylight Time)