Clock widgets in PHP - 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>

Related

How to make a countdown timer that automatically restarts using PHP

I have a requirement to make a countdown timer that automatically restarts when the time has elapsed.
For example, I need to countdown from 3pm or 15:00 UK time and reset to start counting again when the time has reached.
I've been trying with some jQuery but that will show browser time and not server time. If anyone is able to share a solution please let me know.
Thanks in advance!
The code below is a working example from which works perfectly: https://gist.github.com/Majestik/3964527
if (document.getElementById('countdownTimer')) {
pad = function(n, len) { // leading 0's
var s = n.toString();
return (new Array( (len - s.length + 1) ).join('0')) + s;
};
function countDown() {
var now = new Date();
if ( (now.getDay() >= 1) && (now.getDay() <= 5) ) { // Monday to Friday only
var target = 15; // 15:00hrs is the cut-off point
if (now.getHours() < target) { // don't do anything if we're past the cut-off point
var hrs = (target - 1) - now.getHours();
if (hrs < 0) hrs = 0;
var mins = 59 - now.getMinutes();
if (mins < 0) mins = 0;
var secs = 59 - now.getSeconds();
if (secs < 0) secs = 0;
var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
document.getElementById('countdownTimer').innerHTML = str;
}
}
}
var timerRunning = setInterval('countDown()', 1000);
}
Time remaining: <span id="countdownTimer"><span>00:00.<small>00</small></span>
In your PHP code when you are writing the HTML that includes this javascript file just set the variables there rather than getting javascript to do it once it has loaded, this way you are using your server time rather than the browser time.

Jquery calculating time duration by giving start time and end time

I am using jQuery time picker to get start time and end time in 12hr format. I need to calculate time duration between start time and end time in HH:MM:SS format. I have the following code with me. But its returning duration like 1.1666. So what changes should I make in my code.
valueStart = $("#startTime").val();
valueStop = $("#endTime").val();
var diff = ( new Date("1970-1-1 " + valueStop) - new Date("1970-1-1 " + valueStart) ) / 1000 / 60 / 60;
var diffe = Math.abs(diff);
alert(diffe);
valueStart = $("#startTime").val();
valueStop = $("#endTime").val();
var str0="01/01/1970 " + valueStart;
var str1="01/01/1970 " + valueStop;
var diff=(Date.parse(str1)-Date.parse(str0))/1000/60;
var hours=String(100+Math.floor(diff/60)).substr(1);
var mins=String(100+diff%60).substr(1);
alert(hours+':'+mins);
Try it with xdate (javaScript Date Library)
try this if you want in HH:MM:SS format..
var diff =
new Date( '01/01/1970 ' + valueStop) -
new Date( '01/01/1970 ' + valueStart );
var sec_numb=(diff /1000)+"";
var hours = Math.floor(sec_numb / 3600);
var minutes = Math.floor((sec_numb - (hours * 3600)) / 60);
var seconds = sec_numb - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
var time = hours+':'+minutes+':'+seconds;
alert(time);

Trying to use jQuery Countdown with UNIX timestamp from php

As the title says I'm facing a problem displaying the right left time to an end date.
I'm using this jquery plugin: http://keith-wood.name/countdown.html
Let's assume that our timestamp is generated by a PHP script like this: $end_date = strtotime($row4['end']); // 2012-05-09 06:00:00 becomes 1336536000
I use this ($i is used inside a loop):
$('.count-<?php echo $i; ?>').countdown({until: new Date(<?php echo $end_date; ?> * 1000), layout: '{dn} days {hnn} hrs {mnn} min {snn} sec', compact: true});
Right now it's 06:21 AM here. Instead of the expected 1 day left result I get "1 days 17 hrs 04 min 21 sec".
What am I missing here?
I have been using the API for quite a while now, but I cann't seem to figure out what's up with your code. Perhaps (but just perhaps) you're using the untill property wrong.
My code that I usually use:
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $date_to * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#cool-countdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
Obviously you should extend this to feature singular.

How to get location based time?

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>")

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);
}
?>

Categories