Re: Messing with Dates
| From: | Joao Prado Maia | Date: | Tue, 29 Aug 2000 17:56:16 +0000 |
| Subject: | Re: Messing with Dates | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-14196@lists.php.net to get a copy of this message | ||
On Tue, 29 Aug 2000, Phillip S. Baker wrote:
> Greetings All,
>
> In some records I am pulling from a MySQl database I have a field
> called last_updated. With the date that this field was last updated.
>
> Now after I pull all the records from the DB I want to use PHP find
> out if the last_updated field date is within the last month. If so
> then to display an updated next to the record (this I can do).
>
> having trouble figuring out how to do the date math to determine if
> the date in last_updated field is within the last month.
Hi Philipp,
I'm assuming that your last_updated field has the DATETIME datatype. You
have first of all to convert that to a UNIX timestamp, and then compare to
the substraction of the timestamp of "right now" and a month ago. I'm also
assuming that "a month ago" means 30 days. You might want to check for 31
day months if you are really paranoid.
<?php
$last = mysql_result(....);
// ok, this is ugly but I don't know of a better way
$converted = substr($last, 5, 2) . "/" . substr($last, 8,
2) . "/" . substr($last, 0, 4) . " " . substr($last, 11, 5);
$last_updated = strtotime($converted);
$right_now = time();
// 60 seconds * 60 minutes * 24 hours * 30 days
$last_month = $right_now - (60 * 60 * 24 * 30);
if ($last_updated < $last_month)
echo "It is within the last month";
else
echo "It is older than a month ago!";
?>
I didn't test this code in any way, so watch out for any weird bugs. I
hope it can help ya.
Regards,
Joao
--
Joao Prado Maia
Web Developer
Kefta.com, Inc