- Home /
System.DateTime.now and how to avoid time cheats
Hi,
I want to reward my player if he comes back to my game every day.
The obvious solution for this would be something like:
DateTime currentTime = DateTime.Now;
TimeSpan ts = currentTime - Convert.ToDateTime (GameParameters.lastPlayDate);
if (ts.TotalHours > 24)
{
Debug.Log ("Welcome back, here's a prize for you!");
}
But, this approach can be exploited by changing the time and date on my device. I'd like to avoid that, I've look for an answer here, in Unity forums and in game dev in general and I couldn't find any answer...
Is there any way to avoid this cheat at all? Either locally or, say, getting time and date from a server via Unity?
Thanks in advance!
Answer by reddragonpradeep · Jun 16, 2017 at 10:29 AM
For Local use in Android use this method to get system elapsed Time , independent of System Date And Time
C#
public static long ElapsedTime(){ if (Application.platform != RuntimePlatform.Android) return 0; AndroidJavaClass systemClock = new AndroidJavaClass("android.os.SystemClock"); return systemClock.CallStatic<long>("elapsedRealtime"); }
Answer by Vavius · Aug 01, 2014 at 06:25 AM
I've published an asset to solve this task locally on mobile platforms. With the help of native system code it tracks down device date and time changes and calculates unbiased time.
Works only on real Android and iOS devices. It is useful even if you use server timestamps, since user can turn off connection and cheat.
http://forum.unity3d.com/threads/system-datetime-now-and-how-to-avoid-time-cheats.124282/
I was surprised while testing this function with Crossy Road and it just works as you said in the plugin. Not sure if the developer uses this plugin too...
I force quit the game, switch on flight mode, set time become 1 hour later, back to the game and the free gift time still the same!
Will definitely take a look for the plugin if it working like Crossy Road!
Answer by MorleyInsane · Mar 01, 2012 at 04:28 AM
Have your game ping a server to get the time. So if you are serving content (like news and such)to the game, use the server for that service to get the time. If they can't connect to the server (no connection, or site is down) they don't receive the reward(possibly build in a recheck on next launch?)
That works, but then you have to consider server load if your game gets popular. (A nice problem to have I suppose! :) )
Answer by jnc1 · Feb 25, 2014 at 01:18 PM
Hello,
You can work with a database containing a last_logged for your players and create a php server, here is an example : http://wiki.unity3d.com/index.php?title=Server_Side_Highscores#Step_3:_The_Unity_HSController_Script
With php you can get the server time so like this even if the player change is time locally this will not have any impact.
Of course you need to check if he can get his reward in the php file :
if [time] > 0 and [time] <= [lastlogged] {
// send reward
} else {
// no reward for you
}
Like this, if the player connects after 00:00 server time you can send him a notification of reward
Sorry for this "code" because that is no code but working progress. Actually I plan to do a system reward in my project, it is not already implemented but I know how it will process.
I hope for you that you already found a way, cause it is a 2 years old post, but I was passing by ^^
edit : WWW can get the content of the url specified, so printing the response with 'echo' is sufficient
Answer by Leaton · Nov 18, 2017 at 10:27 PM
I use the internet time to keep my time and date the same across all devices. http://leatonm.net/unity3d-internet-time-date-setup-guide-stop-time-cheat/
Your answer
![](https://koobas.hobune.stream/wayback/20220613064302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why in game time is correct in dev env. but not in browser 1 Answer
How to make a date/time system with a fast forward feature? 2 Answers
Save current time on quit, then on relaunch compare to new current time and get difference? 3 Answers
How can I track how much time has passed since a game was turned off? 2 Answers