1.<?php
2. $zero1=date(“y-m-d h:i:s”);
3. $zero2=”2010-11-29 21:07:00′;
4. echo “zero1的时间为:”.$zero1.”<br>”;
5. echo “zero2的时间为:”.$zero2.”<br>”;
6. if(strtotime($zero1)<strtotime($zero2)){
7. echo “zero1早于zero2′;
8. }else{
9. echo “zero2早于zero1′;
10. }
11. ?>

上面是比较两个绝对时间的大小

<?php
$zero1=strtotime (date("y-m-d h:i:s")); //当前时间 ,注意H 是24小时 h是12小时
$zero2=strtotime ("2014-1-21 00:00:00"); //过年时间,不能写2014-1-21 24:00:00 这样不对
$guonian=ceil(($zero2-$zero1)/86400); //60s*60min*24h
echo "离过年还有<strong>$guonian</strong>天!";
?>

上面是倒计时小程序 实例代码

<?php
//PHP计算两个时间差的方法
$startdate="2010-12-11 11:40:00";
$enddate="2012-12-12 11:45:09";
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
$hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
$minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
$second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
echo $date."天<br>";
echo $hour."小时<br>";
echo $minute."分钟<br>";
echo $second."秒<br>"; ?>

<?php
/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://phparch.cn (Professional PHP Architecture)
 */
function time2Units ($time)
{
   $year   = floor($time / 60 / 60 / 24 / 365);
   $time  -= $year * 60 * 60 * 24 * 365;
   $month  = floor($time / 60 / 60 / 24 / 30);
   $time  -= $month * 60 * 60 * 24 * 30;
   $week   = floor($time / 60 / 60 / 24 / 7);
   $time  -= $week * 60 * 60 * 24 * 7;
   $day    = floor($time / 60 / 60 / 24);
   $time  -= $day * 60 * 60 * 24;
   $hour   = floor($time / 60 / 60);
   $time  -= $hour * 60 * 60;
   $minute = floor($time / 60);
   $time  -= $minute * 60;
   $second = $time;
   $elapse = '';

$unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',
                    '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
                    );

foreach ( $unitArr as $cn => $u )
   {
       if ( $$u > 0 )
       {
           $elapse = $$u . $cn;
           break;
       }
   }

return $elapse;
}

$past = 2052345678; // Some timestamp in the past
$now  = time();     // Current timestamp
$diff = $now - $past;

echo '发表于' . time2Units($diff) . '前';
?>

<?php
echo "今天:",date('Y-m-d H:i:s'),"<br>";
echo "明天:",date('Y-m-d H:i:s',strtotime('+1 day'));
?>
上一行输出当前时间,下一行输出明天时间

这里+1 day
可以修改参数1为任何想需要的数  day也可以改成year(年),month(月),hour(小时),minute(分),second(秒)

date('Y-m-d H:i:s',strtotime("+1 day +1 hour +1 minute");
可以随便自由组合,以达到任意输出时间的目的
注:该方法之针对1970年以后试用,也就是时间戳的适用范围。

php 常用日期相函数[日期加减,两日期之差,日期转换时间截]

下面这些代码是一些常用的日期处理函数了,可以两个时间的日期加减,两日期之差,日期转换时间截等。

echo date('Y-m-d',strtotime('+1 d',strtotime('2009-07-08')));//日期天数相加函数

echo date("Y-m-d",'1246982400');
echo '<br>';
echo date("Y-m-d",'1279123200');
die();

$d   =   "2009-07-08 10:19:00";
echo   date("Y-m-d",strtotime("$d   +1   day"));   //日期天数相加函数

function dateToTime($d)//把日期转换成时间堆截
{
$year=((int)substr("$d",0,4));//取得年份

$month=((int)substr("$d",5,2));//取得月份

$day=((int)substr("$d",8,2));//取得几号

return mktime(0,0,0,$month,$day,$year);
}

/*

下面函数计算两日期之差

*/

$Date_1="2009-07-08";

echo $Date_1+1;

$Date_2="2009-06-08";
$Date_List_a1=explode("-",$Date_1);

$Date_List_a2=explode("-",$Date_2);

$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);

$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);

$Days=round(($d1-$d2)/3600/24);

echo "两日期之前相差有$Days 天";

shijan的更多相关文章

随机推荐

  1. 如何在search中动态的显示和隐藏tree中的字段

    在tree定义 invisible 来自context <field name="country_id" invisible="context.get('invis ...

  2. isKindOfClass:和isMemberOfClass:-b

    isKindOfClass: Returns a Boolean value that indicates whether the receiver is an instance of given c ...

  3. 一篇旧文章,结合汇编探索this指针

    //VC6.0下成功编译 #include <iostream.h> class X{ public: void foo(int b,int c){ this->a=b*c; cou ...

  4. 硬盘IO,SAS,SATA,和HD TUNE

    SAS的接口技术可以向下兼容SATA.具体来说,二者的兼容性主要体现在物理层和协议层的兼容. SAS系统的背板(Backplane)既可以连接具有双端口.高性能的SAS驱动器,也可以连接高容量.低成本 ...

  5. keil uVision4一些使用总结(汉字注释,C关键字等)

    近日心血来潮,下载了最新的版的keil,再加上protues ,想弄个虚拟环境.主要原因还是经济问题.电子元件,是要花钱的... 今天遇到些keil uVision 4使用方面的问题,记录下来,方便以 ...

  6. Delphi判断文件是否正在被使用(CreateFile也可以只是为了读取数据,而不是创建)

    首先,我们先来认识下CreateFile函数,它的原型如下   HANDLE CreateFile( LPCTSTR lpFileName,    //指向文件名的指针 DWORD dwDesired ...

  7. java学习面向对象之设计模式之单例模式

    就像上一节当中我们讲到的数组工具集一样,如果我们把他看作一个类,来应用,不阻止他new函数的话,这个类我们在整个过程当中我们只是用他来当一个工具.假如每次用都要new一下产生一个新对象的话,就会显得整 ...

  8. 构造函数语义学之Copy Constructor构建操作(2)

    二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...

  9. 3A. Shortest path of the king

    给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向.   #include <iostream> #include <cmath> #inclu ...

  10. HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

    Problem Description As the increase of population, the living space for people is becoming smaller a ...