首页 > 编程技术 > php

PHP时间戳与日期之间转换

发布时间:2016-11-25 15:03

在php中如果要实现日期转换成时间戳我们可以直接使用strtotime函数,如果把时间戳转换成日期直接使用date()函数即可实现,下面我来给各们朋友介绍介绍。

strtotime()函数 strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。


例子

 代码如下 复制代码

<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

输出:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

上面是把日期转换成时间戳了,我们也可以把如 2013-04-21这种转换成时间戳

 代码如下 复制代码

$a = date();
$mk = strtotime($a)

要求只能在白天8:00-20:00发送短信,怎么样获得到每天的这段时间区间?

 代码如下 复制代码


<?
$y=date("Y",time());
$m=date("m",time());
$d=date("d",time());
$start_time = mktime(9, 0, 0, $m, $d ,$y);
$end_time = mktime(19, 0, 0, $m, $d ,$y);

$time = time();
if($time >= $start_time && $time <= $end_time)
{
// do something....
}
?>


下面来介绍把时间戳转换成日期


date()函数,

此函数不但可以获取各种各样的时间日期之外,还可以进行日期转换呼

 代码如下 复制代码

<?
$time = time();
$date = date("Y-m-d",$time);
echo 'www.111cn.net 提示'.$date
?>

这样就输出 www.111cn.net 提示 2013-04-21 。

在php开发时我们经常会碰到要删除数组中指定指定内容,但数组是特殊变量我们不能直接利用replace进行替换的,需要一些方法来操作,下面我来给大家介绍操作办法。

如果我们知道数组元素的名字这个就好办了

按键名来删除数组中指定数组元素

 代码如下 复制代码

$barray = array('a'=>1,'b'=>2,'wod'=>3,'c'=>4,'abc'=>5);
$del ='b';
unset($barray[$del]);//结果为


Array
(
    [a] => 1
    [wod] => 3
    [c] => 4
    [abc] => 5
)


如果有多个数组要同时删除,上面办法是解决不了,我们可以利用array_diff函数来操作

 代码如下 复制代码

$a1=array("Cat","Dog","Horse",'dff','dfdf','www');
$a2=array("dff","Horse","Dog");
$a1 = array_diff($a1,$a2);
sort($a1);
print_r($a1);

Array
(
    [0] => Cat
    [1] => dfdf
    [2] => www
)

例2

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false

 代码如下 复制代码


$array = array('1', '2', '3', '4', '5');

$del_value = 3;
unset($array[array_search($del_value , $array)]);//利用unset删除这个元素

print_r($array);

输出

array('1', '2', '4', '5');

array_filter()
调用方式:array_filter($array)
参数说明:$array 是操作的对象,我们将删除其中的空元素

实例:

 代码如下 复制代码

<?php
    $array = ('a' => "abc", 'b' => "bcd",'c' =>"cde",'d' =>"def",'e'=>"");
    array_filter($array);
    echo "<pre>";
    print_r($array);
?>

结果:

Array (

     [a] => abc

     [b] => bcd

     [c] => cde

    [d] => def

   )

上面人方法删除的都会不会重建索引的,下面我来给大家介绍一个删除数组元素并重建数组索引的方法

 代码如下 复制代码

