简体   繁体   中英

Adding Date to MySQL using PHP variable

I am building a e-commerce back end. I am attempting to add a "estimated date of delivery" to a HTML table.

All its doing is adding 14 days to the order date. How do I add 14 days to a php variable that's a time. Here's my code:

$order_date = mysql_result($order_results, $a, "order_date"); 

$convert_orderDate = strtotime($order_date);

$order_date = date('m-d-y', $convert_orderDate);

I want to change $order_date to $etd (estimated time of delivery) by simply adding 14 days to $order_date

How do I do this?

直接在MySQL中执行:

SELECT order_date + INTERVAL 14 DAY AS order_date

Take a look at the DateTime class and the modify() method

$etd = new DateTime($order_date);
$etd->modify('+14 Days');
echo $etd->format('Y-m-d');

To answer your question based on your code example, you can do it with strtotime . I've tested this code:

$order_date = '2012-08-14';

$convert_orderDate = strtotime($order_date);
$convert_etd       = strtotime($order_date . ' + 14 days');

$order_date = date('m-d-Y', $convert_orderDate);
$order_etd  = date('m-d-Y', $convert_etd);

echo $order_date; // 08-14-2012
echo $order_etd;  // 08-28-2012

Here is how I got it to work:

$a=0;
while ($a < $num_rows){ //while loop to go through all rows and print data

      $order_date = mysql_result($order_results, $a, "order_date");// pulls order date

      $convert_orderDate = strtotime($order_date); //converts order_date
      $convert_etd       = strtotime($order_date . ' + 14 days'); //converts etd by adding to order date

      $order_date = date('m-d-y', $convert_orderDate); // reformats order date
      $order_etd  = date('m-d-Y', $convert_etd); // reformats etd

echo"<td>$order_date</td>
     <td>$order_etd</td>";

Thanks guys this works perfectly! Thanks @JDavis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM