/    Sign up×
Community /Pin to ProfileBookmark

How to determine if a DATE is earlier then Now()? [PHP5]

Simply put, I have a field in a MySQL database called expdate (of type DATE) which is the Expiry Date of the record, and ever so often I want to kick off a PHP5 function .disableExpired() that will do the following:
1 – Get all records from Table
2 – Check if their expdate has past based on now()
3 – Set a status field in that case (disabled)

Now, I know how to do (1) and (3) but honestly I’ve got no clue how to do (2) which is comparing the date from my table (Date field in MySQL) and NOW() in PHP5 and how to do something conditional (if) based on that …

So far this is what I have

[Code]
$record = $this->db->Select(“SELECT id, expdate FROM table WHERE status = 1”);

foreach($record as $key=>$val)
{
$id = $val[id];
$expdate = $val[expdate];

// ??? DO THE MAGIC HERE ??? ///

if (past expiry date)
{
$this->setStatus($id, ‘0’);
}
}
[/Code]

As you can see … missing the key element …

Any help would be much appreciated…
Thanks,

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 01.2009 — Why don't you just do it all in a query?

<i>
</i>$sql = "update <span><code>table</code></span> set status = '0' where UNIX_TIMESTAMP(expdate) &gt; NOW()";
Copy linkTweet thisAlerts:
@NogDogApr 01.2009 — Why don't you just do it all in a query?

<i>
</i>$sql = "update <span><code>table</code></span> set status = '0' where UNIX_TIMESTAMP(expdate) &gt; NOW()";
[/QUOTE]


Since expdate is a DATE type column, there is no need for the UNIX_TIMESTAMP() function. Just directly compare the expdate column to NOW(). In fact, if you did use UNIX_TIMESTAMP(), then you would also need to express NOW() as an integer via UNIX_TIMESTAMP() (it returns the current timestamp integer when used with no argument). But that's all unnecessary when you can just do:
<i>
</i>. . . WHERE <span><code>expdate</code></span> &gt; NOW()
Copy linkTweet thisAlerts:
@NightShift58Apr 02.2009 — <i>
</i>$sql = "update <span><code>table</code></span> set <span><code>status</code></span> = '0' where status &lt;&gt; '0' AND <span><code>expdate</code></span> &lt; NOW()";
×

Success!

Help @Shaitan00 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...