魏长东

weichangdong

正在查看: php 分类下的文章(第 2 页 / 共 94 篇)

php 科学计数法还原

有时候需要用php的microtime做唯一的id,但是生成之后,显示出来就成科学记数法了,用下面代码可以还原。
function NumToStr($num){ 
    if (stripos($num,'e')===false) return $num; 
   // $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');//出现科学计数法,还原成字符串 
    $result = ""; 
    while ($num > 0){ 
        $v = $num - floor($num / 10)*10; 
        $num = floor($num / 10); 
        $result   =   $v . $result; 
    }//开源代码phpfensi.com 
    return $result; 
}

elastic search扫描模式

function makeLangAction()
    {
        $es = self::connectEs();
        $params = array(  
            "search_type" => "scan",
            "scroll" => "30s",
            "size" => 50,
            "index" => "360weather",  
            "body" => array(  
                "query" => array(  
                    "match_all" => array()  
                )  
            )  
        );  
          
        $docs = $es->search($params);
        $scroll_id = $docs['_scroll_id'];
        $redis = new RedisModel('write');
        echo "all num ".$docs['hits']['total']."\n";
        $num = 0;
        $all_tz = [];
        while (true) {   
            $response = $es->scroll(  
                array(  
                    "scroll_id" => $scroll_id,
                    "scroll" => "30s",
                )  
            );  
            if (count($response['hits']['hits']) > 0) {  
                //loop $response['hits']['hits'] data
                $scroll_id = $response['_scroll_id'];
                //$num += count($response['hits']['hits']); 
                foreach ($response['hits']['hits'] as $tmp) {
                    $tt = explode('_', $tmp['_id']);
                    $loc_id = $tt[0];
                    $data = $tmp['_source'];
                    $lang = $data['lang'];
                    $lang_info = json_encode([
                        'area' => $data['country_code'],
                        'province' => $data['province'],
                        'name' => $data['city'],
                        'tz' => $data['tz'],
                    ]);
                    $tz_num = Functions::computerTz($data['tz']);
                    /*
                    if (isset($all_tz[$data['tz']])) {
                        
                    } else {
                        $all_tz[$data['tz']] = $tz_num;
                    }
                    */
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,self::REDIS_CITY_LANG.$lang,$lang_info);
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,'tz',$tz_num);
                    $num++;
                } 
            } else {   
                break;  
            }  
        }
        
        echo 'tz num '.count($all_tz)."\n";
        print_R($all_tz);
        
        echo "set num ". $num ."\n";  
    }

时区大全

