Ⅰ 如何用php读取date的日期与当前日期进行比较
解决方案»
PHP时间比较用UNIX时间戳最方便了呀
你取出的时间是什么格式?
你先用个函数转化时间格统一UNIX时间戳就能简单的比较大小了
$monDay ='2004/01/02';
$todate ='2004/01/01';
if(strtotime($monDay)>strtotime($todate)){
echo('结束了');
}建议楼主存储unix时间戳,到时候也不用mktime,直接比较时间戳的大小即可!<?
$fromdete="2009-04-20";
$a=explode('-',$fromdete);$c=mktime(0,0,0,$a[1],$a[2],$a[0]);
$b=time();
if($b<$c)
{
echo'对不起,本次比赛活动投稿即将开始,敬请关注';
}
?>
Ⅱ php只有时分秒怎么比较大小
2015-04-22 这样的时间格式在php中是属于字符串的,字符串是不能比较大小的。
你用strtotime(时间) 这个函数将你的时间转化为时间戳,也就是一串数字这样就可以比较了
Ⅲ php中两个时间如何比大小
$time=time();
$min=strtotime(date('Y-m-d').'09:15:00');
$max=strtotime(date('Y-m-d').'15:55:00');
if($time>=$min&&$time<=$max){
//dosomething
}
Ⅳ 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程序设计有所帮助。
Ⅳ php时间戳的比较运算
将你的$time转换成日期表达的形式,因为现在还只是时间戳,而数据库中存的应该是时间,日期吧??
用 $time=date('Y-m-d',$time ); 格式化日期
Ⅵ PHP时间戳判断大小问题
造成这个原因的最大可能是你直接 date()取出来的是 UTC时间,不是北京时间(UTC+8)
解决办法是在你代码的源码顶部插入一行。
date_default_timezone_set("PRC");
详见我的日志
http://snmoney.blog.163.com/blog/static/44005820130282382206/
如果时间还是不准,还有一个可能是 服务器本身的时钟不准,你可以通过 date('H:i:s') 直接输出来判断。
另外..第一行为何要输出成 文本再转换回timestamp呢?感觉效率降低,可以直接替换成 time();
Ⅶ php 代码 怎么比较日期大小
设定两个要比较的日期变量a和b,把两个日期变量后面都添加上一个同样的时间,然后用函数strtotime分别转换为时间戳,再比较时间戳的大小。
下面演示,左侧是代码,右侧是运行结果:
1、设a为2019年5月20日,b为2019年05月21日,运行结果是b>a