Re: Expiry Date ($date function)

From: Date: Wed, 02 Mar 2005 23:19:33 +0000
Subject: Re: Expiry Date ($date function)
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-38744@lists.php.net to get a copy of this message
"Ron Piggott" <ron.php@actsministries.org> wrote in message news:000701c51f0b$f5f67c00$c3b9acce@ron... >I figured out that the syntax below creates the date in the way it may be > stored in a mySQL table: > > $todays_date=DATE('Y-m-d'); If you only need date in DB, then (assuming a DATE column in table) you may use CURDATE() in your sql INSERT statement: $sql = "INSERT cust_records (cust_name, cust_address, ... , purchase_date) " ."VALUES ($custName, $custAddr, ... , CURDATE())"; $mysql_query($sql); > > Is there any way to add 21 days to this as an expiry date? For example if > the date was March 20th 2005 21 days would be in April --- is there any > way > of dealing with this? Firstly, sticking with sql-only solution: $sql = "INSERT cust_records (cust_name, cust_address, ... , purchase_date, expiry_date) " ."VALUES ('$custName', '$custAddr', ... , CURDATE(), " ."DATE_ADD(CURDATE() + INTERVAL 21 DAYS))"; $mysql_query($sql); If these dates are needed in the php code, then it is certainly better to use the php fns to get them, rather than dig them out of DB with a subsequent query. So: $stamp = time(); //Format for DB insertion, but really could still use SQL methods in INSERT op. // If these were datetimes or times, one would use the same timestamp for all. $todayForDB = date('Y-m-d', $stamp); $expiryForDB = date('Y-m-d', strtotime("21 days", $stamp)); // Whatever format is wanted for php & output purposes. $todayForHTML = date('D, jS M, Y'); //eg: "Monday, 23rd March, 1982" $expiryForHTML = date('D, jS M, Y', strtotime("21 days", $stamp)); ... ... $sql = "INSERT cust_records (cust_name, cust_address, ... , purchase_date, expiry_date) " ."VALUES ('$custName', '$custAddr', ... , $todayForDB, $expiryForDB)"; $mysql_query($sql); > > Ron Hope this is helpful, Laurence Heywood

« previous php.db (#38744) next »