如何獲取當前周,當前月份使用PHP的結果數據。如何獲得當前的一週,當前月份的結果數據在PHP中?
這是我的表中的數據:
Uname Type subscribed_on
Xxx Free 1355835358
Yyy paid 1355555358
Zzz premium 1423835358
我將數據存儲到數據庫使用time()
。
現在如何從PHP本週的&月份獲得uname?
如何獲取當前周,當前月份使用PHP的結果數據。如何獲得當前的一週,當前月份的結果數據在PHP中?
這是我的表中的數據:
Uname Type subscribed_on
Xxx Free 1355835358
Yyy paid 1355555358
Zzz premium 1423835358
我將數據存儲到數據庫使用time()
。
現在如何從PHP本週的&月份獲得uname?
從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
?>
使用功能日期:http://es2.php.net/manual/en/function.date.php
例如:
<?php
$current_week = date('W', $your_timestamp);
$current_month = date('m', $your_timestamp);
像查詢下面的應該做的工作:
SELECT uname FROM $tablename WHERE subscribed_on = $current_week_month_timestamp;
像這樣當月:
$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;
從目前的一個月,你可以將數據使用以下內容:
// 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;
我假設你想本週的用戶名。
此外,我從星期天開始一週。如果你願意,你可以使用不同的。
首先,從數據庫
$handle = mysql_query("select * from tablename");
while($row = mysql_fetch_array($handle)) {
if(date("W", $row['subscribed_on']) == date("W")) {
$newResult[] = $row;
}
}
$newResult
獲得所有的行會有你想要的記錄。
@ vlcekmi3:謝謝你打磨我的答案。在所有應有的尊重,我想告訴你,我提供了答案的鏈接,以便narsimha可以看看不同的可能性,因爲他希望稍作修改。再次感謝。 –
你可以這樣做:
$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
}
[PHP時間戳(HTTP:// www.9lessons.info/2010/01/php-time-stamp-function.html) 這應該可以幫到你 – BLOB
我想查詢o f php mysql –
將鏈接的查詢結果傳遞給鏈接中的時間戳函數 – BLOB