How to get the current week, current month results data using PHP.
This is the my table data:
Uname Type subscribed_on
Xxx Free 1355835358
Yyy paid 1355555358
Zzz premium 1423835358
I am storing the data into the db is using time()
.
Now how to get the uname from current week & month in PHP?
As of PHP 5.3 you can use DateTime class for datetime data. 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
?>
Use function date: http://es2.php.net/manual/en/function.date.php
For example:
<?php
$current_week = date('W', $your_timestamp);
$current_month = date('m', $your_timestamp);
像下面这样的查询应该可以完成工作:
SELECT uname FROM $tablename WHERE subscribed_on = $current_week_month_timestamp;
Like this for current month:
$start= mktime (0,0,0, date("n"), 1, date("Y"));
$end= mktime (0,0,0, date("n")+1, 1, date("Y"))-1;
and in sql:
SELECT uname FROM $tablename WHERE subscribed_on BETWEEN $start AND $end;
For current week it is a bit more tricky, use date("N") and strftime, maybe like this:
$start= strftime("-".date("N")." day");
$end= strftime("+7 day", $start)-1;
for data from current month you can use following:
// 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;
I assume that you want username of current week.
Moreover, i have started a week from Sunday. You can use different if you want.
First, get all the rows from database
$handle = mysql_query("select * from tablename");
while($row = mysql_fetch_array($handle)) {
if(date("W", $row['subscribed_on']) == date("W")) {
$newResult[] = $row;
}
}
$newResult
will have the records that you want.
You can do this:
$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);
}
//....
}
Your getWeek
Function
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;
}
Now you have got your list..
foreach($by_current_month as $records){
// Your Specified Month's Records
}
foreach($by_current_date as $records){
// Your Specified date's Records
}
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.