SA-MP Forums Archive
Convert date to timestamp - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Convert date to timestamp (/showthread.php?tid=649333)



Convert date to timestamp - BalkanEliteRP - 07.02.2018

Hi

I was use INI for saving my account's.Now i'm transfer it in MySQL.
I have date of register written in this format:

DatumRegistracije = 2/2/2018 u 11:49

I want to know,is it possible to convert into timestamp format( For example 7/2/2018 15:53 is: 1518000829

Because it's easier contol,i can make some statistic for example( Number of account which are registered in last month,last day,last year etc,etc.With this format i can't do it.

I have many accounts so..It's not very important for me but if there have way it will be nice..

Sorry for my English

Thanks


Re: Convert date to timestamp - KayJ - 07.02.2018

What you can do:
1. Make them read in format x/x/xxxx and also make them read in format xxxxxx.
2. Overwrite those by change onplayerdisconnect by using your custom format.

I don't know what other way you can do but let people try.


Re: Convert date to timestamp - zMason - 07.02.2018

Unless you can come up with a formula, which will be extremely hard to do I'd say leave it as it is. The second value that you want is what you'd get from gettime();. This method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.


Re: Convert date to timestamp - Bolex_ - 07.02.2018

Yea, It's possible. I did for myself somethink like this.

Registration Respone
Код:
e_Info[ playerid ][ RegisterDate ] = gettime();
Код:
Registered On: %s",  timestampToDate(e_Info[ playerid ][ RegisterDate ])
Код:
stock timestampToDate(datetime, style = 0) 
{
	new string[64];
	static tm<timestamp>;
	localtime(Time:datetime, timestamp);

	switch(style)
	{
	    case 0:	strftime(string, 64, "%d/%m/%Y - %X", timestamp);
	    case 1:	strftime(string, 64, "%d/%m/%Y", timestamp);
	}
	return String;
}
Result



Re: Convert date to timestamp - BalkanEliteRP - 07.02.2018

@Bolex_
I know for it..But i have it yet in this format: 2/2/2018 u 11:49 ...I need to convert my old date format to timestamp

Quote:

Hrvatski(Croatian)
Pošto mi Engleski i nije najjača strana; Već imam zapisane datume igrača u formatu "2/2/2018 u 11:49" i trebam već postojeće podatke prebaciti u timestamp format zbog lakše kontrole.




Re: Convert date to timestamp - Bolex_ - 07.02.2018

That's why you have to built in MySQL and update it on next registration/login.


Re: Convert date to timestamp - BalkanEliteRP - 07.02.2018

haha..
So what if someone is registered 2/2/2018..How i can write it in timestamp format? I know for new registration,it's ok.But i'm ask can i old data convert into timestamp..


Thanks


Re: Convert date to timestamp - NaS - 07.02.2018

What Bolex meant is utilizing MySQL to do the job for you. But you can also use this genius formula:

Код:
datetime_to_unix(year, month, day, hour, minute, second)
{
	new jday = 367 * year - 7 * (year + (month + 9) / 12) / 4 - 3 * ((year + (month - 9) / 7) / 100 + 1) / 4 + 275 * month / 9 + day + 1721029 - 2440588;

	return jday * 86400 + hour * 3600 + minute * 60 + second;
}
jday is the julian day (so the number of days passed since the beginning of unix), the lower part adds the day-time in seconds (which is the easiest part, as you can see).

It's working perfectly fine, respecting leap years etc.
It can be shortened a lot but you can do that yourself if you want.


Re: Convert date to timestamp - BalkanEliteRP - 07.02.2018

Thank you very much NaS.That's it


Re: Convert date to timestamp - Gammix - 07.02.2018

https://github.com/Crayder/Time-Conversion