function array_remove(&$arr,$offset){ 
    array_splice($arr,$offset,1); 


$a = array('a','b','c','d'); 
array_remove($a,2); 
print_r($a);

这种问题是你php中把错误提示开启了之后,会把你编程的一些错误给告诉你,下面我来给大家分析php Notice: Undefined index 错误提示方法,有需要的朋友可参考。

原因分析

出现这种问题一般是变量未定义造成的

 代码如下 复制代码

if($a)
{
 echo $a;
}
else
{

}

//提示:Notice: Undefined variable: a in E:/www/test.php on line 5

 代码如下 复制代码


//解决

$a=1;
if($a)
{
 echo $a;
}
else
{

}

//输出1
//在文章头部加

error_reporting(0);

if($a)
{
 echo $a;
}
else
{

}

解决方法总结

1、如果是变量未申请直接申名变量如 $a=1; 这样。
2、在文章头部加error_reporting(0);  所有错误都不提示
3、isset($_GET["page"])  或 :@$page=$_GET["page"]
4、用php.ini中error_reporting   =   E_ALL   &   ~E_NOTICE 可以关闭notice的显示,屏蔽掉此类警告好,

在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::,反之如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->。

另外,如果从类的内部访问const或者static变量或者方法,那么就必须使用自引用的self,反之如果从类的内部访问不为const或者static变量或者方法,那么就必须使用自引用的$this。

$this实例

 代码如下 复制代码

<?php

// this是指向当前对象的指针

class test_this{
    private $content; //定义变量
   
    function __construct($content){ //定义构造函数
          $this->content= $content;
    }
    function __destruct(){}//定义析构函数
   
    function printContent(){//定义打印函数
        echo $this->content.'<br />';
    }
}

$test=new test_this('北京欢迎你!'); //实例化对象
$test->printContent();//北京欢迎你!

::使用方法

 代码如下 复制代码

//parent是指向父类的指针

class test_parent{ //基类
    public $name;  //定义姓名  父类成员需要定义为public,才能够在继承类中直接使用 this来调用。
    function __construct($name){
        $this->name=$name;
    }
}
class test_son extends test_parent{ //派生类  继承test_parent
    public $gender;//定义性别
    public $age;    //定义年龄
    function __construct($gender,$age){ //继承类的构造函数
        parent::__construct('nostop');//使用parent调用父类的构造函数,来进行对父类的实例化
        $this->gender=$gender;
        $this->age=$age;
    }
    function __destruct(){}
    function print_info(){
        echo $this->name.'是个'.$this->gender.',今年'.$this->age.'岁'.'<br />';
    }
}

$nostop=new test_son('女性','22');//实例化test_son对象
$nostop->print_info();//执行输出函数  nostop是个女性,今年23岁


使用self::$name的形式。注意的是const属性的申明格式,const PI=3.14,而不是const $PI=3.14

 代码如下 复制代码

class clss_a {
    
     private static  $name="static class_a";
    
     const PI=3.14;
     public $value;   
        
     public static function getName()
     {
        return self::$name;   
     }
     //这种写法有误,静态方法不能访问非静态属性
     public static function getName2()
     {
         return self::$value;
     }
     public function getPI()
     {
       return self::PI;   
     }
    
    
 }

还要注意的一点是如果类的方法是static的,他所访问的属性也必须是static的。
在类的内部方法访问未声明为const及static的属性时,使用$this->value ='class_a';的形式。

面向对象编程是php中一种常用的使用方法,本文章来介绍php面向对象简单使用方法与一些基本知识有需要的朋友可进入参考。

(OOP)来开发。面向对象开发相对于面向过程有很多优点:
维护简单   模块化是面向对象编程中的一个特征。实体被表示为类和同一名字空间中具有相同功能的类,我们可以在名字空间中添加一个类而不会影响该名字空间的其他成员。
  可扩充性   面向对象编程从本质上支持扩充性。如果有一个具有某种功能的类,就可以很快地扩充这个类,创建一个具有扩充的功能的类。
代码重用   由于功能是被封装在类中的,并且类是作为一个独立实体而存在的,提供一个类库就非常简单了。
它比较适合多人合作来开发项目,所以现在很多大中型网站都选择了用OOP来开发。

下面我来来介绍面向对象编程

类与属性和方法

PHP中定义类语法格式:

 代码如下 复制代码
class classname [可选属性]{
public $property [=value];… //用public声明一个公共标识 然后给予一个变量 变量也可以赋值
function functionname ( args ){ //类的方法里的成员函数
代码} …
//类的方法(成员函数)
}

生成对象(类的实例化): $对象名=new classname( );
使用对象的属性

在一个类中,可以访问一个特殊指针$this当在该类中通过一个操作设置或访问该变量时,使用$this->name来引用.

 代码如下 复制代码

class person{
function _ _destruct( )
{ echo "bye bye !“; }
}
$a=new person();


1.final
final:php5新增一个final关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法;如果一个类被声明final,则不能被继承。

 代码如下 复制代码
 
class BaseClass{
     public function test(){
          ehco "test";
     }
 
     final public function moreTest(){
          echo "moretest";
     }
}
 
class ChildClass extends BaseClass{
     public function moreTest(){
          echo "moretest";
     }
}
// 产生 Fatal error: Cannot override final method BaseClass::moretest()

 
2.__toString(建议用PHP5.2或者更高版本)

 代码如下 复制代码
class Person{
     protected $name;
     protected $email;
    
     public function setName($name){
          $this->name = $name;
     }
 
     public function setEmail($email){
          $this->email = $email;
     }
 
     public function __toString(){
          return "$this->name <$this->email>";
     }
}
$rasums = new Person;
$rasums->setName('test');
$rasums->setEmail('test@qq.com');
print $rasums;


 
3.接口和抽象类

接口的作用:你想要保证一个类按照特定的名称、可见性和原型实现一个或多个方法。
接口的要求:
     类中全部为抽象方法
     抽象方法钱不用加abstract
     接口抽象方法属性为public
     成员属性必须为常量
例:

 代码如下 复制代码
interface ChildTest{
     public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
     public function childTest(){
          echo 1;
     }
     …………
}

 
抽象的作用: 其实抽象类和接口类有一部分很像,记得在哪里看见这样一句话,抽象类就把类像的部分抽出来,这句看上去很搞笑,其实它说出了抽象类的真理,抽象类的作用 是,当你发现你的很多类里面用很多方法你不断的在重复写,那你就可以考虑使用抽象类了,你可能会说“我不是可以重写一个类每个公共类我个实例化一个这个公 共类,调用相同的方法就可以了”,这里是可以,实际上抽象类做的工作也就是这个,不过他省去了你实例化的这个步骤,让你就像直接调用本类方法一样方便,而 且你还可以重载这个方法。
抽象的要求:
     类中至少有一个抽象方法
     抽象方法钱必须加abstract
例:

 代码如下 复制代码
abstract class Database{
     abstract public function connect();
     abstract public function query();
     abstract public function fetch();
     abstract public function close();
}

注:抽象方法不能定义为私有方法、不能定义为最终方法,因为它们需要被继承。
 
4.传递对象引用
php4:所有“=”都是创建一个副本
php5:除了对象外,其他“=”进行赋值时,都是创建一个副本;而对象则是引用
 
5.克隆对象
一、
聚合类:
__call方法简介:
当客户端代码用类中未定义的方法时,__call会被调用。
__call()接受两个参数,一个是方法名称,另一个是传递给要调用方法的所有参数(包括数组)
__call()方法返回的任何值都会返回给客户,将好像调用方式真实存在一样
例:

 代码如下 复制代码

class Address{
     protected $city;
     protected $country;

     public function setCity($city){$this->city = $city;}
     public function getCity(){return $this->city;}
     public function setCountry($country){$this->country = $country;}
     public function getCountry(){return $this->country;}
}

class Person{
     protected $name;
     protected $address;
     //浅克隆
     public function __construct(){
          $this->address = new Address;
     }

     public function setName($name){
          $this->name = $name;
     }
     public function getName(){
          return $this->name;
     }

     public function __call($method,$arguments){
          if(method_exists($this->address,$method)){
               return call_user_func_array(array($this->address,$method),$arguments);
          }
     }
     //深克隆
     public function __clone(){
          $this->address = clone $this->address;
     }
}

$test1 = new Person;
$test2 = clone $test1;

$test1->setName('testname1');
$test1->setCity('testcity1');
$test2->setName('testname2');
$test2->setCity('testcity2');

echo $test1->getName().'-'.$test1->getCity()."n";
echo $test2->getName().'-'.$test2->getCity()."n";
//testname1-testcity2
//testname2-testcity2


 
6.重要属性访问(__set __get __isset __unset) __isset __unset5.1之后才有用
作用:拦截对属性的需求,为了提高分离的程度,还要实现__isset()和__unset(),以便当我们用isset来检测属性或者unset()来删除属性,来保证类的行为正确
例:

 代码如下 复制代码

class Person{
     protected $__data = array('email','test');

     public function __get($property){
          if(isset($this->__data[$property])){
               return $this->__data[$property];
          }else{
               return false;
          }
     }

     public function __set($property,$value){
          if(isset($this->__data[$property])){
               return $this->__data[$property] = $value;
          }else{
               return false;
          }
     }
 
     public function __isset($property){
          if(isset($this->__data[$property])){
               return true;
          }else{
               return false;
          }
     }
 
     public function __unset($property){
          if(isset($this->__data[$property])){
               return unset($this->__data[$property]);
          }else{
               return false;
          }
     }
}

$test = new Person;
$test->email= 'test';
var_dump($test->email);

注意:

这两个方法只会捕捉缺少的属性,如果你为你的类定义了一个属性,那么当访问这个属性时php不会调用__get()和__set();
     这两个方法完全破坏了任何属性继承的想法。如果父对象中有个 __get()方法,而你在子类中又实现了自己的__get()方法,那么你的对象不会正确的执行,因为父类的__get()方法永远不会被调用,当然可以用parent::__get()解决

缺点:

速度相对较慢
使用魔术访问器方法就不可能在使用反射类,如phpdocumentor这类的工具将代码自动文档化
不能将其用于静态属性

标签:[!--infotagslink--]

您可能感兴趣的文章: