max_w1 Posted December 18, 2008 Share Posted December 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/ Share on other sites More sharing options...
gevans Posted December 18, 2008 Share Posted December 18, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-718724 Share on other sites More sharing options...
Maq Posted December 18, 2008 Share Posted December 18, 2008 Providing code is the easiest way for us (mostly developers/programmers) to solve your problem. Unless it is some kind of design issue. Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-718776 Share on other sites More sharing options...
max_w1 Posted December 18, 2008 Author Share Posted December 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719091 Share on other sites More sharing options...
ngreenwood6 Posted December 18, 2008 Share Posted December 18, 2008 convert it using the date function: $initial_time = "123456789"; $time = date("i:s", $time); echo $time; Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719106 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719111 Share on other sites More sharing options...
ngreenwood6 Posted December 18, 2008 Share Posted December 18, 2008 Good one premiso never thought about doing it like that lol. Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719115 Share on other sites More sharing options...
max_w1 Posted December 18, 2008 Author Share Posted December 18, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719157 Share on other sites More sharing options...
max_w1 Posted December 18, 2008 Author Share Posted December 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719173 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 round Quote Link to comment https://forums.phpfreaks.com/topic/137531-solved-please-help-time/#findComment-719178 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.