Compare PHP server time with JavaScript time (client browser time) - php

I am making a notification system which require the time since functionality.
I am using this script for timesince function(jQuery):
$.fn.updateSince = function(interval) {
var times = this.map(function(){ return { e: $(this), t: parseInt($(this).html()) }; });
var format = function(t) {
if(t > 86400) {
return Math.floor(t / 86400) + ' days ago';
} else if (t > 3600) {
return Math.floor(t / 3600) + ' hours ago';
} else if (t > 60) {
return Math.floor(t / 60) + ' minute ago';
} else if (t > 50) {
return '50 seconds ago';
} else if (t > 40) {
return '40 seconds ago';
} else if (t > 30) {
return '30 seconds ago';
} else if (t > 25) {
return '25 seconds ago';
} else if (t > 15) {
return '15 seconds ago';
} else if (t > 10) {
return '10 seconds ago';
} else {
return 'a few seconds ago';
}
}
var update = function(){
var now = new Date().getTime();
$.each(times, function(i, o){
o.e.html(format(Math.round((now - o.t) / 1000)));
});
};
window.setInterval(update, interval);
update();
return this;
}
The notifications are stored in MySQL database , each notification has a timestamp on it that is in this format: 2012-12-26 06:21:28
I use PHP strtotime and change it into UTC format when obtaining the time out of the database for each notificaiton item:(php)
$rows['time']= strtotime($temp_time." UTC");
When I pass it back to the client browser , using ajax. I will multiply the UTC time(from database) with 1000(javascript)
var time = parseInt(dbtime * 1000);
Then I can use the jQuery timesince function to turn it into time ago.
Problem:
My server time varies with client browser time.
How do I make them "compatible" with each other.
For example , a notification is made 2 hours ago according to client browser.
But PHP says its made 7 hours ago.
because client browser timezone is different with my server's
My logic is:
1. [PHP] Get current time , and use the notification time obtained from database to subtract with it.
database time - current time = time difference on server end
[PHP] Then we get a time difference. in this case 7 hours.
[PHP] How do i turn 7 hours into UTC format?
[Javascript] Send the current client browser time , also in UTC format to php script.
[PHP] client browser time - 7 hours(UTC?) = actual time of notification created for javascript use.
[PHP] Send back the results to Javascript , so now the time can be 2 hours ago on client browser's end.
How do I do this?
Just for info , UTC format is like(multiplied with 1000) : 1314952284779

Your logic could be:
Convert the notification time to UNIX timestamp using strtotime (or any similar function)
Use JavaScript Date function with this value (multiplied by 1000) to re-build the date
Use (new Date) - thatDate to calculate the difference
Convert to hours, minutes and seconds
The problem I see here is in the first step. You mention that the dates are stored as 2012-12-26 06:21:28 but what timezone does this date correspond to? If it is the same time as the server timezone then using strtotime("2012-12-26 06:21:28") is sufficient to generate correct timestamp.

i had the same problem to i store the ip of the user in the db and then convert that to location and get the timezone then uses this functionality. main reason of doing that is that i future you might me considering to get info about your user so that approach also gonna help then. You can use interval triggered function to run the code
j(document).ready(function () {
window.initial = 0;
window.setInterval(timeincrement, 60000);
function timeincrement() {
initial = initial + 60;
j('.coment').each(function () {
var timefromserver = j(this).find('input[type=hidden]').val();
timefromserver = parseInt(timefromserver);
if ((timefromserver / 216000) < 1) {
var secgone = timefromserver + initial;
var timeelasped = secgone / 60;
if (Math.floor(timeelasped) < 1) {
j(this).find('.lightgrey').text("Few seconds ago");
} else if (Math.floor(timeelasped) < 60) {
var innertext = timeelasped + " minutes ago";
j(this).find('.lightgrey').text(innertext);
} else if (Math.floor(timeelasped / 60) > 0) {
var hourgone = Math.floor(timeelasped / 60);
var innertext = hourgone + " hours ago";
j(this).find('.fdlightgrey').text(innertext);
}
}
});
}
});

For your info new Date().getTime() in JavaScript return UTC time, as far as the client clocks are right this means that you don't have to deal with different time zones of users, all you have to do is to provide the times from your server in UTC.
In PHP you should either use the gmdate function to create UTC time strings or simply don't format it and just ship the raw UNIX UTC time that you get from the time function.
In short UTC everywhere, then it works. As a bonus it works like daylight savings didn't exist.
You could also also do an adjustment to account for incorrect times on the client by sending a "now" time stamp when the page is first loaded, calculate how long ago that is on the client, and subtract this result from all future calculations. But given that practically all computers automatically adjust their clock to that of a time server it is quite rare that a computer clock is off by more than a few seconds.

