php - ADD 30 day interval inside MySQL query -
i have working code adds order inside event calendar, wan't add expiration date comes order (30 days after order has send).
//include db configuration file include 'connect.php'; $currentdate = date("y-m-d h:i:s"); //insert event data database $insert = $conn->query("insert events (title,date,created,modified,tht) values ('".$title."','".$date."','".$currentdate."','".$currentdate."','".$date." + interval 30 day')"); if($insert){ echo 'ok'; }else{ echo 'err'; }
currently code working fine, shipping date same expiration date, instead of 30 days later want. missing here?
you can either in php:
//include db configuration file include 'connect.php'; $currentdate = date("y-m-d h:i:s"); $enddate = date("y-m-d h:i:s", strtotime('+30 days', strtotime($date))); //insert event data database $insert = $conn->query("insert events (title,date,created,modified,tht) values ('".$title."','".$date."','".$currentdate."','".$currentdate."','".$enddate."')"); if($insert){ echo 'ok'; }else{ echo 'err'; }
or in mysql directly:
//include db configuration file include 'connect.php'; $currentdate = date("y-m-d h:i:s"); //insert event data database $insert = $conn->query("insert events (title,date,created,modified,tht) values ('".$title."','".$date."','".$currentdate."','".$currentdate."',date_add('".$date."', interval 30 day))"); if($insert){ echo 'ok'; }else{ echo 'err'; }
i go first approach because @ least logic calculation of date in 1 place - in php. otherwise if have difference in timezones set on mysql , php servers may experience difference in datetimes returned php , mysql. better leave datetime logic in php.
Comments
Post a Comment