嗨,我
正在做一个项目,我的头脑一片空白,所以我把它放到世界上来帮助我。
我有一个这样的MySQL数据库项目的开始日期,通知日期是结束一周后,然后是其过期的结束日期
+table+
|id| start | notice | end |status|
|01|2013-09-01|2013-9-23|2013-10-01|Active|
....
.PHP
$query = mysql_query("SELECT *,
DATE_FORMAT(`start_date`,'%d-%m-%Y') as fmt_start_date,
DATE_FORMAT(`notice_date`,'%d-%m-%Y') as fmt_notice_date,
DATE_FORMAT(`end_date`,'%d-%m-%Y') as fmt_end_date
FROM `$table` ORDER BY `fmt_start_date` ASC ");
while ($row = mysql_fetch_array($query)) {
$start = strtotime($row["fmt_start_date"]);
$notice = strtotime($row["fmt_notice_date"]);
$end = strtotime($row["fmt_end_date"]);
$current = strtotime(date('d-m-Y'));
if( $notice >= $current && $end < $current){
mysql_query("UPDATE `$table` SET `status` = 'Notice' WHERE `id` ='{$row["id"]}';");
} elseif ( $end <= $current ){
mysql_query("UPDATE `$table` SET `status` = 'Expire' WHERE `id` ='{$row["id"]}';");
} else {
mysql_query("UPDATE `$table` SET `status` = 'Active' WHERE `id` ='{$row["id"]}';");
}
}
我希望它做的是随着日期的过去,它会改变它的状态,所以当它在通知周中时,状态会更改为通知,当它已经过了结束时,它就过期了,当它没有超过这些日期时,它会保持活动状态。
我知道我想要如何完成它,但我的头脑对这种方法一片空白。请帮我上网。
试试这个:
if ($current < $notice){
//has not reached notice (Active)
} elseif ($current < $end){
//has not reached end but has reached notice (Notice)
} else{
//has reached end (Expired)
}
起初:
关于 mysql 函数,你已经被警告过了。
要从MySQL数据库获取UNIX时间戳,您应该使用MySQL函数UNIX_TIMESTAMP(),例如:
SELECT UNIX_TIMESTAMP(`field_name`) FROM `table_name`;
要获取一天开始的时间戳,最好使用"strtotime('today')"。
回答您的问题:在您的情况下,您不需要使用那么多 PHP。足以发出两个 MySQL 请求:
$current = strtotime('today');
mysql_query("UPDATE `$table` SET `status` = 'Notice' WHERE `notice` < FROM_UNIXTIME($current);");
mysql_query("UPDATE `$table` SET `status` = 'Expire' WHERE `end` < FROM_UNIXTIME($current);");