Ok i have this long bit of code that determines how long ago someone input a post or comment. The only bit of the code that is giving me problems is at the very beginning. When i place a fresh topic or comment it doesn't go into the " just a moment ago ". I dont know why it just doesn't seem to go into that piece of code. HELP PLEASE. Heres the code. safetimestamp is the time stamp that was placed into the database.

$diff = time() - $safetimestamp;

$second="1";
$minute="60";
$hour="3600";
$day="86400";
$week="604800";
$month="2592000";
$year="31536000"; 

if($diff> $year){
	if($diff > ($year * 3)){
		 $time = "over 3 years old";
	}else{
		if($diff > $year * 2){
			$time ="over 2 years old";
		}else{
			$time ="over 1 year old";
		}
	}
}else{
	if($diff > $month){
		if($diff > ($month * 6)){
			$time = "over 6 months old";
		}else{
			if($diff > ($month * 3)){
				$time = "over 3 months old";
			}else{
				$time = "over 1 month old";
			}
		}
	}else{
		if($diff > $week){
			if($diff > ($week * 3)){
				$time = "over 3 weeks old";
			}else{
				if($diff > ($week * 2)){
					$time = "over 2 weeks old";
				}else{
					$time = "over 1 week old";
				}
			}			
		}else{
			if($diff > $day){
				if($diff > ($day * 6)){
					$time = " 6 days old";
				}else{
					if($diff > ($day * 5)){
						$time = " 5 days old";
}else{
if($diff > ($day * 4)){
$time = " 4 days old";
}else{
if($diff > ($day * 3)){
$time = " 3 days old";
}else{
if($diff > ($day * 2)){
$time = " 2 days old";
}else{
if($diff > $day){
$time = "1 day old";
}else{
										
}
}
}	
}		
}
}			
}else{
if($diff > $hour){
if($diff > ($hour * 23)){
$time = " 23 hours ago ";
}else{
if($diff > ($hour * 22)){
$time = " 22 hours ago ";
}else{
if($diff > ($hour * 21)){
$time = " 21 hours ago ";
}else{
if($diff > ($hour * 20)){
$time = " 20 hours ago ";
}else{
if($diff > ($hour * 19)){
$time = " 19 hours ago ";
}else{
if($diff > ($hour * 18)){
											$time = " 18 hours ago ";										
}else{
if($diff > ($hour * 17)){
$time = " 17 hours ago ";
											}else{
												if($diff > ($hour * 16)){
													$time = " 16 hours ago ";
												}else{
													if($diff > ($hour * 15)){
														$time = " 15 hours ago ";
													}else{
														if($diff > ($hour * 14)){
															$time = " 14 hours ago ";
														}else{
															if($diff > ($hour * 13)){
																$time = " 13 hours ago ";
															}else{
																if($diff > ($hour * 12)){
																	$time = " 12 hours ago ";										
																}else{
																	if($diff > ($hour * 11)){
																		$time = " 11 hours ago ";
																	}else{
																		if($diff > ($hour * 10)){
																			$time = " 10 hours ago ";
																		}else{
																			if($diff > ($hour * 9)){
																				$time = " 9 hours ago ";
																			}else{
																				if($diff > ($hour * 8)){
																					$time = " 8 hours ago ";
																				}else{
																					if($diff > ($hour * 7)){
																						$time = " 7 hours ago ";
																					}else{
																						if($diff > ($hour * 6)){
																							$time = " 6 hours ago ";										
																						}else{
																							if($diff > ($hour * 5)){
																								$time = " 5 hours ago ";
																							}else{
																								if($diff > ($hour * 4)){
																									$time = " 4 hours ago ";
																								}else{
																									if($diff > ($hour * 3)){
																										$time = " 3 hours ago ";
																									}else{
																										if($diff > ($hour * 2)){
																											$time = " 2 hours ago ";
																										}else{
																											if($diff > ($hour)){
																												$time = " 1 hour ago ";
																											}else{
																												
																												}
																											}
																										}	
																									}		
																								}
																							}										
																						}
																					}
																				}	
																			}		
																		}
																	}										
																}
															}
														}	
													}		
												}
}										
}
}
}	
}		
}
}else{
if($diff > $minute){
if($diff > ($minute * 60)){
$time = " 60 minutes ago ";
}else{
if($diff > ($minute * 59)){
$time = " 59  minutes ago ";
}else{
if($diff > ($minute * 58)){
$time = " 58 minutes ago ";
}else{
if($diff > ($minute * 57)){
											$time = " 57 minutes ago ";
}else{
											if($diff > ($minute * 56)){
												$time = " 56 minutes ago ";
											}else{
												if($diff > ($minute * 55)){
													$time = " 55 minutes ago ";										
												}else{
													if($diff > ($minute * 54)){
														$time = " 54 minutes ago ";
													}else{
														if($diff > ($minute * 53)){
															$time = " 53 minutes ago ";
														}else{
															if($diff > ($minute * 52)){
																$time = " 52 minutes ago ";
															}else{
																if($diff > ($minute * 51)){
																	$time = " 51 minutes ago ";
																}else{
																	if($diff > ($minute * 50)){
																		$time = " 50 minutes ago ";
																	}else{
																		if($diff > ($minute * 49)){
																			$time = " 49 minutes ago ";										
																		}else{
																			if($diff > ($minute * 48)){
																				$time = " 48 minutes ago ";
																			}else{
																				if($diff > ($minute * 47)){
																					$time = " 47 minutes ago ";
																				}else{
																					if($diff > ($minute * 46)){
																						$time = " 46 minutes ago ";
																					}else{
																						if($diff > ($minute * 45)){
																							$time = " 45 minutes ago ";
																						}else{
																							if($diff > ($minute * 44)){
																								$time = " 44 minutes ago ";
																							}else{
																								if($diff > ($minute * 43)){
																									$time = " 43 minutes ago ";										
																										}else{
																									if($diff > ($minute * 42)){
																										$time = " 42 minutes ago ";
																									}else{
																										if($diff > ($minute * 41)){
																											$time = " 41 minutes ago ";
																										}else{
																											if($diff > ($minute * 40)){
																												$time = " 40 minutes ago ";
																											}else{
																												if($diff > ($minute * 39)){
																													$time = " 39 minutes ago ";
																												}else{
																													if($diff > ($minute * 38)){
																														$time = " 38 minutes ago ";
																													}else{
																														if($diff > ($minute * 37)){
																															$time = " 37 minutes ago ";
																														}else{
																															if($diff > ($minute * 36)){
																																$time = " 36 minutes ago ";
																															}else{
																																if($diff > ($minute * 35)){
																																	$time = " 35 minutes ago ";
																																}else{
																																	if($diff > ($minute * 34)){
																																		$time = " 34 minutes ago ";
																																	}else{
																																		if($diff > ($minute * 33)){
																																			$time = " 33 minutes ago ";
																																		}else{
																																			if($diff > ($minute * 32)){
																																				$time = " 32 minutes ago ";										
																																			}else{
																																				if($diff > ($minute * 31)){
																																					$time = " 31 minutes ago ";
																																				}else{
																																					if($diff > ($minute * 30)){
																																						$time = " 30 minutes ago ";
																																					}else{
																																						if($diff > ($minute * 29)){
																																							$time = " 29 minutes ago ";
																																						}else{
																																							if($diff > ($minute * 28)){
																																								$time = " 28 minutes ago ";
																																							}else{
																																								if($diff > ($minute * 27)){
																																									$time = " 27 minutes ago ";
																																								}else{
																																									if($diff > ($minute * 26)){
																																										$time = " 26 minutes ago ";										
																																									}else{
																																										if($diff > ($minute * 25)){
																																											$time = " 25 minutes ago ";
																																										}else{
																																											if($diff > ($minute * 24)){
																																												$time = " 24 minutes ago ";
																																											}else{
																																												if($diff > ($minute * 23)){
																																													$time = " 23 minutes ago ";
																																												}else{
																																													if($diff > ($minute * 22)){
																																														$time = " 22 minutes ago ";
																																													}else{
																																														if($diff > ($minute * 21)){
																																															$time = " 21 minutes ago ";
																																														}else{
																																															if($diff > ($minute * 20)){
																																																$time = " 20 minutes ago ";										
																																															}else{
																																																if($diff > ($minute * 19)){
																																																	$time = " 19 minutes ago ";
																																																}else{
																																																	if($diff > ($minute * 18)){
																																																		$time = " 18 minutes ago ";
																																																	}else{
																																																		if($diff > ($minute * 17)){
																																																			$time = " 17 minutes ago ";
																																																		}else{
																																																			if($diff > ($minute * 16)){
																																																				$time = " 16 minutes ago ";
																																																			}else{
																																																				if($diff > ($minute * 15)){
																																																					$time = " 15 minutes ago ";
																																																				}else
																																																					if($diff > ($minute * 14)){
																																																						$time = " 14 minutes ago ";
																																																					}else{
																																																						if($diff > ($minute * 13)){
																																																							$time = " 13 minutes ago ";
																																																						}else{
																																																							if($diff > ($minute * 12)){
																																																								$time = " 12 minutes ago ";
																																																							}else{
																																																								if($diff > ($minute * 11)){
																																																									$time = " 11 minutes ago ";
																																																								}else{
																																																									if($diff > ($minute * 10)){
																																																										$time = " 10 minutes ago ";										
																																																									}else{
																																																										if($diff > ($minute * 9)){
																																																											$time = " 9 minutes ago ";
																																																										}else{
																																																											if($diff > ($minute * 8)){
																																																												$time = " 8 minutes ago ";
																																																											}else{
																																																												if($diff > ($minute * 7)){
																																																													$time = " 7 minutes ago ";
																																																												}else{
																																																													if($diff > ($minute * 6)){
																																																														$time = " 6 minutes ago ";
																																																													}else{
																																																														if($diff > ($minute * 5)){
																																																															$time = " 5 minutes ago ";
																																																														}else{
																																																															if($diff > ($minute * 4)){
																																																																$time = " 4 minutes ago ";										
																																																															}else{
																																																																if($diff > ($minute * 3)){
																																																																	$time = " 3 minutes ago ";
																																																																}else{
																																																																	if($diff > ($minute * 2)){
																																																																		$time = " 2 minutes ago ";
																																																																	}else{
																																																																		if($diff > $minute){
																																																																			$time = " 1 minute ago ";
																																																																		}else{
																																																																			$time = " just a moment ago ";
																																																																		}
																																																																	}
																																																																}
																																																															}
																																																														}
																																																													}
																																																												}
																																																											}
																																																										}
																																																									}
																																																								}
																																																							}
																																																						}
																																																					}
																																																				}
																																																			}
																																																		}	
																																																	}		
																																																}
																																															}										
																																														}
																																													}
																																												}	
																																											}		
																																										}
																																									}										
																																								}
																																							}
																																						}	
																																					}		
																																				}
																																			}										
																																		}
																																	}
																																}	
																															}		
																														}
																													}
																												}
																											}
																										}
																									}
																								}
																							}
																						}
																					}
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												}
}
}
}
}
}					
}
}				
}	
}
}
Member Avatar for diafol

this code is bonkers. 412 lines with indentation across the screen. Can't you reformat this?

Well sure, if he divides $diff by $minute the result is what he wants to output, he'll just need a single line..... (Something similar for the rest of course, the whole thing can probably be done within 20 lines of code.) Perhaps he gets paid by line... ;)

lol... hey it was my first crack at calculating time cut me a lil slack.. lol.. when i get home tonight i'll go ahead and try to clean up the code...

Member Avatar for diafol

Code:

<?php

//===========  TEMPORARY CODE ===========//

//DEFAULT PARAMETERS
$time = 3;
$units = 'm';
//GET FORM DATA IF POSTED
if(isset($_POST['submit'])){
    $time = $_POST['time'];
    $units = $_POST['units'];
}
//CONVERT ALL TIMES TO SECS
$diff = $time * $unit_array[$units];

//=========END TEMPORARY CODE ===========//

//ALL THE ABOVE AND THE FORM BELOW MAY BE DELETED once you have your time difference code set up...
//OK, by now you should have a valid time difference in seconds - $diff

//SET No SECS FOR EACH UNIT OF TIME
$unit_array = array('y'=>31536000,'m'=>2592000,'w'=>604800,'d'=>86400,'h'=>3600,'i'=>60,'s'=>1);
//INTERVAL PROFILES: (1) periods to include - need to be in descending order, (2) suffixes to attach 
$intervals = 
array(
    'y'     =>
        array(
            'periods'   =>  array(3,1),
            'suffix'    =>  array('over %s years ago','over a year ago'),
        ),
    'm' =>
        array(
            'periods'   =>  array(6,3,1),
            'suffix'    =>  array('over %s months ago','over a month ago'),
        ),
    'w'     =>
        array(
            'periods'   =>  array(3,2,1),
            'suffix'    =>  array('over %s weeks ago','over a week ago'),
        ),
    'd'     =>
        array(  
            'periods'   =>  array_reverse(range(1,6)),
            'suffix'    =>  array('over %s days ago','a day ago'),
        ),
    'h'     =>
        array(  
            'periods'   =>  array_reverse(range(1,23)),
            'suffix'    =>  array('over %s hours ago','an hour ago'),
        ),
    'i'     =>
        array(  
            'periods'   =>  array_reverse(range(1,59)),
            'suffix'    =>  array('over %s minutes ago','just now'),
        ),
    's'     =>
        array(
            'periods'   =>  array_reverse(range(1,59)),
            'suffix'    =>  array('just now','just now'),
        )   
);  
//LOOP THROUGH POSSIBILITIES    
foreach($intervals as $k=>$int){
    $s = $unit_array[$k];
    //First loop to shortcut massive number of loops
    if($diff >= $s){
        $lastindex = count($int['periods'])-1;
        //This is the 'business' loop 
        foreach($int['periods'] as $p){
            //get no. seconds of last item in current array
            $multi = $p*$s;
            if($diff >= $multi){
                //select which suffix to use: 1 = singular, 0 = plural
                $sindex = ($p == $int['periods'][$lastindex]) ? 1 : 0;
                $output = sprintf($int['suffix'][$sindex],$p);
                //if successful, break out of this inner loop
                break;
            }
        }
        //if arrive here break out of outside loop
        break;
    }
}
//tell us what's the time of the post!
echo "This article was posted $output";

//[18-ish lines of code without the comments, temporary bits and beautifying the multidimesnional array]
?>

<!-- ====================  TEMPORARY FORM ============================================== -->
<!-- JUST A FROM TO DEMONSTRATE THE USE - the inline php is awful - but it's just a demo -->
<!-- TRY overflowing the time, e.g. 13 months or 3605 seconds Although all times < 120 secs = 'just now' -->
<form method="post">
    <label for="time">Amount:</label> <input name="time" value="<?php echo $time;?>" autocomplete="off" />
    <br />
    <select name="units">
        <option value="y"<?php if($units == 'y')echo ' selected="selected"';?>>years</option>
        <option value="m"<?php if($units == 'm')echo ' selected="selected"';?>>months (30 days)</option>
        <option value="w"<?php if($units == 'w')echo ' selected="selected"';?>>weeks</option>
        <option value="d"<?php if($units == 'd')echo ' selected="selected"';?>>days</option>
        <option value="h"<?php if($units == 'h')echo ' selected="selected"';?>>hours</option>
        <option value="i"<?php if($units == 'i')echo ' selected="selected"';?>>minutes</option>
        <option value="s"<?php if($units == 's')echo ' selected="selected"';?>>seconds</option>
    </select>
    <br />
    <input name="submit" type="submit" value="submit" />
</form>
<!-- ====================  END TEMPORARY FORM =========================================== -->

This is another way, first take a look at,
http://www.php.net/manual/en/datetime.construct.php
http://www.php.net/manual/en/datetime.diff.php
http://www.php.net/manual/en/class.dateinterval.php

<?php

$safetimestamp = 1332185534;

$then = new DateTime();
$then->setTimestamp($safetimestamp);

$now = new DateTime();
$now->setTimestamp(time());

$difObj = $now->diff($then);

if($difObj->invert == 0)
{
  $message = "error: future date given";
}
else
{
  if($difObj->y > 0)
  {
    $message = "over ". $difObj->y . " years old";
  }
  else if($difObj->m > 0)
  {
    $message = "over ". $difObj->m . " months old";
  }
  else if($difObj->d > 0)
  {
    $message = "over ". $difObj->d . " months old";
  }
  else if($difObj->h > 0)
  {
    $message = "over ". $difObj->h . " hours old";
  }  
  else if($difObj->i > 1)
  {
    $message = "over ". $difObj->i . " minutes old";
  }     
  else if($difObj->i > 0)
  {
    $message = "over ". $difObj->i . " minute old";
  }    
  else 
  {
    $message = "just a moment ago";
  }
}
echo $message;
?>
Member Avatar for diafol

I originally went down that path, but then the user wanted non sequential matches. I found it easier doing the loop method in that case or you'd probably need quite a few conditionals.

For what it's worth RR, if you can use jkon's, do so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.