note 96375 added to function.time

From: Date: Wed, 24 Feb 2010 00:39:58 +0000
Subject: note 96375 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-166303@lists.php.net to get a copy of this message
Here is a really simple hack used to calculate time difference between present and past and future (in a really efficient way!) To change between past, present, future, you need to do things only on the display side (no code patch needed). The default function is: <?php timeago($referencedate=0, $timepointer='', $measureby='', $autotext=true) ?> $timepointer is from where to start calculating and is only needed if you are calculating the difference between 2 specific times. In other words, the present is not one of those times. $referencedate is the time you're calculating to. This is required for all calculations. $measureby is the unit to use $autotext is whether to add "days from now" or "ago" to the string. Option only for present dates, otherwise, this variable is set to off. For a standard present-past, use: timeago(pastdate); It will automatically detect the time since in the appropriate units. There is some coding error because rounding plays a huge roll. If it's 390 days from now, instead of saying "1 year, 25 days", the function will say "1 year". Post a better solution if you can. <?php ## alex at nyoc dot net ## Feel free to better for your needs function timeago($referencedate=0, $timepointer='', $measureby='', $autotext=true){ ## Measureby can be: s, m, h, d, or y if($timepointer == '') $timepointer = time(); $Raw = $timepointer-$referencedate; ## Raw time difference $Clean = abs($Raw); $calcNum = array(array('s', 60), array('m', 60*60), array('h', 60*60*60), array('d', 60*60*60*24), array('y', 60*60*60*24*365)); ## Used for calculating $calc = array('s' => array(1, 'second'), 'm' => array(60, 'minute'), 'h' => array(60*60, 'hour'), 'd' => array(60*60*24, 'day'), 'y' => array(60*60*24*365, 'year')); ## Used for units and determining actual differences per unit (there probably is a more efficient way to do this) if($measureby == ''){ ## Only use if nothing is referenced in the function parameters $usemeasure = 's'; ## Default unit for($i=0; $i<count($calcNum); $i++){ ## Loop through calcNum until we find a low enough unit if($Clean <= $calcNum[$i][1]){ ## Checks to see if the Raw is less than the unit, uses calcNum b/c system is based on seconds being 60 $usemeasure = $calcNum[$i][0]; ## The if statement okayed the proposed unit, we will use this friendly key to output the time left $i = count($calcNum); ## Skip all other units by maxing out the current loop position } } }else{ $usemeasure = $measureby; ## Used if a unit is provided } $datedifference = floor($Clean/$calc[$usemeasure][0]); ## Rounded date difference if($autotext==true && ($timepointer==time())){ if($Raw < 0){ $prospect = ' from now'; }else{ $prospect = ' ago'; } } if($referencedate != 0){ ## Check to make sure a date in the past was supplied if($datedifference == 1){ ## Checks for grammar (plural/singular) return $datedifference . ' ' . $calc[$usemeasure][1] . ' ' . $prospect; }else{ return $datedifference . ' ' . $calc[$usemeasure][1] . 's ' . $prospect; } }else{ return 'No input time referenced.'; } } ?> ---- Server IP: 192.41.40.227 Probable Submitter: 66.67.103.230 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/96375 Del: integrated -- https://master.php.net/note/delete/96375/integrated Del: useless -- https://master.php.net/note/delete/96375/useless Del: bad code -- https://master.php.net/note/delete/96375/bad+code Del: spam -- https://master.php.net/note/delete/96375/spam Del: non-english -- https://master.php.net/note/delete/96375/non-english Del: in docs -- https://master.php.net/note/delete/96375/in+docs Del: other reasons-- https://master.php.net/note/delete/96375 Reject -- https://master.php.net/note/reject/96375 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166303) next »