svenn Posted December 15, 2010 Share Posted December 15, 2010 Hi I'm trying to figure out how to calculate how long since a datevariable I have stored is from whenever the page is loaded.. I currently have a stored datevariable in the format: YYYY-MM-DD H:m:s ... So basically what I wish to accomplish is figuring out whenever i load the page which has stored this variable, how much time has passed. I want it to show up as for example : '10 minutes ago'.. '1 hour 25minutes ago' if it's the same day.. if its more than 1 day old it's enough to show only the number of days.....'1week 3 days ago' etc etc Quote Link to comment https://forums.phpfreaks.com/topic/221807-calculate-how-long-since-last-visit/ Share on other sites More sharing options...
litebearer Posted December 16, 2010 Share Posted December 16, 2010 A starting point... <?php $date1 = time(); $date2 = strtotime("2010-12-15 07:53:22PM"); /* this the datetime you have stored */ $dateDiff = $date1 - $date2; $fullDays = floor($dateDiff/(60*60*24)); $fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60)); $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60); $sec_used = ($full_days * 60 * 60 * 24) + ($fullHours * 60 * 60) + ($fullMinutes * 60); $sec_rem = $dateDiff - $sec_used; echo "Differernce is $fullDays days, $fullHours hours $fullMinutes minutes and $sec_rem seconds."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221807-calculate-how-long-since-last-visit/#findComment-1147914 Share on other sites More sharing options...
svenn Posted December 16, 2010 Author Share Posted December 16, 2010 Brilliant!! I'd say I love you, but i'll restrain myself and say: Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/221807-calculate-how-long-since-last-visit/#findComment-1148032 Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Make things more simple using PHPs own DateTime class: <?php // Create DateTime object with your timestamp $date = DateTime::createFromFormat("Y-m-d H:i:s", '2010-02-15 15:16:17'); // Calculate the difference between your timestamp and "now" $interval = $date->diff(new DateTime()); // Get the data stored in the DateInterval echo $interval->format("Differernce is %d days, %h hours %i minutes and %s seconds."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/221807-calculate-how-long-since-last-visit/#findComment-1148037 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.