Convert date to timestamp
#1

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
Reply
#2

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.
Reply
#3

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.
Reply
#4

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
Reply
#5

@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.

Reply
#6

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

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
Reply
#8

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.
Reply
#9

Thank you very much NaS.That's it
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)