Answer the question
In order to leave comments, you need to log in
How to determine server time?
The bottom line is, I made a local server using XAMPP and Apache, made a small PHP script that shows the time on the page, it looks like this
The script itself
<?php
echo date("Y-m-d-H-i-s");
?>
public class ServerTime : MonoBehaviour
{
private string URL = "http://localhost/";
// Use this for initialization
void Awake ()
{
StartCoroutine(CheckTimeOnline());
}
private IEnumerator CheckTimeOnline()
{
DateTime timeNow = DateTime.Now;
Debug.Log("ВРЕМЯ СЕЙЧАС: " + timeNow);
WWW www = new WWW(URL);
yield return www;
if (www.error != null)
{
Debug.Log("Ошибка " + www.error);
yield break;
}
TimeSpan ts;
Debug.Log(www.text);
string time = www.text;
string format = "yyyy-MM-dd-HH-mm-ss";
//var puk = DateTime.Parse(www.text, out timeNow);
print(string.Format("Прошло: {0} дней, {1} часов, {2} минут, {3} секунд", timeNow.Day, timeNow.Hour, timeNow.Minute, timeNow.Second));
if (PlayerPrefs.HasKey("TimeNow"))
{
ts = DateTime.ParseExact(time, format, CultureInfo.InvariantCulture) - DateTime.ParseExact(PlayerPrefs.GetString("TimeNow"), format, CultureInfo.InvariantCulture);
//Debug.Log(string.Format("Прошло: " + ts.Days + ts.Hours + ts.Minutes + ts.Seconds));
print(string.Format("Прошло: {0} дней, {1} часов, {2} минут, {3} секунд", timeNow.Day, timeNow.Hour, timeNow.Minute, timeNow.Second));
}
PlayerPrefs.SetString("TimeNow", www.text);
Debug.Log(PlayerPrefs.GetString("TimeNow"));
}
}
Answer the question
In order to leave comments, you need to log in
www.text;
here is the text you are sure that you have there 2017-06-07 arrives. not 2017-6-7. which actually will cause a format mismatch ..
well, in general. make the time from the server in UTC format. Long is a variable with the number of seconds..and revisit the standard method without format.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question