【问题标题】:php how to to do operation on datephp如何对日期进行操作
【发布时间】:2013-10-03 05:49:07
【问题描述】:

我想计算日期时遇到问题。简单示例:

我有 2013-09-01 是开始日期,我每个月有 30 天。我的工作需要在月底前 10 天通知我的用户(这意味着2013-09-20我必须提醒消息it's 10day more for end of this month)。所以每个人都有任何想法帮助计算它。因为我喜欢不能(+, -, *,/)约会。现在我是一些数据,例如

<?php
    date_default_timezone_set('Asia/Phnom_Penh');
    $current = time();
    $start = 1380188957;
    echo 'Start date: '. date('Y-m-d', $start) ."\n";
    echo '<br/>';
    $repeat = 30;
    $enddate = time() + ($repeat * 24 * 60 * 60);
    echo 'end date: '. date('Y-m-d', $enddate) ."\n";

感谢您的帮助。

【问题讨论】:

  • 对于我的概念:我想将$current 声明为sub$start,如果它是smaller or Equuleus(&lt;=) 20,我会提醒消息
  • 嘿时间。是否有任何答案解决了您的问题?如果可以,你能接受其中一个吗?

标签: php sql date datetime


【解决方案1】:

不是每个月都有 31 天,您可以通过使用 php 的 date() 函数中的字符串格式参数的 t 选项来获取任何一个月的天数。

// Current time as unix timestamp   
$now = time();

// Number of days in current month
$days_this_month = date("t", time());

// Last day of the current month as a unix timestamp;
$end_of_month = strtotime(date("Y-m-t", time()));

// Ten days before the end of the month as a unix timestamp
$ten_days = strtotime('-10 days', $end_of_month);

现在我们可以检查一下是否距离月底还有 10 天:

if($now > $ten_days) {

    // Do something
}

【讨论】:

    【解决方案2】:
    $start = 1380188957;
    $enddate = time() + ($repeat * 24 * 60 * 60);
    

    这是您自己的代码。使用它我们可以轻松计算10 days before end date

    $alert=$enddate-864000;   // That's 10 days
    $alertdate=date('Y-m-d', $alert);
    

    【讨论】:

    • 非常感谢,但我需要动态的,不想只知道静态的,你能告诉我如何计算 4400= 10day 吗?
    • 1 天 = 24 hours x 60 minutes x 60 seconds = 864000。现在如果您必须计算 6 天,只需将其乘以 6 即可得到答案。但有趣的是它已经成为您代码的一部分,看到$repeat 行了吗?做同样的事情。对不起,我打错了,错过了分钟。刚刚更新了
    • 是的,我有步骤可以使用10day, 6day and ... 进行测试,感谢您的帮助,我现在将对其进行测试
    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多