Home > php教程 > php手册 > body text

用php计算两个日期相差多少

WBOY
Release: 2016-06-06 19:55:22
Original
1175 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 用php计算给定两个日期相差多少天: 计算方法不只下面介绍的这些,只是一些比较常规的方法: 如要计算2009-12-09和2009-12-05相差多少天: ?php $startdate=strtotime(“2009-12-09”); $enddate=strtoti

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

    用php计算给定两个日期相差多少天:

    计算方法不只下面介绍的这些,只是一些比较常规的方法:

    如要计算2009-12-09和2009-12-05相差多少天:

   

    $startdate=strtotime(“2009-12-09”);

    $enddate=strtotime(“2009-12-05”);

    上面的php时间日期函数strtotime已经把字符串日期变成了时间戳,这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下:

    $days=round(($enddate-$startdate)/3600/24) ;

    echo $days; //days为得到的天数;

    ?>

    下面介绍另外一种方法:

    上面判断的是两个日期的大小,下面则是判断生日的程序代码,得到的$n就是相距生日的天数.

    $birthday=“生日”;

    $birthday = preg_replace('/\d+/', Date('Y'), $birthday, 1);

    $d = 60*60*24;

    $n= floor((strtotime($birthday)-time())/$d);

    $n=$n+1;

    还有如果相比的是现在的时间,就可以用time()函数,得到的就是现在的时间戳.

    第二种情况呢,就是有数据库,这样就相对比较容易一些了!如果是MSSQL可以使用触发器!用专门计算日期差的函数datediff()计算便可!

    如果是MYSQL那就用两个日期字段的时间戳值,进行计算后便可得到相差的天数了.方法和上面的代码很像.

用php计算两个日期相差多少

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!