http://www.zeitverschiebung.net/en/difference/timezone/Australia/Broken_Hill/timezone/asia--shanghai
时区和utc时区相差小时数
    [Asia/Riyadh] => 3
    [Asia/Tehran] => 3.5
    [Asia/Yerevan] => 4
    [Africa/Lusaka] => 2
    [Africa/Nairobi] => 3
    [Africa/Kigali] => 2
    [Africa/Lubumbashi] => 2
    [Africa/Kinshasa] => 1
    [Africa/Djibouti] => 3
    [Africa/Kampala] => 3
    [Africa/Bangui] => 1
    [Africa/Ndjamena] => 1
    [Asia/Amman] => 3
    [Africa/Blantyre] => 2
    [Asia/Nicosia] => 3
    [Africa/Luanda] => 1
    [Africa/Dar_es_Salaam] => 3
    [Asia/Baku] => 4
    [Indian/Mahe] => 4
    [Asia/Ashgabat] => 5
    [Asia/Damascus] => 3
    [Europe/Athens] => 3
    [Africa/Mogadishu] => 3
    [Asia/Aden] => 3
    [Africa/Tripoli] => 2
    [Asia/Baghdad] => 3
    [Africa/Addis_Ababa] => 3
    [Africa/Asmara] => 3
    [Africa/Cairo] => 2
    [Africa/Khartoum] => 3
    [Asia/Jerusalem] => 3
    [Asia/Kuwait] => 3
    [Asia/Qatar] => 3
    [Asia/Muscat] => 4
    [Europe/Riga] => 3
    [Europe/Moscow] => 3
    [Europe/Volgograd] => 3
    [Europe/Tirane] => 2
    [Asia/Yekaterinburg] => 5
    [Europe/Ulyanovsk] => 4
    [Europe/Samara] => 4
    [Europe/Astrakhan] => 4
    [Europe/Kaliningrad] => 2
    [Europe/Istanbul] => 3
    [Africa/Bujumbura] => 2
    [Asia/Beirut] => 3
    [Asia/Gaza] => 3
    [Asia/Dubai] => 4
    [Asia/Bahrain] => 3
    [Europe/Zaporozhye] => 3
    [Europe/Simferopol] => 3
    [Europe/Tallinn] => 3
    [Asia/Tbilisi] => 4
    [Europe/Chisinau] => 3
    [Europe/Minsk] => 3
    [Europe/Vilnius] => 3
    [Asia/Samarkand] => 5
    [Europe/Stockholm] => 2
    [Asia/Aqtobe] => 5
    [Asia/Aqtau] => 5
    [Asia/Almaty] => 6
    [Asia/Oral] => 5
    [Europe/Helsinki] => 3
    [Europe/Kiev] => 3
    [Europe/Uzhgorod] => 3
    [Europe/Bucharest] => 3
    [Europe/Bratislava] => 2
    [Europe/Sofia] => 3
    [Europe/Budapest] => 2
    [Europe/Skopje] => 2
    [Europe/Belgrade] => 2
    [Europe/Oslo] => 2
    [Europe/Warsaw] => 2
    [Europe/Podgorica] => 2
    [Asia/Kabul] => 4.5
    [Asia/Bangkok] => 7
    [Asia/Karachi] => 5
    [Asia/Dushanbe] => 5
    [Asia/Kuala_Lumpur] => 8
    [Asia/Colombo] => 5.5
    [Africa/Johannesburg] => 2
    [Asia/Bishkek] => 6
    [Asia/Thimphu] => 6
    [Asia/Kolkata] => 5.5
    [Africa/Maseru] => 2
    [Africa/Gaborone] => 2
    [Africa/Harare] => 2
    [Africa/Maputo] => 2
    [Indian/Antananarivo] => 3
    [Indian/Mayotte] => 3
    [Indian/Comoro] => 3
    [Indian/Mauritius] => 4
    [Africa/Mbabane] => 2
    [Indian/Reunion] => 4
    [Africa/Windhoek] => 2
    [Asia/Dhaka] => 6
    [Asia/Jakarta] => 7
    [Asia/Pontianak] => 7
    [Asia/Tashkent] => 5
    [Asia/Makassar] => 8
    [Indian/Maldives] => 5
    [Asia/Krasnoyarsk] => 7
    [Asia/Barnaul] => 7
    [Asia/Novokuznetsk] => 7
    [Asia/Novosibirsk] => 6
    [Asia/Irkutsk] => 8
    [Asia/Omsk] => 6
    [Asia/Tomsk] => 7
    [Asia/Urumqi] => 6
    [Asia/Kashgar] => 6
    [Asia/Kathmandu] => 5.75
    [Asia/Rangoon] => 6.5
    [Asia/Chongqing] => 8
    [Asia/Qyzylorda] => 6
    [Asia/Manila] => 8
    [Pacific/Palau] => 9
    [Asia/Ho_Chi_Minh] => 7
    [Asia/Jayapura] => 9
    [Indian/Cocos] => 6.5
    [Asia/Ulaanbaatar] => 8
    [Indian/Kerguelen] => 5
    [Asia/Vientiane] => 7
    [Asia/Taipei] => 8
    [Asia/Shanghai] => 8
    [Pacific/Pohnpei] => 11
    [Asia/Dili] => 9
    [Asia/Kuching] => 8
    [Asia/Harbin] => 8
    [Asia/Pyongyang] => 8.5
    [Asia/Tokyo] => 9
    [Asia/Seoul] => 9
    [Asia/Yakutsk] => 9
    [Asia/Vladivostok] => 10
    [Asia/Khandyga] => 9
    [Pacific/Port_Moresby] => 10
    [Pacific/Guadalcanal] => 11
    [Pacific/Tarawa] => 12
    [Pacific/Efate] => 11
    [Pacific/Noumea] => 11
    [Australia/Melbourne] => 11
    [Australia/Lindeman] => 10
    [Australia/Sydney] => 11
    [Australia/Brisbane] => 10
    [Pacific/Majuro] => 12
    [Asia/Kamchatka] => 12
    [Asia/Magadan] => 11
    [Asia/Ust-Nera] => 10
    [Asia/Sakhalin] => 11
    [Asia/Anadyr] => 12
    [Australia/Currie] => 11
    [Australia/Hobart] => 11
    [Australia/Adelaide] => 10.5
    [Pacific/Auckland] => 13
    [Australia/Darwin] => 9.5
    [Australia/Perth] => 8
    [Indian/Christmas] => 7
    [Pacific/Kosrae] => 11
    [Asia/Singapore] => 8
    [Pacific/Funafuti] => 12
    [Asia/Choibalsan] => 8
    [Asia/Brunei] => 8
    [Asia/Macau] => 8
    [Asia/Phnom_Penh] => 7
    [Asia/Hong_Kong] => 8
    [Africa/Dakar] => 0
    [Africa/Brazzaville] => 1
    [Australia/Broken_Hill] => 10.5
    [Europe/Lisbon] => 1
    [Atlantic/Madeira] => 1
    [Africa/Monrovia] => 0
    [Africa/Tunis] => 1
    [Africa/Algiers] => 1
    [Africa/Abidjan] => 0
    [Africa/Accra] => 0
    [Africa/Malabo] => 1
    [Africa/Douala] => 1
    [Pacific/Fiji] => 12
    [Europe/Madrid] => 2
    [Africa/Ceuta] => 2
    [Africa/Lagos] => 1
    [Africa/Ouagadougou] => 0
    [Africa/Lome] => 0
    [Africa/Bissau] => 0
    [Africa/Nouakchott] => 0
    [Africa/Porto-Novo] => 1
    [Africa/Libreville] => 1
    [Africa/Freetown] => 0
    [Africa/Sao_Tome] => 0
    [Africa/Banjul] => 0
    [Atlantic/Canary] => 1
    [Africa/Niamey] => 1
    [Africa/Bamako] => 0
    [Africa/El_Aaiun] => 1
    [Atlantic/St_Helena] => 0
    [Africa/Conakry] => 0
    [Europe/Gibraltar] => 2
    [Africa/Casablanca] => 1
    [Europe/Malta] => 2
    [Europe/Rome] => 2
    [Europe/Copenhagen] => 2
    [Atlantic/Faroe] => 1
    [Europe/Vienna] => 2
    [Europe/London] => 1
    [Atlantic/Reykjavik] => 0
    [Europe/Dublin] => 1
    [Europe/Zurich] => 2
    [Arctic/Longyearbyen] => 2
    [Europe/Amsterdam] => 2
    [Europe/Berlin] => 2
    [Europe/Brussels] => 2
    [Europe/Paris] => 2
    [Europe/Luxembourg] => 2
    [Europe/Monaco] => 2
    [Europe/Andorra] => 2
    [Europe/Vaduz] => 2
    [Europe/Jersey] => 1
    [Europe/Guernsey] => 1
    [Europe/Prague] => 2
    [Europe/Isle_of_Man] => 1
    [Europe/San_Marino] => 2
    [Europe/Ljubljana] => 2
    [Europe/Sarajevo] => 2
    [Europe/Zagreb] => 2
    [Atlantic/Azores] => 0
    [America/Barbados] => -4
    [America/Maceio] => -3
    [America/Fortaleza] => -3
    [America/Belem] => -3
    [America/Araguaina] => -3
    [America/Manaus] => -4
    [America/Recife] => -3
    [America/Godthab] => -2
    [Atlantic/Cape_Verde] => -1
    [America/Guyana] => -4
    [America/Cayenne] => -3
    [America/Paramaribo] => -3
    [America/Asuncion] => -3
    [America/Montevideo] => -3
    [America/Miquelon] => -2
    [Atlantic/South_Georgia] => -2
    [Atlantic/Stanley] => -3
    [America/Argentina/Buenos_Aires] => -3
    [America/Argentina/Tucuman] => -3
    [America/La_Paz] => -4
    [America/Sao_Paulo] => -3
    [America/Bahia] => -3
    [America/Campo_Grande] => -4
    [America/Cuiaba] => -4
    [America/Argentina/Cordoba] => -3
    [America/Martinique] => -4
    [America/Nassau] => -4
    [America/Port_of_Spain] => -4
    [America/St_Kitts] => -4
    [America/Dominica] => -4
    [America/Havana] => -4
    [America/Anguilla] => -4
    [America/Antigua] => -4
    [America/St_Lucia] => -4
    [Atlantic/Bermuda] => -3
    [America/St_Vincent] => -4
    [America/Montserrat] => -4
    [America/Guadeloupe] => -4
    [America/Grenada] => -4
    [America/Cayman] => -5
    [America/Belize] => -6
    [America/El_Salvador] => -6
    [America/Guatemala] => -6
    [America/Aruba] => -4
    [America/Grand_Turk] => -4
    [America/Santo_Domingo] => -4
    [America/Curacao] => -4
    [America/Lower_Princes] => -4
    [America/Mexico_City] => -5
    [America/Merida] => -5
    [America/Tegucigalpa] => -6
    [America/Managua] => -6
    [America/Monterrey] => -5
    [America/Caracas] => -4
    [America/Jamaica] => -5
    [America/Cancun] => -5
    [America/Costa_Rica] => -6
    [America/Tortola] => -4
    [America/Panama] => -5
    [America/Port-au-Prince] => -5
    [America/Lima] => -5
    [America/Bogota] => -5
    [America/Thule] => -3
    [America/Argentina/Jujuy] => -3
    [America/Argentina/Mendoza] => -3
    [America/Santiago] => -3
    [America/Argentina/Ushuaia] => -3
    [America/Rio_Branco] => -5
    [America/Porto_Velho] => -4
    [America/Argentina/San_Juan] => -3
    [America/Mazatlan] => -6
    [America/Tijuana] => -7
    [America/Guayaquil] => -5
    [America/Argentina/Rio_Gallegos] => -3
    [America/Hermosillo] => -7
    [Pacific/Galapagos] => -6
    [America/Boa_Vista] => -4
    [America/Argentina/Catamarca] => -3
    [America/Argentina/La_Rioja] => -3
    [America/Eirunepe] => -5
    [America/Chihuahua] => -6
    [Pacific/Fakaofo] => 13
    [Pacific/Tongatapu] => 13
    [Pacific/Tahiti] => -10
    [Pacific/Wallis] => 12
    [Pacific/Apia] => 14
    [Pacific/Pitcairn] => -8
    [America/Indiana/Knox] => -5
    [America/Chicago] => -5
    [America/New_York] => -4
    [America/Kentucky/Monticello] => -4
    [Pacific/Rarotonga] => -10
    [Pacific/Niue] => -11
    [Pacific/Guam] => 10
    [Pacific/Saipan] => 10
    [America/Indiana/Marengo] => -4
    [America/Indiana/Vevay] => -4
    [Pacific/Gambier] => -9
    [America/Indiana/Petersburg] => -4
    [Pacific/Easter] => -5
    [America/North_Dakota/New_Salem] => -5
    [America/Kentucky/Louisville] => -4
    [America/Detroit] => -4
    [America/Indiana/Indianapolis] => -4
    [America/Indiana/Vincennes] => -4
    [America/Indiana/Tell_City] => -5
    [America/Puerto_Rico] => -4
    [America/St_Thomas] => -4
    [America/Menominee] => -5
    [America/North_Dakota/Center] => -5
    [America/Los_Angeles] => -7
    [America/Phoenix] => -7
    [America/Denver] => -6
    [America/Shiprock] => -6
    [America/Boise] => -6
    [America/Juneau] => -8
    [America/Yakutat] => -8
    [America/Edmonton] => -6
    [America/Dawson_Creek] => -7
    [America/Regina] => -6
    [America/Swift_Current] => -6
    [America/Montreal] => -4
    [America/Vancouver] => -7
    [America/Resolute] => -5
    [America/Goose_Bay] => -3
    [America/Toronto] => -4
    [America/Moncton] => -3
    [America/Iqaluit] => -4
    [America/St_Johns] => -2.5
    [America/Winnipeg] => -5
    [America/Halifax] => -3
    [America/Nipigon] => -4
    [America/Glace_Bay] => -3
    [America/Rankin_Inlet] => -5
    [America/Whitehorse] => -7
    [America/Rainy_River] => -5
    [America/Anchorage] => -8
    [America/Nome] => -8
    [Pacific/Pago_Pago] => -11
    [America/Blanc-Sablon] => -4
    [America/Inuvik] => -6
    [America/Yellowknife] => -6
    [America/Pangnirtung] => -4
    [America/Cambridge_Bay] => -6
    [America/Adak] => -9
    [Pacific/Honolulu] => -10
    [America/Dawson] => -7
    [America/Thunder_Bay] => -4
    [Europe/Vatican] => 2
    [Antarctica/McMurdo] => 13

