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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

asp.net访问网络路径方法(模拟用户登录)

核心代码: public class IdentityScope : IDisposable { // obtains user token [DllImport("...

php不使用copy()函数复制文件的方法

本文实例讲述了php不使用copy()函数复制文件的方法。分享给大家供大家参考。具体如下: 下面的代码不使用php内置的copy函数,直接通过文件读取写入的操作方式复制文件 <...

PHP curl 并发最佳实践代码分享

本文将探讨两种具体的实现方法, 并对不同的方法做简单的性能对比. 1. 经典cURL并发机制及其存在的问题 经典的cURL实现机制在网上很容易找到, 比如参考PHP在线手册的如下实现方式...

详解PHP执行定时任务的实现思路

PHP本身是没有定时功能的,PHP也不能多线程。PHP的定时任务功能必须通过和其他工具结合才能实现,例如WordPress内置了wp-cron的功能,很厉害。 一、Linux服务器上使用...

解析PHP处理换行符的问题 \r\n

一首先说说 \r 与\n的区别回车”(Carriage Return)和“换行”(Line Feed)这两个概念的来历和区别。在计算机还没有出现之前,有一种叫做电传打字机(Teletyp...