如果声明有日期 [英] If Statement with date
本文介绍了如果声明有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做学校作业,如果在某天添加折扣,我会获得加分。我使用date函数(它是一个函数吗?)来积极地更新时间,但我不知道如何为此做if语句。
I am doing an assignment for school, and I get bonus marks if I add a discount on a certain day. I have an actively updating time, using the date function(is it a function?) but I don't know how I would go about making an if statement for this.
这是我的代码:
<?php
$t = time();
echo "Date of Purchase: " . (date("Y-m-d", $t));
if(date == "20140224") {
echo $b;
echo "It works!";
}
?>
很明显,我只提供了相关代码。
Obviously I only provided relevant code. Any help is appreciated!
推荐答案
使用 DateTime()
作为DateTime对象可比较:
Use DateTime()
as DateTime objects are comparable:
$today = new DateTime();
$discountDate = new DateTime('2014-02-24');
if ($today == $discountDate) {
// today is discount day!
}
提示:无需使用 time()
将第二个参数传递给 date()
。 date()
在未传递第二个参数时始终假定为现在
Tip: No need to use time()
to pass a second parameter to date()
. date()
always assumes "now" when no second parameter is passed
这篇关于如果声明有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文