duanfu7004 2013-03-05 19:31
浏览 103
已采纳

使用时间戳,检查即将到期

I am new to working with timestamps and I am trying to come up with a way to determine if the current date is less than 48 hours away than the date stored in a Database.

I was wondering if someone can help me accomplish this?

This is what I tried, I am not sure if this is the best solution:

//future date stored in DB
$timestamp = strtotime($regularFormatDate);

if($timestamp < strtotime("+2 days")){
    //do something
}

I appreciate any advice!

Many thanks in advance!

  • 写回答

2条回答 默认 最新

  • dpg98445 2013-03-05 19:59
    关注

    Your example does not look bad. Actually, I'm more concerned about how you intend to handle leap years/daylight savings.

    If using PHP 5.3 or above. Except in insist on using timestamp, I'd suggest (supports Daylight saving, etc):

    Procedural

          $datetime1 = date_create('now');
            $datetime2 = date_create('2013-03-07');
            $interval = date_diff($datetime1, $datetime2);
            $days = $interval->format('%R%a');
            if ((int) $days >= 2) {
                echo '2 or more days';
                echo '<br />';
                echo 'Difference: ' . $days . ' days';
            } else {
                echo 'Bearly ' . $days . ' day or less';
            }
    

    Object

            $date1 = new DateTime("2013-03-09");
            $date2 = new DateTime("now");
            $interval = $date1->diff($date2);
            $difference =  $interval->days;
            if($difference >= 2){
                echo $difference;
            }
    

    You could try using mysql directly:

    Read this: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

    mysql> SELECT INTERVAL 1 DAY + '2008-12-31';
        -> '2009-01-01'
    
    Mysql date_add() and datediff() are also good alternatives.
    

    PHP Datetime Diff: http://www.php.net/manual/en/datetime.diff.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