如何在PHP中获取当前周,当月结果数据?

时间:2012-12-21 09:25:06

标签: php mysql phpmyadmin

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

这是我的表数据:

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

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

现在如何从本周获得uname& PHP的月份?

7 个答案:

答案 0 :(得分: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
?>

答案 1 :(得分:2)

使用功能日期:http://es2.php.net/manual/en/function.date.php

例如:

<?php
$current_week = date('W', $your_timestamp);
$current_month = date('m', $your_timestamp);

答案 2 :(得分:1)

如下所示的查询应该可以完成工作:

SELECT uname FROM $tablename WHERE subscribed_on = $current_week_month_timestamp;

答案 3 :(得分: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;

本周它有点棘手,使用日期(“N”)和strftime,可能是这样的:

$start=  strftime("-".date("N")." day");
$end= strftime("+7 day", $start)-1;

答案 4 :(得分: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;

答案 5 :(得分:1)

我假设你想要当前周的用户名。
而且,我从周日开始一周。如果需要,您可以使用不同的颜色 首先,从数据库中获取所有行

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

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

$newResult将包含您想要的记录。

答案 6 :(得分: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
  }