显示当前或未来月份列表中的日期
Display dates from list that are of current or future months
使用 PHP,我希望遍历日期列表并仅打印适合当前或未来月份的日期。例如
2015 年 2 月
2015 年 3 月
2015 年 4 月
日期格式为“2015 年 1 月 15 日”。我假设 strtotime 是最好的方法?虽然不确定如何使用它。非常感谢任何方向。
$this_year = date('Y');
$this_month = date('m'); //month in numeric
$year_to_check = date('Y',strtotime($date_to_check));
$month_to_check = date('m',strtotime($date_to_check));
//check if the $this_year is the same or greater than $year_to_check before checking if the months are the same or different
if($year_to_check < $this_year){ //the year is past
//do not print
}else if($year == $year_to_check){ //it's either the same year or a future year
//check if the month is less
if($month_to_check < $this_month){
//the month is past
}else{
//the months are the same or $month_to_check is in the future
}
}else if($year_to_check > $this_year){
//the month is in the future because the $year_to_check is in the future
}
此函数会将所有日期转换为时间戳并比较它们以查看您的日期是否介于两者之间。
function checkMyDate($date){
//set TimeZone
date_default_timezone_set('GMT');
//create current timestamp
$d = new DateTime('now');
//create timestamp from first of the month
$d->modify('first day of this month');
$start = $d->getTimestamp();
//create timestamp from last day of next month
$d->modify('last day of next month');
$end = $d->getTimestamp();
//convert date to timestamp
$date = strtotime($date);
echo $start."-".$end."-".$date;
//check if $date is between $start and $end
if($date >= $start && $date <= $end){
return true;
}
return false;
}
使用 PHP,我希望遍历日期列表并仅打印适合当前或未来月份的日期。例如
2015 年 2 月
2015 年 3 月
2015 年 4 月
日期格式为“2015 年 1 月 15 日”。我假设 strtotime 是最好的方法?虽然不确定如何使用它。非常感谢任何方向。
$this_year = date('Y');
$this_month = date('m'); //month in numeric
$year_to_check = date('Y',strtotime($date_to_check));
$month_to_check = date('m',strtotime($date_to_check));
//check if the $this_year is the same or greater than $year_to_check before checking if the months are the same or different
if($year_to_check < $this_year){ //the year is past
//do not print
}else if($year == $year_to_check){ //it's either the same year or a future year
//check if the month is less
if($month_to_check < $this_month){
//the month is past
}else{
//the months are the same or $month_to_check is in the future
}
}else if($year_to_check > $this_year){
//the month is in the future because the $year_to_check is in the future
}
此函数会将所有日期转换为时间戳并比较它们以查看您的日期是否介于两者之间。
function checkMyDate($date){
//set TimeZone
date_default_timezone_set('GMT');
//create current timestamp
$d = new DateTime('now');
//create timestamp from first of the month
$d->modify('first day of this month');
$start = $d->getTimestamp();
//create timestamp from last day of next month
$d->modify('last day of next month');
$end = $d->getTimestamp();
//convert date to timestamp
$date = strtotime($date);
echo $start."-".$end."-".$date;
//check if $date is between $start and $end
if($date >= $start && $date <= $end){
return true;
}
return false;
}