if mysql data is UTC, i use Date.getTimezoneOffset() in JavaScript and apply it (offset is in minutes: -120 = GMT+2) to the time stored in mysql (obtained via php) when displaying historical time (when event happened) for a user.
RAMBLINGS:
also serves to compare current time with mysql data (never trust a user's clock, but their timezone is usually correct [except for frequent-flyers' laptops, ouch]).
i take care to tell the user what her timezone difference is (either by publishing GMT +/- XX or something like "HH:MM this should be your local time, please fix clock if it isnt") and asking them to confirm it, if necessary. your code will deal with the situation according to your relationship with user ("fix that clock, darnit" or "yes, hmmmm, would you like us to fix our clocks to fit paris time [though we are in argentina], madame?")

Related

countdown php jquery post ajax

I'm designing a website where there is a little game going on. Each user that participates has 99 minutes to complete it otherwise we display a game over state. Here is what I managed to do so far using jQuery post.
I've been able to display the timer. In the PHP I've set the target time to 99*60 seconds but I cant set the starting time to 0 as I'll do the difference beteen those to values for the countdown.
Furthermore, as each user is able to quit the page I want to be able to store the time when they left. All I am able to do is store the countdown value, let's say 5845, in the DB when they log off. Though I tried updating the table with each call to the jQuery post, it just makes it worse.
Here is my jQuery:
function countdown() {
var i = (new Date().getTime() / 1000) + (99 * 60);
setTimeout(function () {
$.post(\'countdown.php\',{target:i},function(data){
$('#countdown').html(data);
});
countdown();
},1000);
}
countdown();
Here is the PHP:
if (!empty($resm['Countdown']) || $resm['Countdown'] >= 0) {
$target = (99 * 60);
$countdown = ($target - $current);
$_SESSION['currenttime'] = $countdown;
$hours = floor($countdown / 3600);
$min = floor($countdown / 60);
$r_min = floor(($countdown - ($hours * 3600)) / 60);
$sec = floor($countdown - ($min * 60));
if ($min == 0) {
echo $target.' '.$current.' '.$countdown;
echo '<br/>'.$min.' minutes '.$sec.' seconds left';
} else {
echo 'Time Over';
$sql = "UPDATE bs10000099 SET Upgradedlevel='2',Activated='2',Countdown='5940' WHERE MemberID='$memberid'";
mysql_query($sql);
}
}
I cant figure out how to set the starting time or how to prevent countdown() restart on refresh page.
I'm not sure what kind of security you want, but getting time from Javascript is not really secure since JS gets the time from the computer instead of the server.
With your example, I would be able to play the game, wait until there's 10 minute left, and just roll back time on my desktop and it will reset. Or roll back a year and have 100000 minutes left!
I suggest you use server time.
Here is what I would do:
When the test starts, get the UNIX time + 90 minutes. This will give you the final time.
PHP
if (!isset($_SESSION['end'])) {
$_SESSION['end'] = strtotime("+90 minutes");
}
$remaining = $_SESSION['end'] - time();
if ($remaining > 0) {
echo json_encode(array(
"remaining" => $remaining
));
} else {
// finished! write code here.
}
You can then do a simple $.getJSON() to get the remaining seconds and display it.
Since you kept the "end" time, even if the users leave, the timer will continue.

Countdown 10 min, run a .php file, then restart

My question is a simple yet I cannot find a way around it. I need a server time countdown script to run for 10 minutes, run a php file and when the countdown ends restart again.
I have read the jQuery scripts but all of them provide me with a countdown to a speciffic date then stop.
Can anyone point me to the right direction to find such a script?
This is what I have
<style style="text/css">
.lcdstyle{ /*Example CSS to create LCD countdown look*/
background-color:black;
color:lime;
font: bold 18px MS Sans Serif;
padding: 3px;
}
.lcdstyle sup{ /*Example CSS to create LCD countdown look*/
font-size: 80%
}
</style>
<script type="text/javascript">
function cdLocalTime(container, servermode, offsetMinutes, targetdate, debugmode){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
this.localtime=this.serverdate=new Date(servertimestring)
this.targetdate=new Date(targetdate)
this.debugmode=(typeof debugmode!="undefined")? 1 : 0
this.timesup=false
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
}
cdLocalTime.prototype.updateTime=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}
cdLocalTime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}
cdLocalTime.prototype.showresults=function(){
var thisobj=this
var debugstring=(this.debugmode)? "<p style=\"background-color: #FCD6D6; color: black; padding: 5px\"><big>Debug Mode on!</big><br /><b>Current Local time:</b> "+this.localtime.toLocaleString()+"<br />Verify this is the correct current local time, in other words, time zone of count down date.<br /><br /><b>Target Time:</b> "+this.targetdate.toLocaleString()+"<br />Verify this is the date/time you wish to count down to (should be a future date).</p>" : ""
var timediff=(this.targetdate-this.localtime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=debugstring+this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=debugstring+this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}
/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////
//Create your own custom format function to pass into cdLocalTime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left
//The values of these arguments may change depending on the "baseunit" parameter of cdLocalTime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc
//1) Display countdown using plain text
function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring="<span style='background-color: #CFEAFE'>"+arguments[2]+" minutes "+arguments[3]+" seconds</span> left until launch time"
}
else{ //else if target date/time met
var displaystring="Launch time!"
}
return displaystring
}
//2) Display countdown with a stylish LCD look, and display an alert on target date/time
function formatresults2(){
if (this.timesup==false){ //if target date/time not yet met
var displaystring="<span class='lcdstyle'>"+arguments[2]+" <sup>minutes</sup> "+arguments[3]+" <sup>seconds</sup></span> left until launch time"
}
else{ //else if target date/time met
var displaystring="" //Don't display any text
alert("Launch time!") //Instead, perform a custom alert
}
return displaystring
}
</script>
<div id="cdcontainer"></div>
<script type="text/javascript">
//cdLocalTime("ID_of_DIV_container", "server_mode", LocaltimeoffsetMinutes, "target_date", "opt_debug_mode")
//cdLocalTime.displaycountdown("base_unit", formatfunction_reference)
//Note: "launchdate" should be an arbitrary but unique variable for each instance of a countdown on your page:
var launchdate=new cdLocalTime("cdcontainer", "server-php", 0, "April 28, 5012 00:05:00", "debugmode")
launchdate.displaycountdown("days", formatresults2)
</script>
Any ideas?
It seems like what you want is a cron job; which can execute a script at predefined times or intervals. Setting up a cron depends a little bit on the server environment in which you are executing it.
To update using a countdown script in JavaScript:
var count = 600;
function hitPhpScript() {
var xhr = new XMLHttpRequest();
xhr.open("get", "myscript.php", true);
xhr.send(null);
startCountdown();
}
function startCountdown() {
count = 600;
doCountdown();
}
function doCountdown() {
count--;
if (count > 0) {
document.getElementById("countdown_label").innerHTML = count + " seconds left";
setTimeout("doCountdown()", 1000);
} else {
hitPhpScript();
}
}
This is the chunk that initializes the clock to 30 minutes on page load:
$date = date('i');
$sec = date('s');
$diff = ($date < 30) ? 1800 - (($date * 60) + $sec) : 3600 - (($date * 60) + $sec); //set diff to run split on the half hour.
$hld_diff = $diff;
What's happening here is
$date = date('i') //is taking the minutes portion of the current time
$sec = date('s') //is taking the seconds portion of the current time
The next line is saying if the number of minutes is less than 30, set $diff to 1800 seconds (i.e. 30 minutes) minus the current time minutes and seconds. In other words - set the $diff value to the number of seconds until the current time will hit 00 mins 00 secs. If the number of minutes is greater than or equal to 30, do a similar thing but setting $diff to the number of minutes and seconds until the current time minutes and seconds reaches 30 and 0 respectively.
So if you want to use this same code but want to set it to 10 minutes, first you have to decide what number of minutes on the clock you want the countdown to end. To keep it simple we'll say it will end on 0, 10, 20, 30, 40, and 50.
Therefore you could do something like this:
if($date < 10)
$diff = 600 - (($date * 60) + $sec);
else if($date < 20)
$diff = 1200 - (($date * 60) + $sec);
else if($date < 30)
$diff = 1800 - (($date * 60) + $sec);
else if($date < 40)
$diff = 2400 - (($date * 60) + $sec);
else if($date < 50)
$diff = 3000 - (($date * 60) + $sec);
else
$diff = 3600 - (($date * 60) + $sec);
That should be all you need (unless I've overlooked something)
its a countdown for a game, i need a visual countdown to when the cash or whatever is gonna be added.
You wouldn't usually script this using an "actual" countdown. The logic is simple enough to program in an "non-realtime" fashion. The next time the user logs on/refreshes the page/does something, you can run all the calculations that are necessary to provide the user with as much money as he should have received since the last time he looked at the page. You do not actually have to give the user the money exactly after 10 mins.
For such games, you'd usually have a cron job/daemon running every minute or so that recalculates all amounts of all users in this way. The visual countdown is just a gimmick on the client-side.

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

Calculating the time difference in an PHP/MySQL/JavaScript system

I was wondering what the best way is to calculate the difference in time from now to a certain point, let's say the countdown time.
I have an auction that has a closetime at a certain point, this time is stored in a MySQL record in the format " DATETIME 00-00-000 00:00:00 ". This record is called closetime.
Now on my website I have JavaScript code that gets this time via a PHP file. The JavaScript loops every second using setInterval 1000. The PHP file gets the closetime from the db, and sends it back in this format
strtotime($result['closetime']);
And I get the time of the request, I want to use the server time, and not the time in JavaScript, because the clock of the user can be off.
strtotime(date("H:i:s", $_SERVER['REQUEST_TIME']))
I send back these two timestamps and calculate the time difference between them in JavaScript. I use this function to do it, the values send back from PHP I call currentTime and closeTime, I think this should be clear.
function auctionDelayTime(currentTime,closeTime){
totaldelay = closeTime - currentTime;
if(totaldelay <= 0){
return 'ended';
}else{
if( days=parseInt((Math.floor(totaldelay/86400))) )
totaldelay = totaldelay % 86400;
if( hours=parseInt((Math.floor(totaldelay/3600))) )
totaldelay = totaldelay % 3600;
if( minutes=parseInt((Math.floor(totaldelay/60))) )
totaldelay = totaldelay % 60;
if( seconds=parseInt((Math.floor(totaldelay/1))) )
totaldelay = totaldelay % 1;
return hours+':'+formatTimes(minutes)+':'+formatTimes(seconds);
}
}
function formatTimes(value){
return value < 10 ? '0'+value : value;
}
I think this is an awful lot of code do something so simple. Does anyone have a better solution or maybe more 'beautiful' code.
Enjoy!
There is a jquery Countdown Plugin that supports server sync through AJAX:
From the docs:
Synchronise the client's time with
that of the server by providing a
function that returns the current
server date and time. This date and
time should take into account the
server's timezone and any difference
between that time and the client's is
applied to the countdown when it is
started or changed.
The following example uses a PHP
program on the server to return the
current server time in a format that
can be used directly by the JavaScript
callback. You should make sure that
your server call is synchronous.
$(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;
}
serverTime.php:
<?php
$now = new DateTime();
echo $now->format("M j, Y H:i:s O")."\n";
?>
Use Date object.
var d = new Date(difference_in_milliseconds);
var seconds = d.getSeconds();
var minutes = d.getMinutes();
var hours = d.getHours() - 1; //See note
Note: Strangely, hours value is bigger by one than I would expect for reason I don't understand. It looks like "midnight Jan 1, 1970" was at 1 AM :-)
UPDATE: The difference of 1 is due to the offset of my timezone (GMT +1).
Slight change that will solve this:
var d = new Date();
var offset = d.getTimezoneOffset() * 60000;
var d = new Date(difference_in_milliseconds + offset);
var seconds = d.getSeconds();
var minutes = d.getMinutes();
var hours = d.getHours();
This JavaScript library is easy to use and will likely serve you well.
Why not have the php page give you the difference?
$sql = "SELECT UNIX_TIMESTAMP(NOW()) as currentTime, UNIX_TIMESTAMP(closeTime) as closeTime FROM yourTable WHERE yourRecordId = '123'";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);
echo timeRemaining($result['currentTime'], $result['closeTime']);
function timeRemaining($start, $end) {
$dateDiff = $end - $start;
if ($dateDiff <= 0) { return 'Ended'; }
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
return "Ending in $fullDays days, $fullHours hours and $fullMinutes minutes.";
}
Do you really have to get the time through AJAX every 1000 ms?
(i suppose that you're hoping for closetime changes? ...)
But if you really must do it this way, i'd suggest getting the time difference directly in MySQL for code simplicity.
$sql = "SELECT TIMEDIFF(NOW(), `CreateTime`) from `Table` WHERE `id`=1;"

Categories