Datetime ve Time Dönüşümü ve Fark Alma

Ayrı ayrı veriyorum. version_compare ile birleştirebilirsiniz.

PHP 5.1  ve öncesi için

<?php
$mysqldatetime="2015-01-29 01:47:00";

$sifirla = date_default_timezone_get();
date_default_timezone_set('Europe/Istanbul');
$dbdekizaman=strtotime($mysqldatetime);
date_default_timezone_set($sifirla );
$simdikizaman=time();
$simdikizamandatetime=date('Y\-m\-d\ H:i:s',$simdikizaman);

if($dbdekizaman < $simdikizaman){
	echo "küçük";
}else{
	echo "büyük";	
}
echo $dbdekizaman-$simdikizaman;

?>

Php 5.2 ve sonrası için

<?php
$mysqldatetime="2015-01-29 02:05:00";

$zamanbol=explode(" ",$mysqldatetime);
$gunbol=explode(":",$zamanbol[1]);

$datetime = new DateTime($zamanbol[0]);
$datetime->setTimezone(new DateTimeZone('Europe/Istanbul'));
$datetime->setTime($gunbol[0],$gunbol[1],$gunbol[2]);
$dbdekizamandatetime=$datetime->format('Y\-m\-d\ H:i:s');
$dbdekizaman=$datetime->getTimestamp();

$time = new DateTime();
$time->setTimezone(new DateTimeZone('Europe/Istanbul'));
$simdikizamandatetime=$datetime->format('Y\-m\-d\ H:i:s');
$simdikizaman=$time->getTimestamp();

if($dbdekizaman < $simdikizaman){
	echo "küçük";
}else{
	echo "büyük";	
}
echo $dbdekizaman-$simdikizaman;

?>

 

Zamana Göre Karşılama Mesajı

<?php 
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('Europe/Istanbul');//Yerel Zaman Seçme

$saat=date("G"); 
if($saat >= 6 and $saat <11) {echo "Güne nasıl başladın?";}
elseif($saat >= 11 and $saat <17) {echo "Günün nasıl geçiyor?";}
elseif($saat >= 17 and $saat <21) {echo "Akşamın nasıl geçiyor? ";}
else{echo "Gecen nasıl geçiyor?";}
?>

 

Gerçek Kullanıcı IP Adresi Bulma

Bazı sitelerde $_SERVER[“REMOTE_ADDR”] ile sunucu ip’si alınmasından dolayı problem yaşanmaktadır.Aşağıdaki fonksiyon ile kullanıcı ip’sini doğru bir şekilde kullanabilirsiniz.

<?php
function GercekIP()// kullanıcı ip bulma
   {
      if(getenv("HTTP_CLIENT_IP")) 
      {
         $ip = getenv("HTTP_CLIENT_IP"); 
      } 
      elseif(getenv("HTTP_X_FORWARDED_FOR")) 
      {
         $ip = getenv("HTTP_X_FORWARDED_FOR");
         if (strstr($ip, ',')) 
         {
            $tmp = explode (',', $ip); $ip = trim($tmp[0]); 
         } 
      } 
      else 
      { 
         $ip = getenv("REMOTE_ADDR"); 
      } 
      return $ip;
   } 

//kullanımı

echo GercekIP();
?>

 

Online Kullanıcı Sayısı

Artık php kodlarına veritabanı bağlantısı yazılması gerektiğini söylememe gerek yoktur sanırım.

CREATE TABLE online (
  zaman bigint(20) NOT NULL default '0',
  ip varchar(15) NOT NULL default ''
)
<?php
   function GetIP()// kullanıcı ip bulma
   {
      if(getenv("HTTP_CLIENT_IP")) 
      {
         $ip = getenv("HTTP_CLIENT_IP"); 
      } 
      elseif(getenv("HTTP_X_FORWARDED_FOR")) 
      {
         $ip = getenv("HTTP_X_FORWARDED_FOR");
         if (strstr($ip, ',')) 
         {
            $tmp = explode (',', $ip); $ip = trim($tmp[0]); 
         } 
      } 
      else 
      { 
         $ip = getenv("REMOTE_ADDR"); 
      } 
      return $ip;
   } 

$zaman = time();  // O andaki zaman
$timeout_suresi = $zaman - 300; // 300 saniye yani 5 dakika kendine göre değiştirebilirsin
$guncelleme_suresi = $zaman - 30; // 30 saniye
$ip = GetIP(); // Siteye giren kullanıcının ip'si

$sorgu = mysql_query("SELECT ip FROM online WHERE ip = '".$ip."' LIMIT 1");
$say = mysql_num_rows($sorgu);

// Kullanıcının ip'si ile veritabanında kayıt yoksa veritabanına kayıt yapıyoruz
if($say < 1){

     mysql_query("INSERT INTO online (zaman, ip) VALUES ('".$zaman."', '".$ip."')");

}else{

     // Kullanıcının en son online zamanını almak için yukarıdaki sorguyu kullanarak verileri çekiyoruz
     $veri = mysql_fetch_array($sorgu);

     // Kullanıcının son online olma süresi güncelleme süresinden küçükse kaydını güncelliyoruz
     if($veri['zaman'] < $guncelleme_suresi){

     // Online kaydını güncelliyoruz
     mysql_query("UPDATE online SET zaman = '".$zaman".' WHERE ip = '".$ip."' LIMIT 1");

     }

}

// Timeout süresi dolmuş kullanıcıların kaydını veritabanından siliyoruz
mysql_query("DELETE FROM online WHERE zaman < ".$timeout_suresi);

$top_sorgu = mysql_query("SELECT * FROM online");
// Kaç kullanıcının online olduğunu buluyoruz
$toplam_online = mysql_num_rows($top_sorgu);

// Kaç kullanıcının online olduğunu ekrana bastırıyoruz
echo "$toplam_online kullanıcı şu anda sitede online";
?>

 

Youtube Oembed JSON API

<?php 
$url = "http://www.youtube.com/watch?v=CvBfHwUxHIk";//burası değişken
$oembedurlcreate = "http://www.youtube.com/oembed?url=".$url."&format=json";
$curl = curl_init($oembedurlcreate); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$html=curl_exec($curl);
curl_close($curl);
$result = json_decode($html, true);
echo $result["thumbnail_url"]."<br>";//http://i.ytimg.com/vi/CvBfHwUxHIk/hqdefault.jpg 
echo $result["provider_url"]."<br>";//http://www.youtube.com/ 
echo $result["thumbnail_height"]."<br>";//360 
echo $result["type"]."<br>";//video 
echo $result["provider_name"]."<br>";//YouTube 
echo $result["version"]."<br>";// 1.0 
echo $result["html"]."<br>";//html5 video oynacıtıcını görüntüler  
echo $result["author_name"]."<br>";//RihannaVEVO
echo $result["width"]."<br>";//459 
echo $result["title"]."<br>";//Rihanna - Umbrella (Orange Version) ft. JAY-Z
echo $result["height"]."<br>";//344 
echo $result["thumbnail_width"]."<br>";//480 
echo $result["author_url"]."<br>";//http://www.youtube.com/user/RihannaVEVO
?>

WordPress Unutulan Şifre Üretici Phpass

indir

Unutulan wordpress şifrelerinizi el ile phpmyadmin’den bu kod yardımıyla değiştirebilirsiniz.

