Jump to content

[SOLVED] PLEASE HELP (TIME)


max_w1

Recommended Posts

hi, i am working on a new project where i have to make two pages, the first one(add.php) adds the products to the database and the next one(products.php) displays it, the problem is i don't want to display the time when the product was added, i want something like this "This product was added **seconds ago". can someone help me.

 

Thanks in advance.   

Link to comment
Share on other sites

Well, if you're using timestamps this is very easy.

 

When you add the product, also add the current timestamp e.g 1229604675

 

When displaying you take that timestamp away from the current time, for example;

 

1229604696 - 1229604675 = 21

 

added 12 seconds ago

 

You can then use 21 (or whatever number you get) to change what it display, seconds, hours, days etc...

Link to comment
Share on other sites

Well, if you're using timestamps this is very easy.

 

When you add the product, also add the current timestamp e.g 1229604675

 

When displaying you take that timestamp away from the current time, for example;

 

1229604696 - 1229604675 = 21

 

added 12 seconds ago

 

You can then use 21 (or whatever number you get) to change what it display, seconds, hours, days etc...

 

I tried using that method but it is showing me only seconds. like 60seconds ago 85 seconds ago... 400 seconds ago.

how do i make it show both in seconds and minutes?

Link to comment
Share on other sites

$time1 = 1229604696;
$time2 = 1229604675;

$diff = $time1 - $time2;
$minutes = floor($diff * 60);
$seconds = round($diff/60, 0);

echo $minutes " Minutes and<br />";
echo $seconds " Seconds since.<br />";

 

EDIT:

Modified to show seconds remaining instead of total seconds.

Link to comment
Share on other sites

$time1 = 1229604696;
$time2 = 1229604675;

$diff = $time1 - $time2;
$minutes = floor($diff * 60);
$seconds = round($diff/60, 0);

echo $minutes " Minutes and<br />";
echo $seconds " Seconds since.<br />";

i tried that too, but the problem is, i am using the time in a loop and when i use the above function it repeats the same time.

EDIT:

Modified to show seconds remaining instead of total seconds.

Link to comment
Share on other sites

hi guys, thank you for trying to help me.

i have found a solution for this problem.

$ftime = time() - $time_stored_in_database;
            if ($ftime > 60)
			{
			$mtime = $ftime/60;
			echo $mtime." minutes ago";
			}
			else
			{
			echo $ftime. "seconds ago";
			}

this works great.

But this is returning very long values like 74.1333333333. does any one here know how to make this short?

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.