如果当前日期等于每月的最后一天,我想更新我的数据 休假变量将每月仅更新一次 +1,但似乎当我刷新主页时它会继续添加。
function get_leave() {
$current_date = date('d'); // current date
$lastDayOfMonth = date('t'); // last day of the month
$leave = 1;
$total_add_leave = 0;
$query = $this->db->query('SELECT total_leave,user_id FROM user');
foreach ($query->result_array() as $row) {
if($current_date == $lastDayOfMonth) { //if current day is equal to last day of the month ADD 1
$total_add_leave = $row['total_leave'] + $leave;
}
$this->db->set('total_leave',$total_add_leave);
$this->db->where('user_id', $row['user_id']);
$this->db->update('user');
}
}
您可以按如下方式执行此操作:
$current_date = date("y-m-d");
$last_day_of_the_month = date("y-m-t", strtotime($current_date));
if($current_date == $last_day_of_the_month) {
$total_add_leave = $row['total_leave'] + 1;
// database updating query must be placed in this condition
}
使用此功能,您将找到每月的最后一天"t为您提供每月的最后一天">
date("Y-m-t");
然后与当前日期比较
if(date("Y-m-d") == date("Y-m-t"))
{
// write your logic here
}