header

Relative Date vs Absolute Date, Relative Date with PHP

One thing that I’ve been noticing a lot recently is the usage of relative dates instead of absolute dates. In other words, dates of objects are formatted as “X days ago”, “X minutes ago” or “X weeks ago” instead of “5:32 p.m. October 12, 2006”.

I’m sure that each of these has applications where they are more appropriate. However, if you format a date absolutely, you need to have the time zone, and whether or not daylight savings time is enabled on the client side. Even so, it is prone to error. For example, if the server’s time is different than the client’s then they would see an inevitable difference. I.e. “I just created an object on this website, why does it have the timestamp of X minutes in the future?”

So, I would argue that more often than not, relative is better than absolute. Awhile back, I created a thread notifier script which utilizes a relative date difference function in PHP. I honestly don’t remember where I found it, but if it’s YOURS, let me know and I can give you credit.

// this function determines the difference between two unix timestamps
function dateDiff($dateTimeBegin,$dateTimeEnd) {
 // $dateTimeBegin =strtotime($dateTimeBegin);
 // $dateTimeEnd  =strtotime($dateTimeEnd);
     
  if($dateTimeEnd === -1 || $dateTimeBegin === -1) {
   # error condition
   return false;
  }
    
  $diff = $dateTimeEnd - $dateTimeBegin;
   
  if ($diff < 0) {
   # error condition
   return false;
  }
   
  $weeks = $days = $hours = $minutes = $seconds = 0; # initialize vars
   
  if($diff % 604800 > 0) {
   $rest1  = $diff % 604800;
   $weeks  = ($diff - $rest1) / 604800; # seconds a week
   if($rest1 % 86400 > 0) {
     $rest2 = ($rest1 % 86400);
     $days  = ($rest1 - $rest2) / 86400; # seconds a day
     if( $rest2 % 3600 > 0 ) {
       $rest3 = ($rest2 % 3600);
       $hours = ($rest2 - $rest3) / 3600; # seconds an hour
       if( $rest3 % 60 > 0 ) {
         $seconds = ($rest3 % 60);
         $minutes = ($rest3 - $seconds) / 60;  # seconds a minute
       } else {
         $minutes = $rest3 / 60;
       }
     } else {
       $hours = $rest2 / 3600;
     }
   } else {
     $days = $rest1/ 86400;
   }
  }else {
   $weeks = $diff / 604800;
  }

  $string = array();
  if($weeks > 1) {
   $string[]  = "$weeks weeks";
  } elseif ($weeks == 1) {
   $string[]  = "a week";
  }
  if($days > 1) {
   $string[] = "$days days";
  } elseif($days == 1) {
   $string[] = "a day";
  }
  if($hours > 1) {
   $string[] = "$hours hours";
  } elseif ($hours == 1) {
   $string[] = "an hour";
  }
  if($minutes > 1) {
   $string[] = "$minutes minutes";
  } elseif ($minutes == 1) {
   $string[] = "a minute";
  }
  if($seconds > 1) {
   $string[] = "$seconds seconds";
  } elseif($seconds == 1) {
   $string[] = "a second";
  }

  # join together all the strings in the array above except the last element
  //$text  = join(', ', array_slice($string,0,sizeof($string)-1)) . ", and ";
  //$text .= array_pop($string);  # put the last one on after the and
  //i only want the most significant date increment
  $text = $string[0];

  return array($text, $weeks, $days, $hours, $minutes, $seconds);
}

I know I’ll use this a lot in the future. Please let me know if YOU use it.

Comments (2)

PHP to select multiple options

I’ve never had to deal with a “multiple” select html entity before with PHP… somehow.

If you have the html tag “select” with the attribute “multiple” set, as in:
<select name='favorites' multiple>
Unless end the name of the tag with [], you aren’t able to utilize the values.

e.g.
<select name='favorites' multiple>
<option selected value='0'>0</option>
<option selected value='1'>1</option>
</select>

as seen here

When the data is submitted, PHP will only have one data value, even though two are selected:

Array
(
    [favorites] => 1
)

If you add brackets to the name
e.g.
<select name='favorites[]' multiple>
<option selected value='0'>0</option>
<option selected value='1'>1</option>
</select>

as seen here (it looks the same)

PHP will then have an array of the selected values:

Array
(
    [favorites] => Array
        (
            [0] => 0
            [1] => 1
        )
)

Trivial if you knew it. If not, it’s good to know.

Comments (0)

JpGraph is great.

For dynamic graph generation, there’s no beating JpGraph. I’ve used it recently for work, and it’s very nice.

Comments (0)

A sql search algorithm

I had the opportunity to do a simple search algorithm for my work. We have a fairly small database of text based certifications which need to be searched. If someone were to search for “radeon ati” it should show all of the certifications that match that string in any way.

Our database is MySQL, and PHP is the frontend, so you can find the respective MySQL functions at http://www.mysql.com and the PHP functions at http://www.php.net/.

If someone inserted into the form “radeon ati”, I would get that text and run it through this function:

function split_spaces($in){
    $tmp = explode(" ", $in);
    $tmp = implode("%' AND N.CardName LIKE '%", $tmp);
    return $tmp;
}

Then calling that function inside of my query:

...
AND ( N.CardName LIKE '%".$this->strip_spaces($q)."%' )
...

leading to the final result becoming something similar to

...
AND ( N.CardName LIKE '%radeon%' AND N.CardName LIKE '%ati%' )
...

This leads to a much more efficient search “engine” than simply feeding in the entire string inside the LIKE ‘%%’ clause.

Comments (6)

PHP – Last Modified Time of File

I was manually entering the last time I edited the file for a project I’m working on. I found a way to automatically do it with php:


<em>Last updated
<?php echo date("F d Y h:i:s.",filemtime(__FILE__)); ?>
</em>

Here it is in action: Index.php was last updated
< ?php echo date("F d Y h:i:s.",filemtime('/home/rdehler/raybdbomb.com/index.php')); ?>

Comments (0)


blogtimes