<?php
class PasswordHash {
 var $itoa64;
 var $iteration_count_log2;
 var $portable_hashes;
 var $random_state;
 function PasswordHash($iteration_count_log2, $portable_hashes)
 {
 $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

 if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
 $iteration_count_log2 = 8;
 $this->iteration_count_log2 = $iteration_count_log2;
 $this->portable_hashes = $portable_hashes;
 $this->random_state = microtime() . uniqid(rand(), TRUE); // removed getmypid() for compatibility reasons
 }
 function get_random_bytes($count)
 {
 $output = '';
 if ( @is_readable('/dev/urandom') &&
 ($fh = @fopen('/dev/urandom', 'rb'))) {
 $output = fread($fh, $count);
 fclose($fh);
 }
 if (strlen($output) < $count) {
 $output = '';
 for ($i = 0; $i < $count; $i += 16) {
 $this->random_state =
 md5(microtime() . $this->random_state);
 $output .=
 pack('H*', md5($this->random_state));
 }
 $output = substr($output, 0, $count);
 }
 return $output;
 }
 function encode64($input, $count)
 {
 $output = '';
 $i = 0;
 do {
 $value = ord($input[$i++]);
 $output .= $this->itoa64[$value & 0x3f];
 if ($i < $count)
 $value |= ord($input[$i]) << 8;
 $output .= $this->itoa64[($value >> 6) & 0x3f];
 if ($i++ >= $count)
 break;
 if ($i < $count)
 $value |= ord($input[$i]) << 16;
 $output .= $this->itoa64[($value >> 12) & 0x3f];
 if ($i++ >= $count)
 break;
 $output .= $this->itoa64[($value >> 18) & 0x3f];
 } while ($i < $count);

 return $output;
 }
 function gensalt_private($input)
 {
 $output = '$P$';
 $output .= $this->itoa64[min($this->iteration_count_log2 +
 ((PHP_VERSION >= '5') ? 5 : 3), 30)];
 $output .= $this->encode64($input, 6);

 return $output;
 }
 function crypt_private($password, $setting)
 {
 $output = '*0';
 if (substr($setting, 0, 2) == $output)
 $output = '*1';
 $id = substr($setting, 0, 3);
 if ($id != '$P$' && $id != '$H$')
 return $output;
 $count_log2 = strpos($this->itoa64, $setting[3]);
 if ($count_log2 < 7 || $count_log2 > 30)
 return $output;
 $count = 1 << $count_log2;
 $salt = substr($setting, 4, 8);
 if (strlen($salt) != 8)
 return $output;
 if (PHP_VERSION >= '5') {
 $hash = md5($salt . $password, TRUE);
 do {
 $hash = md5($hash . $password, TRUE);
 } while (--$count);
 } else {
 $hash = pack('H*', md5($salt . $password));
 do {
 $hash = pack('H*', md5($hash . $password));
 } while (--$count);
 }
 $output = substr($setting, 0, 12);
 $output .= $this->encode64($hash, 16);
 return $output;
 }
 function gensalt_extended($input)
 {
 $count_log2 = min($this->iteration_count_log2 + 8, 24);
 $count = (1 << $count_log2) - 1;
 $output = '_';
 $output .= $this->itoa64[$count & 0x3f];
 $output .= $this->itoa64[($count >> 6) & 0x3f];
 $output .= $this->itoa64[($count >> 12) & 0x3f];
 $output .= $this->itoa64[($count >> 18) & 0x3f];
 $output .= $this->encode64($input, 3);
 return $output;
 }
 function gensalt_blowfish($input)
 {
 $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 $output = '$2a$';
 $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
 $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
 $output .= '$';
 $i = 0;
 do {
 $c1 = ord($input[$i++]);
 $output .= $itoa64[$c1 >> 2];
 $c1 = ($c1 & 0x03) << 4;
 if ($i >= 16) {
 $output .= $itoa64[$c1];
 break;
 }
 $c2 = ord($input[$i++]);
 $c1 |= $c2 >> 4;
 $output .= $itoa64[$c1];
 $c1 = ($c2 & 0x0f) << 2;
 $c2 = ord($input[$i++]);
 $c1 |= $c2 >> 6;
 $output .= $itoa64[$c1];
 $output .= $itoa64[$c2 & 0x3f];
 } while (1);

 return $output;
 }
 function HashPassword($password)
 {
 $random = '';
 if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
 $random = $this->get_random_bytes(16);
 $hash =
 crypt($password, $this->gensalt_blowfish($random));
 if (strlen($hash) == 60)
 return $hash;
 }
 if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
 if (strlen($random) < 3)
 $random = $this->get_random_bytes(3);
 $hash =
 crypt($password, $this->gensalt_extended($random));
 if (strlen($hash) == 20)
 return $hash;
 }
 if (strlen($random) < 6)
 $random = $this->get_random_bytes(6);
 $hash =
 $this->crypt_private($password,
 $this->gensalt_private($random));
 if (strlen($hash) == 34)
 return $hash;
 return '*';
 }
 function CheckPassword($password, $stored_hash)
 {
 $hash = $this->crypt_private($password, $stored_hash);
 if ($hash[0] == '*')
 $hash = crypt($password, $stored_hash);
 return $hash === $stored_hash;
 }
}
$password="12345678";//buraya istediğiniz şifre
$wp_hasher = new PasswordHash( 8, true );
$hashed = $wp_hasher->HashPassword( $password );
if($wp_hasher->CheckPassword($password, $hashed)){echo "Doğru Şifre";}else{echo "Hatalı Şifre";}

echo "<br>Şifreniz: ".$password;
echo "<br>Veritabanı Kodunuz: ".$hashed;//mysql wp_users.user_pass kısmına yazabileceğiniz hash(veritabanına eklenecek şifre karma kodu)
?>

