Current location: Hot Scripts Forums » Programming Languages » PHP » Two useful functions


Two useful functions

Reply
  #1 (permalink)  
Old 01-31-04, 09:05 AM
Styric Styric is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Two useful functions

I have written two extremely useful functions for my CMS that I think could be useful for other people.

---------------------------------------------------

Code:
function compare_paths($coma, $comb)
{
 //===== We want the directory ================

 $temp = explode("/", $coma);
 $temp = array_reverse($temp);
 if(substr_count($temp[0], "."))
 {
  array_shift($temp);
  $temp = array_reverse($temp);
  $coma = implode("/", $temp);
 }
 else
 {
  //----- No trailing slash -------------------

  if($coma{strlen($coma) - 1} == "/")
  {
   $coma = substr($coma, 0, strlen($coma) - 1);
  }
 }

 $temp = explode("/", $comb);
 $temp = array_reverse($temp);
 if(substr_count($temp[0], "."))
 {
  array_shift($temp);
  $temp = array_reverse($temp);
  $comb = implode("/", $temp);
 }
 else
 {
  //----- No trailing slash -------------------

  if($comb{strlen($comb) - 1} == "/")
  {
   $comb = substr($comb, 0, strlen($comb) - 1);
  }
 }

 //===== No starting slashes ==================

 if($coma{0} == "/")
 {
  $coma = substr($coma, 1, strlen($coma) - 1);
 }

 if($comb{0} == "/")
 {
  $comb = substr($comb, 1, strlen($comb) - 1);
 }

 //===== Remove same start ====================

 for($a = strlen($coma);$a > 0;$a--)
 {
  $b = substr($coma, 0, $a);

  if(substr_count($comb, $b))
  {
   $coma = str_replace($b, "", $coma);
   $comb = str_replace($b, "", $comb);
   break;
  }
 }

 //===== Add dots =============================

 if(strlen($comb))
 {
  for($a = 0;$a < count(explode("/", $comb));$a++) { $coma = "../".$coma; }
 }
 else
 {
  $coma = ".".$coma;
 }

 //===== And we are done! =====================

 return $coma;
}
This function makes $coma relative to $comb.

E.g.

Code:
echo compare_paths("/home/foo/bar", "/home/styric/public_html");
Would return:

Quote:
../../foo/bar
---------------------------------------------------

Code:
function compare_timezones($tza, $tzb)
{
 return (intval(intval($tza) / 100) * 60)-(intval(intval($tzb) / 100) * 60)
      + ($tza / 100 - intval($tza / 100)) * 100
      - ($tzb / 100 - intval($tzb / 100)) * 100;
}
This function finds the difference between two timezones.

E.g.

Code:
$server_tz = date("O");
$server_tm = time();
$client_tz = 930;

$gmt        = $server_tm + (compare_timezones(0, $server_tz) * 60);
$client_tm  = $gmt + (compare_timezones($client_tz, 0) * 60);
$client_tm2 = $server_tm + (compare_timezones($client_tz, $server_tz) * 60);

echo "Server Timezone: "      .$server_tz."<br>"
   . "Client Timezone: &nbsp;".$client_tz."<br>"
   . "<br>"
   . "Server: "         .date("g:i A", $server_tm)."<br>"
   . "GMT: &nbsp;&nbsp;".date("g:i A", $gmt      )."<br>"
   . "Client: &nbsp;"   .date("g:i A", $client_tm)."<br>"
   . "Client: &nbsp;"   .date("g:i A", $client_tm2);
Would return something like:

Quote:
Server Timezone: 800
Client Timezone: 930

Server: 11:02 PM
GMT: 3:02 PM
Client: 12:32 AM
Client: 12:32 AM
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
timed functions and cycles smarty PHP 0 12-29-03 12:22 PM
Err I know what I want but dont know what the functions called. DAL JavaScript 1 11-28-03 02:16 PM
Array passing between functions alfreds PHP 1 08-25-03 05:29 AM
Optimizing functions amidamaru PHP 7 08-08-03 05:56 PM
User Functions DA Master PHP 4 06-15-03 05:00 AM


All times are GMT -5. The time now is 08:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.