1

如何使用 PHP 获取当周、当月结果数据。

这是我的表数据:

   Uname    Type        subscribed_on
    Xxx     Free        1355835358
    Yyy     paid        1355555358
    Zzz     premium     1423835358

我正在将数据存储到正在使用的数据库中time()

现在如何从 PHP 中的当前周和月获取 uname?

4

7 回答 7

4

从 PHP 5.3 开始,您可以将 DateTime 类用于日期时间数据。 http://php.net/manual/en/datetime.settimestamp.php

<?php
$date = new DateTime();

$date->setTimestamp($yourTimestamp);
echo $date->format('W') . "\n"; // week number of the year
echo $date->format('m') . "\n"; // month
?>
于 2012-12-21T09:44:08.813 回答
2

使用函数日期:http ://es2.php.net/manual/en/function.date.php

例如:

<?php
$current_week = date('W', $your_timestamp);
$current_month = date('m', $your_timestamp);
于 2012-12-21T09:28:31.387 回答
1

像下面这样的查询应该可以完成工作:

SELECT uname FROM $tablename WHERE subscribed_on = $current_week_month_timestamp;
于 2012-12-21T09:30:49.163 回答
1

像本月这样:

$start=  mktime (0,0,0, date("n"), 1, date("Y"));
$end= mktime (0,0,0, date("n")+1, 1, date("Y"))-1;

在 sql 中:

SELECT uname FROM $tablename WHERE subscribed_on BETWEEN $start AND $end;

对于本周它有点棘手,使用 date("N") 和 strftime,可能是这样的:

$start=  strftime("-".date("N")." day");
$end= strftime("+7 day", $start)-1;
于 2012-12-21T09:40:25.340 回答
1

对于当月的数据,您可以使用以下内容:

// month
$month = date('m', time());
$year = date('Y', time());

$curr_month = mktime(0, 0, 0, $month, 1, $year);
SELECT * FROM `table` WHERE `subscribed_on` > $curr_month;

// + week
$curr_day = date('d', time());
$start_day = date('N', time());
$day = $day-$start_day+1; // last monday day

$curr_week = mktime(0, 0, 0, $month, $day, $year);
SELECT * FROM `table` WHERE `subscribed_on` > $curr_week;
于 2012-12-21T09:43:31.217 回答
1

我假设您想要本周的用户名。
此外,我从周日开始一周。如果你愿意,你可以使用不同的。
首先,从数据库中获取所有行

$handle = mysql_query("select * from tablename");

while($row = mysql_fetch_array($handle)) {
    if(date("W", $row['subscribed_on']) == date("W")) {
        $newResult[] = $row;
    }
}

$newResult会有你想要的记录。

于 2012-12-21T09:45:45.060 回答
0

你可以这样做:

$sql="SELECT * FROM table_name ORDER BY subscribed_on DESC";
$res=mysql_query($sql);
$by_current_week=array();
$by_current_date=array();
$by_current_month=array();

define('CUR_MON','12');
define('CUR_DATE',21);
define('CUR_WEEk',3);

while($obj=mysql_fetch_obj($res)){

   $week_of_subscribed=getWeek($obj->subscribed_on);    
   $month_of_subscription=date('m',$obj->subscribed_on);
   $date_of_subscription=date('d',$obj->subscribed_on);

   if(CUR_MON==$month_of_subscription){
      array_push($by_current_month,$obj);
   }

   if(CUR_DATE==$date_of_subscription){
      array_push($by_current_date,$obj);
   }

  //....

 }

你的getWeek功能

        function getWeek($timestamp) {
            $week_year = date('W',$timestamp);
            $week = 0;//date('d',$timestamp)/7;
            $year = date('Y',$timestamp);
            $month = date('m',$timestamp);
            $day = date('d',$timestamp);
            $prev_month = date('m',$timestamp) -1;
            if($month != 1 ){
                $last_day_prev = $year."-".$prev_month."-1";
                $last_day_prev = date('t',strtotime($last_day_prev));
                $week_year_last_mon = date('W',strtotime($year."-".$prev_month."-".$last_day_prev));
                $week_year_first_this = date('W',strtotime($year."-".$month."-1"));
                if($week_year_first_this == $week_year_last_mon){
                    $week_diff = 0;
                }
                else{
                    $week_diff = 1;

                }
                if($week_year ==1 && $month == 12 ){
                // to handle December's last two days coming in first week of January 
                    $week_year = 53;
                }

                $week = $week_year-$week_year_last_mon + 1 +$week_diff;


            }

            else{
             // to handle first three days January coming in last week of December.
                $week_year_first_this = date('W',strtotime($year."-01-1"));
                if($week_year_first_this ==52 || $week_year_first_this ==53){
                    if($week_year == 52 || $week_year == 53){
                        $week =1;
                    }
                    else{
                        $week = $week_year + 1;
                    }
                }
                else{
                    $week = $week_year;
                }
            }
            return $week;
        }

现在你有你的清单..

  foreach($by_current_month as $records){
    // Your Specified Month's Records
  }


  foreach($by_current_date as $records){
    // Your Specified date's Records
  }
于 2012-12-21T09:57:11.637 回答