S
S
Suren2018-06-07 17:28:20
C++ / C#
Suren, 2018-06-07 17:28:20

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
5b19401e318a5949147323.png
The script itself

<?php
  echo date("Y-m-d-H-i-s");
  ?>

I'm trying to get it in Unity using C#, the script gets everything, that is, it sees the site and the text.
My problem is that I can't properly format the record in order to interact with it somehow.
I do it like this
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"));
    }
}

Unity itself gives this error
5b194059f3797962913344.png
. I understand that the whole problem is in the format representation, but as far as I understand, I wrote the correct format, so the problem is in CultureInfo.InvariantCulture, but I don’t know another way (((
Please help))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2018-06-07
Jagaryan @saylar

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 question

Ask a Question

731 491 924 answers to any question