PDA

View Full Version : Operating with php date


Xallow
2006-12-01, 12:49 PM
Hi guys and gals

I am trying to do the following:

extract a date from my database, it is MySql and stored as a Date, ie. 2006-11-29

compare this date to todays date using date("Ymd") and find out if the date from the datbase is more or less than 7 days ago, I just cant get it to work!

I won't copy my code here, since it is so messed up that nobody would understand it anyway! a working example would be Highly appreciated and almost worth giving a blowjob for.... almost :D

cd34
2006-12-01, 01:12 PM
select if('2006-11-01'>date_sub(curdate(),interval 7 day),'within the last week','more than a week old');

something like that will work.

substitute your field name in the place where I put the date

oast
2006-12-01, 02:20 PM
I use this basic code to do something similar to what you are looking for.

<?php
$OneWeekAgo = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-7, date("Y")));
$query = DatabaseQuery("SELECT * FROM table");
while($rows = mysql_fetch_array($query)){
if ($rows[lastaction] < $OneWeekAgo)
echo $rows[name].' last action was more than one week ago ('.$rows[lastaction].')<br>'."\n";
else
echo $rows[name].' was active <b>within the last week</b> ('.$rows[lastaction].')<br>'."\n";
}
?>

Change the table and field names to suit your needs.

** DatabaseQuery(); is a self made database access function, so you will have to alter the query to suit your needs, but the rest is straightforward.

Xallow
2006-12-02, 09:58 AM
thanks guys, it works now