日期加 30 天验证

发布于 2025-01-03 04:13:06 字数 935 浏览 1 评论 0原文

我正在尝试在 Yii 中编写验证规则,但无法将日期与工作进行比较。如果 EndStart 不超过 30 天,则希望呈现错误消息。

public function checkDate($attribute,$params)
    {
        $duration = strtotime(date('Y-m-d', strtotime($this->Start)) . ' +30 days');
        if ($this->End < $duration) 
                $this->addError('End','30 days or more!');
    }

这样,每个输入都会产生错误消息。我正在使用 MySql 数据库,开始和结束是日期格式

编辑: 鉴于下面答案中的输入,我已经尝试过

,现在甚至不会渲染页面。

    $startDate = $this->Start;
    $endDate = $this->End;
    $interval = $startDate->diff($endDate);
    if ($interval < 30) 
                $this->addError('End','30 days or more!');

给出了 100% 的错误。

$duration = date('Y-m-d', strtotime($this->Start . '+ 30 days'));
if ($this->End < $duration) 
    $this->addError('End','30 days or more!');

I am trying to write a validation rule in Yii but can't get the date compare to work. Looking to render the error message if End is not greater than 30 days past Start.

public function checkDate($attribute,$params)
    {
        $duration = strtotime(date('Y-m-d', strtotime($this->Start)) . ' +30 days');
        if ($this->End < $duration) 
                $this->addError('End','30 days or more!');
    }

With this every input yields the error message. I am using a MySql db and Start and End are date format

EDIT:
Given the input in the answers below I have tried

This will not even render the page now.

    $startDate = $this->Start;
    $endDate = $this->End;
    $interval = $startDate->diff($endDate);
    if ($interval < 30) 
                $this->addError('End','30 days or more!');

and

This gives the error 100% of the time.

$duration = date('Y-m-d', strtotime($this->Start . '+ 30 days'));
if ($this->End < $duration) 
    $this->addError('End','30 days or more!');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他是夢罘是命 2025-01-10 04:13:06

如果您要将 $duration$this->END(直接来自数据库)进行比较,则需要输入 date('Ymd', $持续时间),然后再检查 $this->END
您正在将 strtotime 与 date('Ymd') 进行比较,其中苹果和橙子发生

了变化
$duration = strtotime(date('Ymd', strtotime($this->Start)) . ' +30 天');

$duration = date('Ymd', strtotime($this->START . '+ 30 天'));

你的脚本应该可以工作

希望这有帮助

if you are compairing $duration to $this->END(which comes right out of the database) you need to put date('Y-m-d', $duration) before you check against $this->END
you're compairing a strtotime to a date('Y-m-d') where are apples and oranges

change
$duration = strtotime(date('Y-m-d', strtotime($this->Start)) . ' +30 days');
to
$duration = date('Y-m-d', strtotime($this->START . '+ 30 days'));

and your script should work

hope this helps

寂寞美少年 2025-01-10 04:13:06
#source: http://www.php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
#source: http://www.php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
笙痞 2025-01-10 04:13:06

您可能需要使用 DateTime->diff 并检查是否存在差异天数小于 30 则触发错误

You may want to use DateTime->diff and check if the difference of days is less 30 then trigger an error

慕巷 2025-01-10 04:13:06

我已经通过以下方式使用它:

public function rules()
{
    return array(
    ...
        array('dateX','timeoutSomething'),
    ...
    );
}


public function timeoutSomething($dateX){
    // If you try to modify the model after 300 seconds the error arise
    if((strtotime($this->dateX)+300) < time()){
        $this->addError($dateX,"Timeout exceed");
    }
}

I'm already using this in the following way:

public function rules()
{
    return array(
    ...
        array('dateX','timeoutSomething'),
    ...
    );
}


public function timeoutSomething($dateX){
    // If you try to modify the model after 300 seconds the error arise
    if((strtotime($this->dateX)+300) < time()){
        $this->addError($dateX,"Timeout exceed");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文