嗨,我正在做一个项目,我的思想已经空白,所以我把它放在世界上帮助我。
我有一个像这样的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"]}';");
}
}我想要它做的是,随着日期的流逝,它改变了它的状态,所以当它在通知周中状态改变为通知,当它已经过了结束时,它过期了,当它没有超过这些日期时,它保持活动状态。
我知道我想怎么做,但我的头脑对这方面的方法感到一片空白。请帮我上网。
发布于 2013-09-19 18:18:07
试试这个:
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)
}发布于 2013-09-19 18:54:57
一开始:
关于mysql函数,您已经收到警告了。
为了从MySQL数据库中获取UNIX时间戳,您应该使用MySQL函数UNIX_TIMESTAMP(),例如:
SELECT UNIX_TIMESTAMP(`field_name`) FROM `table_name`;为了获得一天开始的时间戳,最好使用“strtotime(今天)”。
在回答您的问题时:在您的情况下,您不需要使用这么多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);");https://stackoverflow.com/questions/18901982
复制相似问题