我想重写一个mysql查询,该查询使用month()和year()函数显示特定月份的所有帖子,这些帖子以“ Ymd”参数格式显示在我的函数中,但是我不知道如何获取给定月份日期的最后一天。
$query_date = '2010-02-04';
list($y, $m, $d) = explode('-', $query_date);
$first_day = $y . '-' . $m . '-01';
Answers:
我知道这个问题的答案很好,但我想我会添加另一个解决方案。
$first = date("Y-m-d", strtotime("first day of this month"));
$last = date("Y-m-d", strtotime("last day of this month"));
如果您使用的是PHP 5.3+,请尝试此操作
$query_date = '2010-02-04';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');
要查找下个月的上一个日期,请进行以下修改,
$date->modify('last day of 1 month');
echo $date->format('Y-m-d');
等等..
cal_days_in_month()应该为您提供当月的总天数,因此也可以为您提供最后的天数。
仅打印当前月份的周:
function my_week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
$start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
if($currentWeek==1)
{$start_date = date('Y-m-01', strtotime($date));}
else if($currentWeek==5)
{$end_date = date('Y-m-t', strtotime($date));}
else
{}
return array($start_date, $end_date );
}
$date_range=list($start_date, $end_date) = my_week_range($new_fdate);
## Get Current Month's First Date And Last Date
echo "Today Date: ". $query_date = date('d-m-Y');
echo "<br> First day of the month: ". date('01-m-Y', strtotime($query_date));
echo "<br> Last day of the month: ". date('t-m-Y', strtotime($query_date));