Reputation: 41
i want to get each month last date for every year. year will change and it should take from currdate.
i tried below one
$day = date( 't-m-Y' );
But am writing different if condition for each month. So in this code i want to give constant month. year will change. Is it possible to do like that?
can anyone help me to do this.
Upvotes: 1
Views: 248
Reputation: 3832
The last date of the month stays the same year in and year out, i.e. December will have 31 days whether referencing this year or the next. The only thing that changes is on what day of the week the last day of a month falls. So, the following indicates how to obtain all this data for the current month and the remaining months of the year as well as for next year.
<?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";
}
Live code
Note, the DateTime object gets initialized with a Unix timestamp string but one could also just simply provide a parameter of "now". Also, the try-catch is a good idea just in case an issue should arise in terms of instantiating the DateTime object.
Upvotes: 0
Reputation: 5690
you can check this code
$a_date = "2017-04-17";
echo date("Y-m-t", strtotime($a_date));
or use
$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'));
this is your code
echo $last_date = date('t-03-Y');
and this is output
30-03-2017
Upvotes: 0
Reputation: 12085
t
returns the number of days in the month of a given date
so you can pass the dynamic year and constant month like this to know the last day of month
<?php
$year ="2017";
echo date("Y-m-t", strtotime(date("$year-04-d")));
?>
OUTPUT:
2017-04-30
update 1:
$date = new DateTime('last day of 2017-04');
echo $date->format('Y-m-d');
Upvotes: 0
Reputation: 303
// 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));
Upvotes: 0
Reputation: 28529
Check demo here.
<?php
$year = date("Y");
echo $year . "\n";
for ($i = 1; $i <= 12; $i++)
{
$month = strtotime("$year-$i");
echo date("t", $month) . "\n";
}
output:
2017
31
28
31
30
31
30
31
31
30
31
30
31
Upvotes: 1