如何比较上周和上个月的日期函数

时间:2014-07-17 06:32:54

标签: php mysql

$start = strtotime('this week');
$results =$wpdb->get_results("SELECT count( doctor_name ) AS totalleads FROM  `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date >='". $start."'");

这是我的代码,以获取上周的医生姓名和本周日期的数量(这意味着今天从上周开始) 它不工作??

和上个月的功能一样吗?

在我的数据库中,我使用此leads_date字段作为时间戳

2 个答案:

答案 0 :(得分:1)

您可以使用mysql的date_sub函数

获取上周的所有记录

SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 WEEK) and now()

获取上个月的所有记录

SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 MONTH) and now()

答案 1 :(得分:0)

$daynumber = date('N', date('d'));// getting today day number
$prevweek = $daynumber+7; // starting from prev week 
echo $prevdate = strtotime('-'.$prevweek.' days'); // prev week date
echo strtotime("-1 month"); // last month

更多: - Getting last month's date in php

day of the week to day number (Monday = 1, Tuesday = 2)