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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.