Jump to content

Calculate how long since last visit


svenn

Recommended Posts

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  :)

Link to comment
https://forums.phpfreaks.com/topic/221807-calculate-how-long-since-last-visit/
Share on other sites

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.";
?>

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.");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.