如何在php中获取每个月的最后一个日期,年份将根据cuurent日期而变化
我想得到每年每个月的最后一个日期。年份将发生变化,从currdate开始计算 我试过低于一如何在php中获取每个月的最后一个日期,年份将根据cuurent日期而变化,php,date,Php,Date,我想得到每年每个月的最后一个日期。年份将发生变化,从currdate开始计算 我试过低于一 $day = date( 't-m-Y' ); 但是我每个月都会写不同的if条件。所以在这段代码中,我想给出常量月。这一年将会改变。有可能这样做吗 有人能帮我吗。检查演示 <?php $year = date("Y"); echo $year . "\n"; for ($i = 1; $i <= 12; $i++) { $month = strtotime("$year-$i");
$day = date( 't-m-Y' );
但是我每个月都会写不同的if条件。所以在这段代码中,我想给出常量月。这一年将会改变。有可能这样做吗
有人能帮我吗。检查演示
<?php
$year = date("Y");
echo $year . "\n";
for ($i = 1; $i <= 12; $i++)
{
$month = strtotime("$year-$i");
echo date("t", $month) . "\n";
}
你可以检查这个代码
$a_date = "2017-04-17";
echo date("Y-m-t", strtotime($a_date));
或使用
$current_date = "2017-04-17";
$date = new DateTime($current_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
echo $first_date = date('Y-m-d',strtotime('first day of this month'));
echo $last_date = date('Y-m-d',strtotime('last day of this month'));
这是你的密码
echo $last_date = date('t-03-Y');
这就是输出
30-03-2017
t
返回给定日期当月的天数
所以你可以像这样通过动态年份和固定月份来知道月份的最后一天
<?php
$year ="2017";
echo date("Y-m-t", strtotime(date("$year-04-d")));
?>
更新1:
2017-04-30
$date = new DateTime('last day of 2017-04');
echo $date->format('Y-m-d');
月份的最后一天年复一年保持不变,即12月将有31天,无论是指今年还是下一年。唯一的变化是在一周的哪一天,一个月的最后一天。因此,下面说明了如何获取当前月份、今年剩余月份以及明年的所有这些数据
<?php
// preliminaries
try {
$date = new DateTime("@".time());
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
$strDate = $date->format("Y-n-t");
list($Y,$m,$t) = explode("-",$strDate);
$offset = 12 - $m;
$max = $offset + 1; // include current month
//this year starting with current month:
for ( $i=0; $i < $max; $i++ ){
$mo_day_one_yr = "$Y-" . ( $m + $i ) . "-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
if( $i == ( $max -1 )) echo "\n";
}
// next year:
for ( $i=1, $Y+=1, $max=12; $i <= $max; $i++ ){
$mo_day_one_yr = "$Y-$i-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
}
t返回给定日期当月的天数,请尝试此$date=“2009-11-23”;回音日期(“t-m-y”,标准时间($date));我们可以这样用吗。日期('t-03-Y')??因为在每个if条件下,我都知道月份,我只想得到今年和未来该月的最后日期。当然,您可以根据自己的目的编辑此代码。
// get Current Date
$date = date('j-m-Y');
echo date("j-m-y", strtotime($date));//Current Month date with Current year
echo "Current Year =".date("Y", strtotime($date))." , Current Month Days =".date("t", strtotime($date))." , Current Month Last Day Date =".date("t-m-Y", strtotime($date));
<?php
// preliminaries
try {
$date = new DateTime("@".time());
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
$strDate = $date->format("Y-n-t");
list($Y,$m,$t) = explode("-",$strDate);
$offset = 12 - $m;
$max = $offset + 1; // include current month
//this year starting with current month:
for ( $i=0; $i < $max; $i++ ){
$mo_day_one_yr = "$Y-" . ( $m + $i ) . "-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
if( $i == ( $max -1 )) echo "\n";
}
// next year:
for ( $i=1, $Y+=1, $max=12; $i <= $max; $i++ ){
$mo_day_one_yr = "$Y-$i-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
}