Re: Expiry Date ($date function)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Ron Piggott" <ron.php@xxxxxxxxxxxxxxxxxx> wrote in message 
news:000701c51f0b$f5f67c00$c3b9acce@xxxxxx
>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 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux