简体   繁体   中英

how to get last weeks entry in mysql by using php

there is table in data base as P_order now i want fetch the last one weeks entry in that data base.... as i fetched the entry of the present date....but not able to fetch the last one week entry...

 <?php

 $wherecat = "`p_order_status` = 'active' AND `p_order_date` LIKE '".date('Y-m-d')."%' ";
 $catsql =$general->GetRows('*' ,'p_order' ,$wherecat);
 $catRes = mysql_num_rows($catsql);
 ?>
<p><?=$catRes;?></p>

<div class="box1">
    <span class="li_cloud"></span>
    <h3>Order Placed in Last Week</h3>
</div>
<?php

 $wherecat1 = "`p_order_status` = 'active' AND `p_order_date` LIKE '".date('Y-m-d',strtotime('-1 day')). "%' ";
 $catsql1 =$general->GetRows('*' ,'p_order' ,$wherecat1);
 $catRes1 = mysql_num_rows($catsql1);
 ?>

<p><?=$catRes1;?></p>

Try something like this :

SELECT * FROM p_order WHERE p_order_date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK);

or

SELECT * FROM p_order WHERE p_order_date > DATE_SUB(NOW(), INTERVAL 1 WEEK);

Try this code :-

$wherecat1 = "`p_order_status` = 'active' 
AND `p_order_date` >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY 
AND p_order_date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY ";
SELECT * FROM p_order WHERE date(p_order_date) > DATE_SUB(CURDATE(), INTERVAL 1 WEEK);

要么

$wherecat1 = "`p_order_status` = 'active' AND date(`p_order_date`) > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)";

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