문제

I have a database table with articles and each one of this articles have a submitted date. I need to calculate the days and hours since the article have been published in the database, like:

This article has been published 4 hours ago.
This article has been published 3 days and 4 hours ago.

There are already some code that I can reuse to do this? I've searched on google, but maybe I'm not using the correct words.

Any clue that could help me?

Best Regards,

도움이 되었습니까?

해결책

Have a look at the datetime package, it has everything you need.

when you subtract one datetime from another, you get a timedelta object. You can use total_seconds() to get the duration in seconds and use division to convert it to hours and days. Then your only job then is to format it into a readable string.

다른 팁

I'd convert the submitted date into a datetime, then use something like https://gist.github.com/207624 to convert the datetime to a humanized string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top