datetime - Date Diff Human Format Output in PHP -


ok have found many threads on topic, can't find 1 seems working me.

i've got script working, if input date today, have date example that's sunday 7th of july @ 10:51am, , it's coming thursday @ 12:33 pm, , older week coming january 1 @ 12:33 pm.

this script far (the input date $timestamp in format y-m-d h:i:s)

function datediff($timestamp) { if(empty($timestamp)) {     return "no date provided"; } // time difference , setup arrays $unix_date = strtotime($timestamp); if(empty($unix_date)) {         return "bad date"; } $difference = time() - $unix_date; $periods = array("second", "minute", "hour", "day", "week", "month", "years"); $lengths = array("60","60","24","7","4.35","12");  // past or present if ($difference >= 0) {     $ending = "ago"; } else {     $difference = -$difference;     $ending = "to go"; }  // figure out difference looping while less array length // , difference larger lengths. $arr_len = count($lengths); for($j = 0; $j < $arr_len && $difference >= $lengths[$j]; $j++) {     $difference /= $lengths[$j]; }  // round      $difference = round($difference);  // make plural if needed if($difference != 1) {     $periods[$j].= "s"; }  // default format $text = $difference." ".$periods[$j]." ".$ending;  // on 24 hours if($j > 2) {     // future date on day formate year     if($ending == "to go") {         if($j == 3 && $difference == 1) {             $text = "tomorrow @ ". date("g:i a", $timestamp);         } else {             $text = date("f j, y \a\\t g:i a", $timestamp);         }         return $text;     }      if($j == 3 && $difference == 1) { // yesterday         $text = "yesterday @ ". date("g:i a", $timestamp);     } else if($j == 3) { // less week display -- monday @ 5:28pm         $text = date("l \a\\t g:i a", $timestamp);     } else if($j < 6 && !($j == 5 && $difference == 12)) { // less year display -- june 25 @ 5:23am         $text = date("f j \a\\t g:i a", $timestamp);     } else { // if on year or same month 1 year ago -- june 30, 2010 @ 5:34pm         $text = date("f j, y \a\\t g:i a", $timestamp);     } }  return $text; } 

can see may making show wrong information??

fixed. added following. error reports took bit come through didn't see them @ first

if(empty($timestamp)) {     return "no date provided"; } else {     $timestamp = strtotime($timestamp); } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -