How to get location based time? - php

i am working on javascript which displays time based on location, that is if a user logs on from china it should display their local time or a user from india it should display their code.No matter what, i am not able to get the code.pls someone help.

You can get the users local time in JS with:
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
document.write("<b>" + hours + ":" + minutes + " " + "</b>")

Related

How to make Javascript use time from the world clock (GMT to be specific)

I am trying to create a Javascript clock that will display the current Actual time, rather than displaying the local time of the client machine.
The clock is part of a script which calculates the difference in time between two values (in php but the purpose of this clock is to give a visual representation). It is important that the user of the script cannot change the time that is displayed as this will produce incorrect results for the outcome of the whole program.
I realise that i may have to use a php function to return the time in a particular time zone but i do not understand how to input this into my script. My Javascript clock code is below (currenttime_large is the ID of the DIV container of this script) :
<script type="text/javascript" language="javascript">
function renderTime() {
var currentTime = new Date();
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();
setTimeout('renderTime()',1000);
if (h == 0) {
h = 12;
}
if (h < 10) {
h = "0" + h;
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
var myClock = document.getElementById('currenttime_large');
myClock.textContent = h + ":" + m + ":" + s + " ";
myClock.innerText = h + ":" + m + ":" + s + " ";
}
renderTime();
</script>
Please could someone advise how this script can be adjusted so that the clock which is displayed, displays GMT(London) time rather than the client time.
Many thanks in advance,
Aidan
Use the getUTC... methods:
var h = currentTime.getUTCHours();
var m = currentTime.getUTCMinutes();
var s = currentTime.getUTCSeconds();
I've been looking over something like this,
The best solution I've found is this
http://www.jqueryscript.net/demo/jQuery-Based-Analog-And-Digital-World-Clock-jClocksGMT-js/
If you want you can save the whole page, or download necessary files if they not saved automatically. Then in your html code you just change the GMT timezone according to ID.
That's it
I was searching of world analog clock. i found one source/example which is basically cretated with jQuery, Html 5, CSS 3 and canvas.
Source : https://github.com/php-dedicated-developers/world-clock
It can be done without PHP. I ve done it using simple HTML and JavaScript calculations.
Note: This clock IS NOT UNIVERSAL.
Because i live in india, i fixed all parameters according to Indian GMT (5:30).
If someone wants to use it who does not live in India will have to customize parameters according their own country GMT
How my code works? Here is your answer
First we have created a simple timer program.
Passed the time argument to a function called london()
3.in function london() everything is processed. I have put GMT of India. using the parameters function london is producing correct London time from Indian time and given GMT time
Note:
1. As this code is not universal.. people out of India will have to customize it as they want...
2.This code uses device time and processes correct GMT 0 time. Example my code works substracting 5 hours and 30 minutes from device time because my GMT is 5:30 (India)
3.Will work fine offline
4.If someone changes country(GMT) they will have to customize code again
Full Code
<body onload="timer()">
<p id="txt1"></p>
<p id="txt2"></p>
<script>
function timer(){
//Getting Device Time
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//Returning value of ckeckTime();
m = checkTime(m);
h = checkTime(h);
s = checkTime(s);
var trigger = setTimeout(timer,1000);
document.getElementById("txt1").innerHTML = h+" "+m+" "+s;
london(h,m,s); //Passing device time to function london
}
function london(h,m,s){
//This function produces london time
//var h (Device hour)
// m (Device minute)
// s (Device second)
defhr = 5; //define hour
defmn = 30; //define minutes
/* defhr = 5 and defmn = 30 according to India GMT +5 30*/
if(m < defmn){ //When device minute is less than define minutes
defhr += +0
var hour = h - defhr;
let x = m - defmn;
var min = 60 + x;
} else {
defhr += -1
var hour = h - defhr;
var min = m - defmn;
}
if(h < defhr){ //When device hour is less than define hour
let y = defhr - h;
var hour = 24 - y;
}else {
var hour = h - defhr;
}
// returning value of checkTime();
hour = checkTime(hour);
min = checkTime(min);
document.getElementById("txt2").innerHTML = hour+" "+min+" "+s;
}
//checkTime function adds 0 in front a number when it is less than 10
function checkTime(i){
if(i<10){ i="0"+i; }
return i;
}
</script>
</body>
</html>
Tip:
1. If you live in western countries or GMT value is negative then put negative value in defhr and defmn section.
If anyone is using this code in GMT + 5:30 (India)
They will not need customization.
Make sure your code is producing correct London GMT time.. Grab notebook,calculator or you can test it with online world clock too

Clock widgets in PHP

I need to show system date in my web site through wordpress in PHP.
it shows system time one time but not updating as days gone passed.
I need to change it according to my system date
You can't show your system time using PHP. If you want to show your system time you just need to use javascript.
try this,
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes < 10)
minutes = "0" + minutes
document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
Update:
If you want to show everything, please try this.
<script type="text/javascript">
document.write(new Date());
</script>

Client time settings broke readable date in javascript

