首页 > php代码

php 显示今天是星期几与一年的每几天代码

php 显示今天是星期几与一年的每几天代码

public function getPart ($part) {
  $source = $this->timestamp;
  switch($part)
  {
   case 'yy' : $result = intval(date("Y", $source)); break; //年
   case 'mm' : $result = intval(date("m", $source)); break; //月
   case 'dd' : $result = intval(date("d", $source)); break; //日
   case 'w'  : $result = intval(date("N", $source)); break; //星期 [1-7] "1"表示星期一
   case 'cw' : $index = date("N", $source); //中文星期
      $week_array = array('1'=>'一', '2'=>'二', '3'=>'三', '4'=>'四', '5'=>'五', '6'=>'六', '7'=>'日');
      $result = '星期' . $week_array[$index];

阅读全文

php 批量保存数据与批量更新数据

if ($insert && $insertsql) {php 批量保存数据与批量更新数据
  $keystr = $valstr = $tmp = '';
  foreach($insertsql as $key => $val) {
   if ($val) {
    $keystr .= $tmp.$key;
    $valstr .= $tmp."'".addslashes($val)."'";
    $tmp = ',';
   }
  }
  if ($keystr && $valstr) {
   dbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
   m(q("INSERT INTO $tablename ($keystr) VALUES ($valstr)") ? 'Insert new record of success' : mysql_error());
  }
 }
 if ($update && $insertsql && $base64) {
  $valstr = $tmp = '';
  foreach($insertsql as $key => $val) {
   $valstr .= $tmp.$key."='".addslashes($val)."'";
   $tmp = ',';
  }
  if ($valstr) {
   $where = base64_decode($base64);
   dbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
   m(q("UPDATE $tablename SET $valstr WHERE $where LIMIT 1") ? 'Record updating' : mysql_error());
  }
 }

阅读全文

php 类的静态变量的初始化

共有的成员还有办法解决,例如:
class A {
static public $child;
}
A::$child = new B();

对于私有的成员似乎就没有什么干净的方法了,只能这样做:
class A {
static private $child;
static public initialize() {
self::$child = new B();
}
}
A::initialize();

阅读全文

php 获取系统当前路径与检查php配置参数

php 获取系统当前路径与检查php配置参数
function getPath($mainpath, $relativepath) {
  global $dir;
  $mainpath_info           = explode('/', $mainpath);
  $relativepath_info       = explode('/', $relativepath);
  $relativepath_info_count = count($relativepath_info);
  for ($i=0; $i<$relativepath_info_count; $i++) {
   if ($relativepath_info[$i] == '.' || $relativepath_info[$i] == '') continue;
   if ($relativepath_info[$i] == '..') {
    $mainpath_info_count = count($mainpath_info);
    unset($mainpath_info[$mainpath_info_count-1]);
    continue;
   }
   $mainpath_info[count($mainpath_info)] = $relativepath_info[$i];
  } //end for
  return implode('/', $mainpath_info);
 }

阅读全文

php 复制目录及目录下所有文件

function copy($from, $to) {
  if ($this->abspath($to)=="/") $to=$this->basedir;
  if ($this->dirname($from) == $this->dirname($to)) $to = $this->dirname($to).'/复件'.basename($from);
  if (!is_dir($from)) {
   return @copy($from, $to);
  } else {
   if (!is_dir($to)) @mkdir($to);
   $path = opendir($from);
   while( $file = readdir( $path ) ) {
    if (($file=='.')||($file=='..')) continue;
    if (is_dir($from.'/'.$file)) $this->copy($from.'/'.$file, $to.'/'.$file);
    else echo basename($file), copy($from.'/'.$file, $to.'/'.$file) ? ' Success!' : ' False.', '<br />';
   }
   return true;
  }
 }

阅读全文

php 批量删除文件

php 批量删除文件
 elseif($doing == 'delfiles') {
  if ($dl) {
   $dfiles='';
   $succ = $fail = 0;
   foreach ($dl as $filepath => $value) {
    if (@unlink($filepath)) {
     $succ++;
    } else {
     $fail++;
    }
   }
   m('删除文件 '.count($dl).' 成功 '.$succ.' 失败 '.$fail);
  } else {
   m('请选择文件');
  }

阅读全文

php 对数组排序实例代码

php 对数组排序实例代码
  * 对数组排序
  * @param array $array 操作的数组
  * @param string $type key按键排序,value按值排序
  * @param string $field 字段名
  * @param string $order 排序方式asc顺序desc逆序
  * @return void
  */
 public static function sort(&$array, $type = 'value', $field = NULL, $order = 'asc') {
  if ($field) {
   foreach ($array as $key => $value) {
    $temp[$key] = $value[$field];
   }
   if ($order=='asc') {
    asort($temp);
   } else {
    arsort($temp);
   }
   $newarray = array();
   foreach ($temp as $key => $value) {
    $newarray[] = $array[$key];
   }
   $array = $newarray;
  } else {
   if ($type=='key') {
    if ($order=='asc') {
     ksort($array);
    } else {
     krsort($array);
    }
   } else {
    if ($order=='asc') {
     asort($array);
    } else {
     arsort($array);
    }
   }
  }
  
 }

阅读全文

php 格式化输出日期函数

php 格式化输出日期函数

public function format ($formatTo='standard',$timestamp = NULL) {
  if ($timestamp!==NULL) {
   $source = $timestamp;
   if (!$source) {
    return '';
   }
  } else {
   $source = $this->timestamp;
  }
  
  switch (strtolower($formatTo))
  {
   case 'chinese' : //中文格式串“YYYY年MM月DD日

阅读全文