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. BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&hibernate

    1.在lib中加入必要的包,导入后结果如下: lib打包下载:SSH-lib.jar  (struts2.3.1.2  spring3.0.5 hibernate3.6.10.Final) 只包含必要 ...

  2. php编译安装configure完全配置够日常所用功能

    php编译安装configure完全配置够日常所用功能 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/p ...

  3. MFC 如何改变对话框按钮上的文字

    原文地址:http://blog.163.com/wenxianliang08@126/blog/static/8358326320110300643282/ 什么是标记菜单  如何标记菜单   如何 ...

  4. ctr预估模型

    http://wenku.baidu.com/course/view/1488bfd5b9f3f90f76c61b8d

  5. nutch,hbase,zookeeper兼容性问题

    nutch-2.1使用gora-0.2.1, gora-0.2.1使用hbase-0.90.4,hbase-0.90.4和hadoop-1.1.1不兼容,hbase-0.94.4和gora-0.2.1 ...

  6. mysql oracle 删除外键约束

    mysql alter table xxx drop foreign key xxx cascade; oracle alter table drop constraint xxxxx cascade ...

  7. Building a Space Station

    poj2031:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通, ...

  8. HYPER-V2008里安装WINDOWS 2012,以及监控宝

    呵呵,这两者有关系么?没关系.哈哈. 为了方便就放一起了. 至少,2008里的HYPERV能安装2012,倒是给我很多欣慰.

  9. DELL 720XD和R820玩赏

  10. json数值和结构

    JSON 值可以是: l  数字(整数或浮点数) l  字符串(在双引号中) l  逻辑值(true 或 false) l  数组(在方括号中) l  对象(在花括号中) l  null JSON建构 ...