Given start date how to get all the months until current month?
For eg. If given date is 1st Jan 2019
output should be:
Jan 2019
Feb 2019
Mar 2019
Apr 2019
May 2019
Jun 2019
Jul 2019
Aug 2019
Sep 2019
Oct 2019
Nov 2019
Dec 2019
Jan 2020
Feb 2020
Mar 2020
Solved it with slightly different approach. Thank you all for hints and answers.
<?php
$startTime = strtotime("1 January 2019");
$startYear = date("Y", $startTime);
$currentYear = date("Y");
$yearDiff = $currentYear - $startYear;
$currentMonth = date("m", time());
for($i=0; $i < intval($currentMonth) + ($yearDiff * 12) ; $i++)
{
$t = strtotime("+". $i . " months", $startTime);
$monthName = date("M", $t) ." ". date('Y', $t);
echo $monthName . "<br/>\n";
}
?>
here you go , hope this work
$start = new DateTime('2019-01-01');
$end = new DateTime('2020-03-02');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("F Y") . "<br>\n";
}
I send a earlier date and it returns me the periods between the earlier date and today cut by tens of days.
Example with 14/01/2016
I need the result to be exactly as below:
11 Jan 2016 to 20 Jan 2016
21 Jan 2016 to 31 Jan 2016
01 Feb 2016 to 10 Feb 2016
11 Feb 2016 to 20 Feb 2016
21 Feb 2016 to 29 Feb 2016
01 Mar 2016 to 10 Mar 2016
11 Mar 2016 to 20 Mar 2016
21 Mar 2016 to 31 Mar 2016
01 Apr 2016 to 10 Apr 2016
11 Apr 2016 to 20 Apr 2016
Here my code:
function date_interval($startTime = false){
if( ! $startTime){
return array();
} else {
if(date('d',$startTime) < 10){
$actual = mktime(0, 0, 0, date('m',$startTime), 1, date('Y',$startTime));
} elseif(date('d',$startTime) < 20){
$actual = mktime(0, 0, 0, date('m',$startTime), 10, date('Y',$startTime));
} else {
$actual = mktime(0, 0, 0, date('m',$startTime), 20, date('Y',$startTime));
}
if(date('d',time()) < 10) {
$target = mktime(0, 0, 0, date('m',time()), 10, date('Y',time()));
} elseif(date('d',time()) < 20) {
$target = mktime(0, 0, 0, date('m',time()), 20, date('Y',time()));
} else {
$target = mktime(0, 0, 0, date('m',time())+1, 1, date('Y',time()));
}
$current = $actual;
$last = $actual;
while($current < $target) {
if(date('d',$current) < 10){
$current = mktime(0, 0, 0, date('m',$current), 10, date('Y',$current));
} elseif(date('d',$current) < 20){
$current = mktime(0, 0, 0, date('m',$current), 20, date('Y',$current));
} else {
$current = mktime(0, 0, 0, date('m',$current)+1, 1, date('Y',$current));
}
$dateTime[date("Y-m-d", $last) .'~'. date("Y-m-d", $current)] = date('d M Y',$last) . ' - ' . date('d M Y',$current));
$last = $current;
}
}
return $dateTime;
}
Datetime is the darling class from php that solves this type of problem nicely.
Here you feed the startdate to the function as DATETIME object and the function throws back a stack of list tags.
In the loop startDay is beeing modyfied first by adding 10 days to make the jump and then by one to avoid the overlap.
If you want a final line with the (less then ten days) time period from last startDay till today you can add something like:
$return .= '<li>From '.$startDay->format('d M Y').' to '.$today->format('d M Y').'</li>';
after the loop
function date_interval(DATETIME $startDay = NULL)
{
if( NULL == $startDay){
return array(); }
$return = '';
$today = new DATETIME('now');
while( $today->diff( $startDay )->format('%a%') > 10 )
{
$return .= '<li>From '.$startDay->format('d M Y').' to ';
$startDay->modify('+10 days');
$return .= $startDay->format('d M Y').'</li>
';
$startDay->modify('+1 day');
}
return $return;
}
I altered the above more general solution to do exactly what you describe:
<?php
function date_interval(DATETIME $startDay = NULL)
{
if( NULL == $startDay){
return array(); }
$return = '';
$today = new DATETIME('now');
$startMonth = new DateTime('first day of '.$startDay->format('M Y') );
$firstRun = TRUE;
while( $today > $startDay )
{
$return .= '<li>From '.$startDay->format('d M Y').' to ';
if( $firstRun == TRUE ){
if( (int)$startDay->format('d') > 10 )
$startMonth->modify('+10 days');
if( (int)$startDay->format('d') > 20 )
$startMonth->modify('+10 days');
$startDay = $startMonth;
$firstRun = FALSE;
}
$lastOfMonth = new DateTime('last day of '.$startDay->format('M Y') );
if( $lastOfMonth->diff( $startDay )->format('%a%') > 10 )
$startDay->modify('+9 days');
else
$startDay = $lastOfMonth;
$return .= $startDay->format('d M Y').'</li>
';
$startDay->modify('+1 day');
}
return $return;
}
echo date_interval( date_create_from_format('Y-m-d','2016-01-21') )."\n";
?>
See this function using DateTime:
function date_interval( $start = Null )
{
if( !$start ) return array();
$cur = new DateTime( sprintf( '%s-%02d', $start->format('Y-m'), [1,11,21,21][ floor(($start->format('j')-1)/10) ] ) );
$end = new DateTime();
$retval = [];
while( $cur <= $end )
{
$key = $cur->format( 'Y-m-d~' );
$val = $cur->format( 'd M Y - ' );
if( $cur->format( 'j' ) > 20 ) $cur->modify( 'last day of this month' );
else $cur->modify( '+ 9 days' );
$retval[ $key.$cur->format( 'Y-m-d' ) ] = $val.$cur->format( 'd M Y' );
$cur->modify( '+ 1 days' );
}
return $retval;
}
Calling it in this way:
$startDate = '14/01/2016';
$array = date_interval( DateTime::createFromFormat( 'd/m/Y', $startDate ) );
print_r( $array );
According with your keys/values schema, the resulting array is:
Array
(
[2016-01-11~2016-01-20] => 11 Jan 2016 - 20 Jan 2016
[2016-01-21~2016-01-31] => 21 Jan 2016 - 31 Jan 2016
[2016-02-01~2016-02-10] => 01 Feb 2016 - 10 Feb 2016
[2016-02-11~2016-02-20] => 11 Feb 2016 - 20 Feb 2016
[2016-02-21~2016-02-29] => 21 Feb 2016 - 29 Feb 2016
[2016-03-01~2016-03-10] => 01 Mar 2016 - 10 Mar 2016
[2016-03-11~2016-03-20] => 11 Mar 2016 - 20 Mar 2016
[2016-03-21~2016-03-31] => 21 Mar 2016 - 31 Mar 2016
[2016-04-01~2016-04-10] => 01 Apr 2016 - 10 Apr 2016
[2016-04-11~2016-04-20] => 11 Apr 2016 - 20 Apr 2016
[2016-04-21~2016-04-30] => 21 Apr 2016 - 30 Apr 2016
)
We operate only on one date ($cur), initially created from passed argument; we set the day of month using array [1,11,21,21] and selecting key by the rounded down division by 10 of original day (in your example, (14-1)/10 = 1.3, rounded as 1, that is 11 in [1,11,21,21] array). This is a quick way to resolve the 31th day exception.
Then we perform a loop until $cur date is greater than today date: we create starting part of key and value, we modify $cur adding it nine days or selecting last month's day, we create complete key and value of returned array. At the end of each loop, we increment $cur by 1 day.
I've this difficult problem. I've a unix timestamp, such as 1280102221, which corresponds to 25 Jul 2010. What I want to do is list all months/years since that day till now. In my example it would be:
Jul 2010
Aug 2010
Sep 2010
...
Oct 2014
Nov 2014
How do I do this?
Here is code I tried:
<?php
$timestamp = 1280102221;
$end = 1310000000;
for ($i = $timestamp; $i < $end; $i+= 24*30*3600) {
echo $i . "<br>";
}
?>
<?php
$timestamp = 1280102221;
$end = 1310000000;
$range = array();
while ($timestamp <= $end)
{
$range[] = date('M Y', $timestamp);
$timestamp = strtotime('+1 month', $timestamp);
}
print_r($range);
?>
I am trying to show month shorten name using date() inside for loop. But it doesn't want to return all of the month names, instead of just the first month name that is Jan.
for($d=1; $d<=12; $d++){
if($d < 10) {
$s = 0 . $d;
echo date("M", $s) . '<br/>';
}
}
It will return:
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
I don't know why the value of variable $d won't increment if it is located inside the date() function.
Can someone here explain me why it is happened like that?
Best Regards
for($d=1; $d<=12; $d++){
$myDate = mktime(1,1,1,$d,1,2014);
echo date("M", $myDate) . '<br/>';
}
This has been tested. Output:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
...
Basically, the second parameter of the date() function needs to be a timestamp (in seconds).
The mktime() function will create a timestamp based on the hours/minutes/seconds/months/days/years that you provide.
http://uk1.php.net/mktime
Date() requires timestamp as second parameter, not "month number" as you provide. So, for seconds 1-12 from Unix epoch, month will be January.
If you want to print the next 12 months from current date this will help you.
<?php
for ($m=1; $m<=12; $m++) {
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
echo $month. '<br>';
}
?>
What you are doing is simply looping current day 12 times and printing the same dates month in each iteration. Thats why you are getting the same month.
for ($m=1; $m<=12; $m++) {
$month = date('M', strtotime("2014-$m-01"));
echo $month. '<br>';
}
i am a newbie of php, supposed my blog created on 2011 year 1 month.and the article stored time as this 1305357473. now i want to use a function, which can output the before year and moth as this.
2011 year 1 month
2011 year 2 month
2011 year 3 month
2011 year 4 month
2011 year 5 month
......
i want to make the function can output the current and the past year and month.
this is my function and output the result. but i don't know how to finish.
function outYearMonth($year,$month){
$year=date(Y);
$past_month=strtotime(date(1)) ;
$month=strtotime(date(m));
$jg_month=round($month - $past_month);
$month=array();
for($i==0;$i<=$jg_month;$i++){
$month[]= date(1)+1;
}
}
This code:
$months = 10; //count of months
$date = date_create( 'now' );
echo date_format( $date, 'Y M' );
for ( $i = 0; $i < $months; $i++ ) {
date_sub( $date , date_interval_create_from_date_string( '1 months' ) );
echo date_format( $date, 'Y M' );
}
Will output:
2011 May
2011 Apr
2011 Mar
2011 Feb
2011 Jan
2010 Dec
2010 Nov
2010 Oct
2010 Sep
2010 Aug
2010 Jul
echo date('Y m');
will give you this year and month
echo date('Y m',strtotime('last month',time()))
will give you last month
for looping through all the month just get the current month and make a loop
$currentMonth = (int) date('m',time());
$currentYear = date('Y',time());
for($i=0; $i <= $currentMonth; $i++) {
echo "$currentYear $i";
}
will echo all months
2011 1
2011 2
2011 3
2011 4
2011 5