0

我很乐意帮助那些不起作用的代码我想看看该项目是否在上个月发布。如果是,那么得到一个积极的结果。

    //$project_time="2012-08-01 13:43:49";
    $project_time="2012-10-02 14:05:09";
 $end=mktime(0,0,0,date("m",strtotime($project_time))+1,date("d",strtotime($project_time)),d    ate("y",strtotime($project_time)));
 $end=date("d.m.y",$end);
 $today=mktime(0,0,0,date("m"),date("d"),date("y"));
 $today=date("d.m.y",$today);    

echo 'Project date '.$date.'<br />';
echo 'End date '.$end.'<br />';
echo 'Today '.$today.'<br />';

if($today<$end){
  echo " open<br />";
}
else{
  echo " finish<br />";
}

PROJECT_TIME 首先给出了一个好的结果,而另一个则没有。$ 结束创建日期基于 $ PROJECT_TIME 加上一个月。变量数据 TOTDAY 获取今天的日期。以及我想从 PROJECTTIME 上个月得到答案的比较

如果有人理解并可以帮助我,我会很高兴。

4

3 回答 3

1

strtotime 是您要使用的功能。只需使用以下语法:

$end = date('d.m.y', strtotime('+1 month', strtotime($project_time));

编辑

人们所说的比较字符串是正确的。不要比较字符串,比较时间戳。

于 2012-10-03T11:56:17.073 回答
0

尝试将其重写为:

$project_time = "2012-10-02 14:05:09";
$project_endtimestamp = strtotime('+1 month', strtotime($project_time));

echo 'Project date ' . $date . '<br />';
echo 'End date ' . date('d.m.y', $project_endtimestamp) . '<br />';
echo 'Today ' . date('d.m.y') . '<br />';

if (time() < $project_endtimestamp) {
    echo " open<br />";
} else {
    echo " finish<br />";
}

编辑:没有完全理解这个问题。根据@Simon Germain 的回答+1 month在通话中添加。strtotime

于 2012-10-03T11:56:46.973 回答
0

Basic problem: You're comparing two "d.m.y" strings.

This will always fail, because PHP sees them as plain text, not as dates. Therefore, asking which one is bigger will generally give the wrong answer.

Also: Get rid of all that crazyness with the old-style date handling functions. PHP has much better ways to do that sort of thing these days.

$project_time="2012-10-02 14:05:09";

$projDate = DateTime::createFromFormat('Y-m-d H:i:s', $project_time);
$dateNow = new DateTime();
if($projDate < $dateNow) {
    ... do something here...
}
于 2012-10-03T12:00:09.170 回答