【问题标题】:Operations on date PHP [closed]PHP日期的操作[关闭]
【发布时间】:2013-03-10 15:50:09
【问题描述】:

我需要对日期进行一些操作。 从 DB 脚本读取操作的日期和频率(例如 21 天),并应计算自实际日期以来的下一个操作时间。我已经尝试以某种方式做到这一点(我很难用英文写它,所以你可以在下面找到代码)。

    //$unix_on - date from DB
    //$devices[$i]['freq'] - frequency of actions
    $unix_on=strtotime($devices[$i]['date_on']);
    $unix_today=strtotime(date('Y-m-d'));
    $actions=($unix_today-$unix_on)/(86400*$devices[$i]['freq']);
    $b=explode(".", $actions);
    $a='0'.'.'.$b['1'];
    $f=$a*$devices[$i]['freq'];
    $d=$unix_today+($f*86400);
    $e=date("Y-m-d",$d); 

但它不能正常工作 - 计算中有错误,我不知道为什么。

【问题讨论】:

  • 请使用搜索功能。 How to add time之前已经被问过无数次了

标签: php date days


【解决方案1】:

strtotime 比您使用它的功能强大得多。试试这个:

$e = date("Y-m-d",strtotime($devices[$i]['date_on']." +".$devices[$i]['freq']."days"));

【讨论】:

  • 这正是你在提名中受到批评的那种答案
【解决方案2】:
$dateOn = new DateTime($devices[$i]['date_on']);
$frecuency = new DateInterval('P' . $devices[$i]['freq'] . 'D'); // Period of x Days

$dateOn->add($frequency);

$e = $dateOn->format('Y-m-d');

http://es2.php.net/manual/en/datetime.construct.phphttp://es2.php.net/manual/en/datetime.add.phphttp://es2.php.net/manual/en/dateinterval.construct.php

【讨论】:

  • 非常感谢您的帮助。代码有效,但仅适用于第一个操作 - 它不显示自实际日期以来的下一个日期...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2017-05-12
  • 1970-01-01
相关资源
最近更新 更多