美文网首页
php实现比较两个字符串日期大小的方法

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

作者: Yumazhiyao | 来源:发表于2016-12-13 09:05 被阅读738次

本文实例讲述了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;
 }
}

跟时间格式没关系 , 不过 数字的时间格式 其实是可以这样比较 , 只不过日期的格式要一样

echo strcmp("2016-09-27","2016-9-26"); // -1
echo strcmp("2016-11-27","2016-9-26"); // -1
echo strcmp("2016-09-27","2016-09-26");// 1
echo strcmp("2016-10-01","2016-09-26");// 1

相关文章

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

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

  • delphi DateUtils功能详解

    CompareDate 比较两个日期时间值日期部分的大小CompareDateTime 比较两个日期时间值的大小C...

  • 计算缓存大小

    计算缓存大小。 在字符串分类中暴露两个方法:1个是计算大小,1个是根据大小分类。 方法实现 - (NSIntege...

  • iOS字符串比较(区分与不区分大小写比较)

    1、比较字符串(搜索功能)(区分大小写)方法一: 2、比较字符串(搜索功能)(区分大小写)方法二: 3、比较字符串...

  • iOS比较两个日期的大小

    iOS比较两个日期的大小 //比较两个日期的大小 日期格式为2016-08-14 08:46:20 NSLog(@...

  • PHP strcmp()漏洞

    PHP strcmp() 函数 比较两个字符串(区分大小写) 语法 strcmp(string1,string2)...

  • 比较两个日期的大小

    //比较两个日期的大小 日期格式为2016-08-14 08:46:20 + (NSInteger)compare...

  • 比较两个日期的大小

    封装的方法如下: //比较两个时间字符串 public staticBooleancompareTime(Stri...

  • php常用方法

    php中处理字符串的相关方法 将日期时间字符串转化为整形(时间戳)strtotime() 函数将任何英文文本的日期...

  • php strnatcasecmp函数怎么用?

    strnatcasecmp()函数是PHP中的一个内置函数,它使用“自然顺序”算法比较比较两个字符串(不区分大小写...

网友评论

      本文标题:php实现比较两个字符串日期大小的方法

      本文链接:https://www.haomeiwen.com/subject/agmvmttx.html