在php中比较两次 [英] compare two times in php

查看:32
本文介绍了在php中比较两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 PHP 中比较两个时间

我是 php 时间函数的新手,我有,

Hi I am new in time functions in php, i have,

@$download_time = date('d-m-Y H:i:s', $dec_time);
$current_time = date('d-m-Y H:i:s');    
$diff = abs(strtotime($current_time) - strtotime($download_time));    
$time=65 // $time is in seconds

现在我想比较 if ($diff < $time) 如何与这两个值进行比较.

Now I want to compare if ($diff < $time) how can I compare with this two values.

推荐答案

如果 $dec_timeunix 时间戳,那么只需:

If $dec_time is a unix timestamp, then simply:

if (time() - $dec_time < $time) {/* ... */}

否则如果是文本表示:

if (time() - strtotime($dec_time) < $time) {/* ... */}

如果以后不需要,请注意使用不必要的变量,因为如果注意这一点,您可以节省大量内存.如果服务器的 PHP 版本高于 5.2,那么您可以使用 DateTime 类为您提供许多扩展功能.

Be aware of using needless variables if it's doesn't needed later, because you can save a lot of memory if keep an eye of this. If the server's PHP version is above 5.2, then you can use the DateTime class wich gives you a lot of extended functionality.

我不明白为什么是 abs() 因为下载时间应该在过去.

I didn't understood why is the abs() because the download time should be at the past.

注意:@ 远离你的代码,因为它会为 PHP 解释器花费大量额外的计算,也许输入时间更长,但测试变量之前是否存在,例如:

Notice: Keep the @'s away from your code, because it costs a lot of additional calculations for the PHP interpreter, maybe it is longer to type, but test if the variable exists before, like:

if (!empty($dec_time) && time() - strtotime($dec_time) < $time) {/* ... */}

这篇关于在php中比较两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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