PHP如何比较日期和日期? [英] PHP How to compare date and date?

查看:118
本文介绍了PHP如何比较日期和日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自数据库的值:



'2009-1-1 00:00:00',好吧,让我粘贴我的代码:

  $ fetch = mysql_fetch_assoc($ result); 
$ db_value = $ fetch ['date']; //'2009-1-1 00:00:00'

$ today = date('Ymd H:i:s' ); //今天日期

如果我想比较这两个值,我该怎么办:

  if($ db_value< $ today){
// Do something
}



或方法2,转换为strtotime:

  if(strtotime($ db_value)< strtotime($ today)){
// Do somethinging
}

也许我的方法不正确,我应该用什么来比较2个日期?

解决方案

您可以



轻松比较日期的时间戳值。之前,您将第一个日期字符串



解析为年,月,日,小时,分钟,秒。然后使用mktime函数


I have this value from database:

'2009-1-1 00:00:00', okay, let me paste my code:

$fetch = mysql_fetch_assoc($result);
$db_value = $fetch['date'];//'2009-1-1 00:00:00'

$today = date('Y-m-d H:i:s'); // Todays date

If I want to compare the two values, what should I do:

if($db_value < $today){
    // Do something
}

or method 2, convert to strtotime:

if(strtotime($db_value) < strtotime($today)){
    // Do someting
}

Maybe my method is not correct, what should I use to compare 2 dates?

解决方案

Probably , you can get the epoch value of the date using mktime function , then you could

compare the date's times stamp values easily . before that you parse first date string

into year, month , day , hours, minutes , seconds. then use mktime function

这篇关于PHP如何比较日期和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