SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 26/06/2020, 11:30 AM   #1
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 990
Reputation: 107
Default Unix to human time

Hello, in my script i save the registration date as unix timestamp, I now want it to show in /stats command in a human readable date. Is there a way to convert this unix timestamp to something like: 26/06/2020?

Thanks.
Symon is offline   Reply With Quote
Old 26/06/2020, 11:35 AM   #2
xRadical3
Gangsta
 
Join Date: May 2017
Location: Iran
Posts: 691
Reputation: 63
Default Re: Unix to human time

Try this function:
pawn Code:
stock MyTimeStamp(timestamp, &year, &month, &day, &hour, &minute, &second)
{
    second = timestamp % 60;
    minute = timestamp / 60;
    hour = minute / 60;
    if(minute > 59) minute %= 60;
   
    day = hour / 24;
    if(hour > 23) hour %= 24;
   
    month = day / 30;
    if(day > 29) hour %= 30;
   
    year = month / 12;
    if(month > 11) month %= 12;
    return 1;
}
xRadical3 is offline   Reply With Quote
Old 26/06/2020, 11:40 AM   #3
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Unix to human time

This should help.
https://forum.sa-mp.com/showthread.php?t=347605
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 26/06/2020, 03:13 PM   #4
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 990
Reputation: 107
Default Re: Unix to human time

Quote:
Originally Posted by xRadical3 View Post
Try this function:
pawn Code:
stock MyTimeStamp(timestamp, &year, &month, &day, &hour, &minute, &second)
{
    second = timestamp % 60;
    minute = timestamp / 60;
    hour = minute / 60;
    if(minute > 59) minute %= 60;
   
    day = hour / 24;
    if(hour > 23) hour %= 24;
   
    month = day / 30;
    if(day > 29) hour %= 30;
   
    year = month / 12;
    if(month > 11) month %= 12;
    return 1;
}
What do i have to place in year, month and so on? I just got the unix timestamp.

Quote:
Originally Posted by Proxus View Post
Thanks but i don't wanna add an extra include for just a function that i'll use once.
Symon is offline   Reply With Quote
Old 26/06/2020, 03:22 PM   #5
coool
Gangsta
 
coool's Avatar
 
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 637
Reputation: 64
Default Re: Unix to human time

Quote:
Originally Posted by Symon View Post
What do i have to place in year, month and so on? I just got the unix timestamp.
Variables in which you want to store them, these are called reference variables, and the phenomenon is called 'call by reference'
Code:
new year, month, day, hours, minutes, second;
MyTimeStamp(gettime(), year, month, day, ...);
//now you have the current year stored in var `year` and so on.
__________________
coool is offline   Reply With Quote
Old 26/06/2020, 04:00 PM   #6
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,859
Reputation: 1693
Default Re: Unix to human time

You can select unix timestamp to date aswell through MySQL using FROM_UNIXTIME(). Example;

Code:
SELECT username, FROM_UNIXTIME(reg_date) AS reg_date FROM users WHERE id = 1;
Eg. if you'd have a record for userid 1, username being Kwarde, and reg_date being 1593179397 it would return
Code:
//#    [username]    [reg_date]
//0    Kwarde        2020-06-26 15:49:57
The opposite function is UNIX_TIMESTAMP()
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009
Kwarde is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UNIX time limit. Dayrion Scripting Help 2 07/11/2016 04:25 PM
[Tutorial] Convert Unix time (Timestamp) to Normal time in Pawn mrsh Tutorials 2 15/09/2016 12:53 PM
Unix time help Ahmed21 Scripting Help 5 08/08/2016 08:07 AM
Unix Time? BLAbla93 Help Archive 2 08/07/2011 04:09 AM
Unix Time Czeladnik Help Archive 12 20/04/2011 08:09 AM


All times are GMT. The time now is 01:01 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.