What i'm trying to do is show date in more readable format like 3 seconds ago, 1 hours ago, yesterday, June 12 and etc.
What i did is , first calculate the timestamp for comment's date.. Then send it to client with ajax, then create two date instance on javascript one for is comment's date and other one is current date.. After that I just find differences between to date item then write on the screen with fancy words..
Everything works fine on localhost, even server.. But sometimes if client's pc date is earlier from the server date (independent from time zone)..
Let say server time is today 13.30 pm and client time is today 13.00 , it's failing with this scenario because current time is being comment's post time.. Difference will be negative value..
I'm creating date object for comment in php like this;
date("Y-m-d G:i:s")
Then write it to mysql db..
After that when i select comments i convert it to timestamp to push it on client side with ;
$comment['timestamp'] = strtotime($row['creationDate']);
And then in javascript, I make calculation for human readable date format ;
DateObject.getFormatted = function(unixtime){
d = new Date(unixtime*1000);
now = new Date();
var diff = (now.getTime()/1000- unixtime) ;
var result = "";
var MIN = 60,
HOUR = 3600,
DAY = 86400;
if(diff < 2)
result ="just now";
else if(diff < MIN)
result = Math.floor(diff) + " seconds ago";
else if(diff < HOUR)
result = Math.floor(diff/60) + " minutes ago";
else if(diff < DAY)
result = Math.floor(diff/3600) + " hours ago";
else if(diff < DAY*3)
{
var days = diff/DAY;
if(days < 2)
result = "yesterday";
else
result = Math.floor(days) + " days ago";
}
else if(now.getFullYear() == d.getFullYear())
{
formattedTime = this.getTime(d);
result = this.getSameYear(d) + " at " + formattedTime;
}
else
{
formattedDate = this.getDate(d);
formattedTime = this.getTime(d);
result = formattedDate + " at " + formattedTime;
}
return result;
};
So if clients date is earlier then the comment's date diff value is being negative, so the first case would be true
if(diff < 2)
result ="just now";
It's going behing as time difference between client and comment's date .. In my case 10 minutes.. If I set my computer time 10 minutes later it's working nice..
So how can i fix it in a better way?
Thank you.
May be better send from server to client difference between comment date and current date?(instead of creation two javascript date instances)
A third date so you can calculate the difference between server and client and use it as an offset?

How can I get the user's local time instead of the server's time?

How can I get the time of the client side?
When I use date() it returns server's time.
Here's a "PHP" solution:
echo '<script type="text/javascript">
var x = new Date()
document.write(x)
</script>';
As mentioned by everyone PHP only displays server side time.
For client side, you would need Javascript, something like the following should do the trick.
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
document.write("<b>" + hours + ":" + minutes + " " + "</b>");
And if you want the AM/PM suffix, something like the following should work:
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");
Here is a list of additional JavaScript Date and Time functions you could mess around with.
You could possibly use Geolocation by IP Address to work out which country the user is in, and then use that.
But using Javascript or letting the user choose a Timezone will probably be better.
As PHP runs on the server-side, you cannot access the client-side time from PHP : PHP doesn't know much about the browser -- and you can have PHP scripts that run without being called from a browser.
But you could get it from Javascript (which is executed on the client-side), and, then, pass it to PHP via an Ajax request, for example.
And here are a couple of questions+answers that might help you getting started :
Automatically detect user’s current local time with JavaScript or PHP
How can I determine a web user’s time zone?
PHP is server side only as far as i know.
You maybe want to use JavaScript.
As other's have mentioned, you can use Geo Location Services based on the IP Address.
I found it to be off by about 18 seconds due to IP location accuracy, by using a $responseTime offset it helped me narrow it down to 2 second accuracy in the Viewers Location.
<?php;
echo deviceTime('D, M d Y h:i:s a');
function deviceTime($dateFormatString)
{
$responseTime = 21;
$ip = (isset($_SERVER["HTTP_CF_CONNECTING_IP"])?$_SERVER["HTTP_CF_CONNECTING_IP"]:$_SERVER['REMOTE_ADDR']);
$ch = file_get_contents('https://ipapi.co/'.$viewersIP.'/json/');
$ipParts = json_decode($ch,true);
$timezone = $ipParts['timezone'];
$date = new DateTime(date('m/d/Y h:i:s a', time()+$responseTime));
$date->setTimezone(new DateTimeZone($timezone));
return $date->format($dateFormatString);
}
?>

PHP Time Remaining in Current Day

How can we find out how many time remains to end the current day from current time[date('Y-m-d H:i:s')] in PHP.
For example with a combination of mktime() and time():
$left = mktime(23,59,59) - time() +1; // +1 adds the one second left to 00:00
Update:
From Simon's suggestion, mktime(24,0,0) works too:
$left = mktime(24,0,0) - time();
$time_in_seconds = (24*3600) - (date('H')*3600 + date('i')*60 + date('s'));
Calculates the total seconds of one day and subtracts the seconds passed until the current hour of the day.
In Javascript, for anyone interested (takes care of timezone differences) :
var now=new Date();
var d=new Date(now.getYear(),now.getMonth(),now.getDate(),23-now.getHours(),59-now.getMinutes(),59-now.getSeconds()+1,0);
document.write(checkTime(d.getHours()) + ':' + checkTime(d.getMinutes()) + ':' + checkTime(d.getSeconds()) + ' time left');
function checkTime(i) {
if (i<10)
{
i="0" + i;
}
return i;
}

Categories