php PHP检查当前日期是在设定日期之前还是之后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4149569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Checking if the current date is before or after a set date
提问by Chris
Im pulling a date from a database which is formatted like dd-mm-YYYY ... What i want to do is check the current date, if the current date is before the date coming from the database then it needs to print the database date, if its after then it needs to print 'go ahead'.
我从格式为 dd-mm-YYYY 的数据库中提取日期......我想要做的是检查当前日期,如果当前日期在来自数据库的日期之前,那么它需要打印数据库日期,如果它在之后,则需要打印“继续”。
Cheers.
干杯。
回答by Cfreak
if( strtotime($database_date) > strtotime('now') ) {
...
回答by kaiser
Here's a list of all possible checks for …
以下是所有可能的检查清单……
"Did a date pass?"
“约会过去了吗?”
Possible ways to obtain the value
获取值的可能方法
$date = strtotime( $date );
$date > date( "U" )
$date > mktime( 0, 0, 0 )
$date > strtotime( 'now' )
$date > time()
$date > abs( intval( $_SERVER['REQUEST_TIME'] ) )
Performance Test Result (PHP 5.4.7)
性能测试结果 (PHP 5.4.7)
I did some performance test on 1.000.000 iterations and calculated the average –?Ordered fastest to slowest.
我对 1.000.000 次迭代进行了一些性能测试,并计算了平均值——从最快到最慢排序。
+---------------------+---------------+
| method | time |
+---------------------+---------------+
| time() | 0.0000006732 |
| $_SERVER | 0.0000009131 |
| date("U") | 0.0000028951 |
| mktime(0,0,0) | 0.000003906 |
| strtotime("now") | 0.0000045032 |
| new DateTime("now") | 0.0000053365 |
+---------------------+---------------+
ProTip:You can easily remember what's fastest by simply looking at the length of the function. The longer, the slower the function is.
专业提示:只需查看函数的长度,您就可以轻松记住什么是最快的。时间越长,函数越慢。
Performance Test Setup
性能测试设置
The following loop was run for each of the above mentioned possibilities. I converted the values to non-scientific notation for easier readability.
针对上述每种可能性运行以下循环。为了更容易阅读,我将这些值转换为非科学符号。
$loops = 1000000;
$start = microtime( true );
for ( $i = 0; $i < $loops; $i++ )
date( "U" );
printf(
'| date("U") | %s |'."\n",
rtrim( sprintf( '%.10F', ( microtime( true ) - $start ) / $loops ), '0' )
);
Conclusion
结论
time()
still seems to be the fastest.
time()
似乎仍然是最快的。
回答by Scott Brown
I wanted to set a specific date so have used this to do stuff before 2nd December 2013
我想设置一个特定的日期,所以在 2013 年 12 月 2 日之前用它来做事
if(mktime(0,0,0,12,2,2013) > strtotime('now')) {
// do stuff
}
The 0,0,0
is midnight, the 12
is the month, the 2
is the day and the 2013
is the year.
该0,0,0
是午夜,12
是一个月,2
是白天和2013
是一年。
回答by AndreKR
if (strtotime($date) > mktime(0,0,0))
should do the job.
if (strtotime($date) > mktime(0,0,0))
应该做的工作。
回答by KGC
If you are looking for a generic way of doing this via MySQL, you could simply use a SELECT
statement, and add the WHERE
clause to it.
如果您正在寻找通过 MySQL 执行此操作的通用方法,您可以简单地使用一个SELECT
语句,并向其中添加WHERE
子句。
This will grab all fields for all rows, where the date stored in field "date" is before "now".
这将抓取所有行的所有字段,其中存储在字段“date”中的日期在“now”之前。
SELECT * FROM table WHERE UNIX_TIMESTAMP(`date`) < UNIX_TIMESTAMP()
Personally, I found this method to be more gentle on newbies in MySQL, since it uses the standard SELECT statement with WHERE to fetch the results. Obviously, you could grab only the fields relevant if you wanted to, by listing them instead of using a wildcard (*).
就个人而言,我发现这种方法对 MySQL 新手更温和,因为它使用标准的 SELECT 语句和 WHERE 来获取结果。显然,如果您愿意,您可以只获取相关的字段,方法是列出它们而不是使用通配符 (*)。
回答by quant
I have used this one and it served the purpose:
我已经使用了这个,它达到了目的:
if($date < date("Y-m-d") ) {
echo "Date is in the past";}
BR
BR
回答by robjmills
a MySQL-only solution would be something like this:
仅 MySQL 的解决方案将是这样的:
SELECT IF (UNIX_TIMESTAMP(`field`) > UNIX_TIMESTAMP(), `field`,'GO AHEAD') as `yourdate`
FROM `table`
回答by Drew
if(strtotime($db_date) > time()) {
echo $db_date;
} else {
echo 'go ahead';
}
回答by zerodin
回答by superfro
if(strtotime($row['database_date']) > strtotime('now')) echo $row['database_date'];
else echo date("d-m-Y");
No need to check the hours because if they are on the same day it will show the same date either way...
无需检查小时数,因为如果它们在同一天,无论哪种方式都会显示相同的日期......