PHP - Moved: Help Converting Seconds To Minutes And Hours
This topic has been moved to JavaScript Help.
http://www.phpfreaks.com/forums/index.php?topic=352669.0 Similar Tutorials
Hi, echo '<div style="float:right;width:60%;color:#666666">'. TeamSpeak3_Helper_Convert::version($ts3->virtualserver_uptime) .'</div>'; Edited June 24, 2020 by Lux05 Other than if statement, is there a way to change a duration give in seconds (e.g. 5746 seconds) to the standard format of x hours and y minutes and z seconds? Code: [Select] function time_ago($timestamp) { //$months = array("Jan" => 31, "Feb" => 28, "Mar" => 31, "Apr" => 30, "May" => 31, "Jun" => 30, "Jul" => 31, "Aug" => 31, "Sept" => 30, "Oct" => 31, "Nov" => 30, "Dec" => 31); $curr_time = time(); $time_ago = $curr_time - $timestamp; if($time_ago < 60) // seconds { $ext = $time_ago . " seconds ago.."; } else { if($time_ago >= 60) // minutes { $time = floor($time_ago / 60); $seconds_remainder = $time_ago % 60; $ext = $time . " minutes " . $seconds_remainder . " seconds ago.."; if($time >= 60) // hours { $hours = floor($time / 60); $minutes = $time % 60; $ext = $hours . " hours " . $minutes . " minutes ago.."; if($hours >= 24) // days { $days = floor($hours / 24); $day_hours = $hours % 24; $ext = $days . " days " . $day_hours . " hours ago .."; } } } } return $ext; } Is there a cleaner approach to this? Code: [Select] function time_ago($timestamp) { //$months = array("Jan" => 31, "Feb" => 28, "Mar" => 31, "Apr" => 30, "May" => 31, "Jun" => 30, "Jul" => 31, "Aug" => 31, "Sept" => 30, "Oct" => 31, "Nov" => 30, "Dec" => 31); $curr_time = time(); $time_ago = $curr_time - $timestamp; if($time_ago < 60) // seconds { $ext = $time_ago . " seconds ago.."; } else { if($time_ago >= 60) // minutes { $time = floor($time_ago / 60); $seconds_remainder = $time_ago % 60; $ext = $time . " minutes " . $seconds_remainder . " seconds ago.."; if($time >= 60) // hours { $hours = floor($time / 60); $minutes = $time % 60; $ext = $hours . " hours " . $minutes . " minutes ago.."; if($hours >= 24) // days { $days = floor($hours / 24); $day_hours = $hours % 24; $ext = $days . " days " . $day_hours . " hours ago .."; } } } } return $ext; } It seems to work fine (was going to tackle Months but haven't done it yet), but anyway is there a better way to do this? I'm guessing there is as I wrote this up rather fast and I'm not that great with date math. Any suggestions are appreciated (besides maybe using ternaries which I'm working on atm). so i have a mysql column with a datetime and i was wondering how i could make it echo the time in "X minutes ago" or "X hours ago" and also, when its more then 24 hours old, delete the entire entry Unfortunately I do not know how to go about doing this in php any simple examples of time and date will be great. So what I am trying to do is, display to minutes until the next half hour as well as the seconds until the next minute. Eg. Code: [Select] <?php echo $minutes; ?>:<?php echo $seconds; ?> Any help using the time and or date functions in this case will be great. Thank you for your stupendous help. Brian I wrote a function that grabs the elapsed time of a recently uploaded video. However, the time does not seem to increment. For example, if I upload a video, the time will display as '1 second'. However, if I continuously refresh the page, the time does not increment or increase. Any way to fix this? I figured I'd have to put it in some kind of loop (I do call the function in another class).
function getElapsedTime($time) { $time = time() - $time; // get time since video upload date $time = ($time < 1) ? 1 : $time; $tokens = array( 31536000 => 'year', 2592000 => 'month', 604800 => 'week', 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second' ); foreach ($tokens as $unit => $text) { if ($time < $unit) continue; $numberOfUnits = floor($time / $unit); return $numberOfUnits . ' ' . $text . (($numberOfUnits > 1) ? 's' : ''); } }
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=322423.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=343671.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=342245.0 This topic has been moved to Other Web Server Software. http://www.phpfreaks.com/forums/index.php?topic=351144.0 This topic has been moved to Other. http://www.phpfreaks.com/forums/index.php?topic=316401.0 To get current date and time I use: $date = date("d-M-Y h:i:s"); But how can I get date and time which was before 5 minutes. I'm trying to add some minutes ($interval) to any given time, but my results aren't as expected... Code: [Select] <?php $year="2012"; $opentime="09"; $interval="10"; $thedate = "$year:01:01 $opentime:00:00"; $startdate = strtotime($thedate); $date = date("Y-m-d",$startdate); $time = date("g:i a",$startdate); $newtime = strtotime("+$interval", $time); $nexttime = date("g:i a",$newtime); echo $date; echo "<br>"; echo $time; echo "<br>"; echo $nexttime; is returning: 2012-01-01 9:00 am 7:10 pm I was expecting the last to be 9:10 am. Can someone point me in the right direction? Thanks. I have a database with Users as a table. It has the normal stuff, id, username, password, and I have anther in it called lastactive. It is int(11) , and I am wondering how I would make a sql query select the users in the database 'users' and see who has been active in the past 15 mins. By the way, the lastactive time gets updated by the function time() Here is my code: <?php $online = mysql_query("SELECT * from users WHERE (TIMESTAMPDIFF(MINUTE, `lastactive`, NOW()) < 15) ORDER by lastactive DESC") or die (mysql_error()); while($online = mysql_fetch_assoc($online)) { echo '<a style="color:#F0CD87;" href="profile?id='.$online['user_id'].'">'; echo ucFirst($online['username']); echo '</a>, '; } ?> Here is how the last active gets updated. <?php if(isset($_SESSION['logged'])) { mysql_query("UPDATE `users` SET `lastactive`='" . time() . "' WHERE `username`='" . $_SESSION['username'] . "'"); } ?> Again its not showing the users that have been on since 15 minutes ago, even tho it updates the users last active on every page since its in the footer.php part. which is on every page. Thanks for the upcoming help. hello, if i have the following, how would i display it? Code: [Select] function minutes_round ($hour = "$signintime", $minutes = '5', $format = "H:i") { $seconds = strtotime($hour); $rounded = round($seconds / ($minutes * 60)) * ($minutes * 60); return date($format, $rounded); } I've created a demo page for my CMS, and I want to delete itself and make a fresh content install. I got the delete part figured out, but what about uploading fresh content files? (only a few text files). Here's the delete part: Code: [Select] <?php $expiretime=720; // minutes (in how many minutes it deletes the files) $tmpFolder="tmp/"; // where to delete the files - be careful with this. $fileTypes="*.*"; foreach (glob($tmpFolder . $fileTypes) as $Filename) { $FileCreationTime = filectime($Filename); $FileAge = time() - $FileCreationTime; if ($FileAge > ($expiretime * 60)){ //deleting files: unlink($Filename); } } ?> I have a forum script I made that has thread bumping for the thread creator only. I am trying to limit the amout of time the creator can bump to be every 10 minutes. Here's my code. list($title, $creator, $locked, $hidden, $moved, $lastbump) = mysql_fetch_row(mysql_query('SELECT title, creator, locked, hidden, moved, lastbump FROM forum_threads WHERE id="'.$_GET['threadid'].'"')); if(($locked == 1 || $hidden == 1 || $moved == 1) && !$_SESSION['rights'] == 2){ header("location: thread.ws?id=".$_GET['threadid']."&page=".$_GET['page'].""); } else { if($creator == $_SESSION['username']){ mysql_query("UPDATE forum_threads SET lastbump = NOW() WHERE id='".$_GET['threadid']."'"); } else { header("location: thread.ws?id=".$_GET['threadid']."&page=".$_GET['page'].""); } } Hey guys only thing i can find online is examples of showing how much time is left until a predetermind time. but what I need is how many minutes are left until the next hour hits anyone have any ideas on this? Im wanting to do it in UNIX time Hello I am trying to do a cronjob to check if a user has been online within the past 24 hours. I have a column with "last_login" with this format: "1337593284". How can I do a simple if (last_login+24hours< servertime) { do something... |