php swoole小试牛刀

一直觉得swoole很神奇,终于自己研究了一把,果然很牛逼。之前我实现队列,简单的就是while true死循环,一直查询数据库(mysql或者redis),如果有新数据就处理。稍微高级一点的就是,php fork子进程来处理,每处理完成一个就exit。

但是发现swoole实现起来,很简单。

»»阅读全文

elastic search php版本

今天接触elastic search,居然发现有php的sdk,然后操作elastic search想操作mysql数据库一样。不过那个sdk文件太多了,等有时间再去研究下。网上发现了一个php操作他的类,其实也可以用的。

»»阅读全文

解决thinkphp分页缓存bug

遇到一个问题,thinkphp框架用了smarty,用了缓存,造成分页有缓存,网上搜了下,好多人也遇到这种问题。

总体的解决思路是:smarty局部缓存的方法。以下方法,自己试了,可行有效。

php中自定义块

//smarty 3
// function declaration 如果使用是在类里面,就吧这个函数放到公共函数代码里面。不然会报错说
//找不到这个函数

function smarty_block_nocache ($param,$content,$smarty)
{
  return $content;
}
 
// register with smarty
$smarty->registerPlugin("function","nocache", "smarty_block_nocache");

Smarty3是用registerPlugin , Smarty2则是用register_block,我用的是smarty3.

