php实现比较两个字符串日期大小的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php实现比较两个字符串日期大小的方法。分享给大家供大家参考。具体如下:

<?php
function dateBDate($date1, $date2) {
// 日期1是否大于日期2
 $month1 = date("m", strtotime($date1));
 $month2 = date("m", strtotime($date2));
 $day1 = date("d", strtotime($date1));
 $day2 = date("d", strtotime($date2));
 $year1 = date("Y", strtotime($date1));
 $year2 = date("Y", strtotime($date2));
 $from = mktime(0, 0, 0, $month1, $day1, $year1);
 $to = mktime(0, 0, 0, $month2, $day2, $year2);
 if ($from > $to) {
 return true;
 } else {
 return false;
 } 
} 
?>
$date1 = "2009-10-13";
$date= mktime(0, 0, 0, date("m", strtotime($date1)), date("d", strtotime($date1)), date("Y", strtotime($date1)));

最终取得一个日期的 Unix 时间戳$date=1255392000。

很多时候做搜索的时候,搜索的时间不能大于当前日期,比较函数的写法大致和上面一个函数相同,具体如下:

function dateBCurrent($date){
//日期是否大于当前日期
 $currentDate=date("Y-m-d");
 //获取当前日期
 $cYear=date("Y",strtotime($currentDate));
 $cMonth=date("m",strtotime($currentDate));
 $cDay=date("d",strtotime($currentDate));
 $year=date("Y",strtotime($date));
 $month=date("m",strtotime($date));
 $day=date("d",strtotime($date));
 $currentUnix=mktime(0,0,0,$cMonth,$cDay,$cYear);
 //当前日期的 Unix 时间戳
 $dateUnix=mktime(0,0,0,$month,$day,$year);
 //待比较日期的 Unix 时间戳
 if($dateUnix<=$currentUnix){
 return true;
 }else{
 return false;
 }
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

Ubuntu中支持PHP5与PHP7双版本的简单实现

前言 最近在编写一个工具的时候,使用了PHP命名空间特性,在命名空间中如果想引用常量、函数,需要PHP5.6以上的版本,但我阿里云 ECS 上安装的版本是PHP 5.5.9,由于 ECS...

php mssql扩展SQL查询中文字段名解决方法

一、问题: 数据库是MS SQLServer2000,要把SQLServer2000里的一张表的数据导入MySQL5,其中SQLServer2000表的字段以简体中文命名(强烈建议不要以...

windows下zendframework项目环境搭建(通过命令行配置)

windows下zendframework项目环境搭建(通过命令行配置)

1、首先你要确定你的PHP版本不低于5.1.4,但强烈建议使用 5.2.3 或更高版本 2、确保你的php.ini开启了如下模块: extension=php_pdo.dllextens...

php定义参数数量可变的函数用法实例

本文实例讲述了php定义参数数量可变的函数用法。分享给大家供大家参考。具体分析如下: php中的的函数参数可以不固定,甚至不用定义参数,在函数内部使用func_get_args()函数获...

PHP用户管理中常用接口调用实例及解析(含源码)

PHP用户管理中常用接口调用实例及解析(含源码)

掌握用户的第一步就是将已经关注的粉丝信息保存起来,这个时候就用到获取用户列表接口。公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公...