In my opinion @Fabian's answer is probably the best. But are we making it more complicated than it should be?
Do you need to calculate it by days, months, years?
If you create this, wouldn't it be easier to use the timesec view?
When a user logs in:
user_login_time = time.time()
When the user revises:
time_difference = time.time() - user_login_time
Then check if the time_difference is more than XXXX seconds longer? Now I assume that you are just going to check if the user has not been registered for XX minutes or xx hours?
Otherwise, I would like to emphasize that @Fabian's answer would be better if you are looking at parsing time strings, or you need to perform other time and date functions with data.
I would also like to emphasize that if you use this method to create constants for time or do not forget to comment on them to make it more readable.
But if you're just trying to figure out if the user has been logged on in the last 30 minutes, it might be easier.
source share