但是这么做,有一点不方便就是,需要在代码里面重新调用Smarty类文件,所以下面这个方法更加好点。

给smarty写插件,函数,自己在n年前就已经干过了。

在Smarty/plugins目录下建一个文件 block.nocache.php 内容如下: 

<?php
function smarty_block_nocache($param, $content, $smarty)  
{  
      return $content;   
}  
?>

模板调用和上面一样。

php的trait用法

/在facebook的php的sdk中 用法就是假如某一个类不让使用delete方法,就会用到这个trait

当这个类被调用delete这个方法的时候,就会触发trait。

还有fb的类 set某个值的时候,会调用__set方法,这个时候,使用trait,会对需要set的key和value做下

检查判断

trait myTrait{
    function traitMethod1(){
        echo "no this func 1\n";
    }
    function traitMethod2(){
        echo "no this func 2\n";
    }

}

//然后是调用这个traits,语法为:
class myClass{
    use myTrait;
}

//这样就可以通过use myTraits,调用Traits中的方法了,比如:
$obj = new myClass();
$obj->traitMethod1();
$obj->traitMethod2(); 

run result:

no this func 1 no this func 2

»»阅读全文

php get_class vs get_called_calss

class foo  {
    static public function  test () {
         var_dump(get_class());
    }
}

class bar  extends  foo {
}

foo::test();
bar::test();

echo "====================\n\n";

class  foo2  {
    static public function  test () {
         var_dump(get_called_class());
    }
}

class  bar2  extends  foo2  {
}

foo2::test();
bar2::test();

»»阅读全文