Dizin Seçerek Toplu Dosya Yükleme

<?php
header('Content-Type: text/html; charset=utf-8');
if (!file_exists("dosya")) { 
mkdir("dosya", 0777);
header("Location: ?klasor=olusturuldu");
}
$sayac = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    foreach ($_FILES['dosya']['name'] as $i => $isim) {
        if (strlen($_FILES['dosya']['name'][$i]) > 1) {
            if (move_uploaded_file($_FILES['dosya']['tmp_name'][$i], 'dosya/'.$isim)) {
                $sayac++;
            }
        }
    }
}
if ($sayac > 0) {echo "<p>{$sayac} dosya yüklendi</p>";}
?>
<form method="post" enctype="multipart/form-data">
  <input type="file" name="dosya[]" multiple="" directory="" webkitdirectory="" mozdirectory="">
  <input class="button" type="submit" value="Yükle" />
</form>

 

Sprintf ile Rakam E+ Ondalık Özelleştirme

 $a=496787459.234230497098723489;
$b=349807256234.2397823;
$c=$a+$b;
echo $c."<br>";;//350304043693.47
echo sprintf('%.20e',$c)."<br>";//3.50304043693474060059e+11
echo sprintf('%.20f',$c)."<br>";#en çok kullanılan//350304043693.47406005859375000000
echo sprintf('%.20s',$c)."<br>";//350304043693.47
echo sprintf('%e',$c)."<br>";//3.503040e+11
echo sprintf('%f',$c)."<br>";//350304043693.474060
echo sprintf('%s',$c)."<br>";//350304043693.47
echo sprintf("%10.10e", $c)."<br>";//3.5030404369e+11
echo sprintf("%10.10f", $c)."<br>";//350304043693.4740600586
echo sprintf("%10.10s", $c)."<br>";//3503040436

Bir başka örnek

 $basla=microtime();
$son=microtime(); 
$fark=abs($basla-$son);
echo "<br>".sprintf('%.6f', $fark)." saniyede yüklendi";

Itranslate4 Çeviri API

Not:Sunucu kaynaklı uzun kelimelerde çeviri süresi uzamaktadır.

<?php
$basla=microtime(); 
header('Content-Type: text/html; charset=utf-8');
?><pre>
<form action="" method="post"><textarea name="text" maxlenght="500" cols="75" rows="5"></textarea><br>
<input type="submit" value="Çevir"/></form>
<?php
function it4cevir($text,$from,$to){
 $data='{"src":"'.$from.'","trg":"'.$to.'","dat":"'.$text.'","dom":"","uid":"3ba5c1b2494446deac64500ff977171f","type":"text","trs_open_count":6,"trs_max_count":100}';
 $url="http://itranslate4.eu/csa?func=translate&origin=text&data=".rawurlencode($data);
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_URL, $url);
 $html = curl_exec($ch);
 curl_close($ch);
 $e=json_decode($html);
 $data='{"tid":"'.$e->tid.'"}';
 $r="1111111";
 $a="9999999";
 $t="111111111";
 $b="999999999";
 $url="http://itranslate4.eu/csa?func=translate_poll&rand=0.".rand($r,$a).rand($t,$b)."&data=".rawurlencode($data);
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_URL, $url);
 $html = curl_exec($ch);
 curl_close($ch);
 $a=(json_decode($html));
 if(isset($a->dat[0]->sgms[0])){
 return ($a->dat[0]->sgms[0]->units[0]->text);
 }else{
 return it4cevir($text);
 }
}
if(isset($_POST["text"])){
echo it4cevir($_POST["text"],"en","tr");//ingilizceden türkçeye çeviriyoruz
}
$son=microtime(); 
echo "<p>".abs($basla-$son)." saniyede yüklendi";

 

Wordlingo Çeviri API

function worldlingocevir($text,$from,$to){
$url='http://www.worldlingo.com/S000.1/api';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"wl_data=".rawurlencode($text)."&".
"wl_srclang=".$from."&".
"wl_trglang=".$to."&".
"wl_srcenc=UTF-8&".
"wl_trgenc=UTF-8&".
"wl_password=secret&".
"wl_mimetype=text/plain&".
"wl_opt=1&".
"wl_errorstyle=1&".
"wl_dictno=&".
"Submit=Submit"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}

Kullanımı

 echo worldlingocevir("what is your name","en","tr");