Is there a way in PHP to get the current and the previous 5 months in the following format?
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
Have you tried following:
<?php
echo date('F, Y');
for ($i = 1; $i < 6; $i++) {
echo date(', F Y', strtotime("-$i month"));
}
Let me know, if this wont work.
Do not use:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime(-$i . 'month'));
}
// With date e.g.: "May, 31", outputs:
// May, 2018, May 2018, March 2018, March 2018, January 2018, December 2017
You can fix it by:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime('last day of ' . -$i . 'month'));
}
Or better use DateTime, e.g.:
$dateTime = new DateTime('first day of this month');
for ($i = 1; $i <= 6; $i++) {
echo $dateTime->format('F Y');
$dateTime->modify('-1 month');
}
Try this
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
}
Why not use DateTime Object as
$start = new DateTime('first day of this month - 6 months');
$end = new DateTime('last month');
$interval = new DateInterval('P1M'); // http://www.php.net/manual/en/class.dateinterval.php
$date_period = new DatePeriod($start, $interval, $end);
$months = array();
foreach($date_period as $dates) {
array_push($months, $dates->format('F').' '.$dates->format('Y'));
}
print_r($months);
Use strtotime and date
for( $i = 0; $i <= 5 ; $i++) {
print date("F Y", strtotime("-".$i." month"))."\n";
}
to achieve another formats for date look PHP date format HERE
<?php
for ($i =0; $i < 6; $i++) {
$months[] = date("F Y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months)
?>
Try This..
for ($i = 1; $i <= 6; $i++) {
$months[] = date("M-y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months);
#php
for($i=0; $i<=5; $i++) {
$last_six_months[] =date("Y-m-d", strtotime( date( 'Y-m-01' )." -$i months"));
}
$last_six_months = array_reverse($last_six_months); // If you want to reverse...
#endphp
next >>>
#foreach($last_six_months as $key => $row)
<span>{{date('F Y',strtotime($row))}}</span>
#endforeach
Related
I saw this question:
how to get the previous 3 months in php
My question is.. How do i output from a custom month.
I want to start from Mar 2018 (or any M Y user inputs) and it should output the next 3 (or any number user inputs) months.
Ex: Mar, Apr, May
The code below is from current month and year.
// current month: Aug 2018
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month");
echo $dte = date('M Y', $x);
echo '<br>';
}
And the output is
Aug 2018
Sep 2018
Oct 2018
You could use the DateTime class and increment using a DateInterval object:
// Assuming these are the user inputs
$month = 11;
$year = 2015;
// We create a new object with year and month format
$date = DateTime::createFromFormat('Y m', $year . ' ' . $month);
for ($i = 0; $i <= 2; $i++){
// Output the month and year
echo $date->format('m Y') . '<br>';
// Add 1 month to the date
$date->add(new DateInterval('P1M'));
}
Output:
11 2015
12 2015
01 2016
Documentation:
DateTime::createFromFormat
DateTime::format
DateTime::add
Change it to this as below it will give you as expected see the below code
// current month: Aug 2018
$effective_date = "MAR 2018";
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month",strtotime($effective_date));
echo $dte = date('M Y', $x);
echo '<br>';
}
This may be also helpful:
<?php
$month = 11;
$year = 2017;
$count = 15;
for ($i = 1; $i <= $count; $i++) {
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
$x = DateTime::createFromFormat('Y-m', $year.'-'.$month);
echo $x->format('m-Y');
echo '<br>';
}
?>
Output:
12-2017
01-2018
02-2018
03-2018
04-2018
05-2018
06-2018
07-2018
08-2018
09-2018
10-2018
11-2018
12-2018
01-2019
02-2019
Try this code for add nth Days, Months and Years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
You can generate the date with strtotime() function
Check this phpfiddle
<?php
$currMonth = 11;
$currYear = 2017;
$count = 15;
$currentYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $currentYear.'<br>';
for ($i = 1; $i < $count; $i++) {
$currMonth++;
if ($currMonth > 12) {
$currMonth = 1;
$currYear++;
}
$newMonthandYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $newMonthandYear.'<br>';
}
?>
<?php
$year = date('Y');
//start at march
$startMonth = 7;
//go forwards 2 months
$stepForwards = 22;
for ($i = $startMonth; $i <= $stepForwards; $i++){
if($i > 12 ) {
$month = $i % 12 == 0 ? 12 : $i % 12;
}
else {
$month = $i;
}
echo date("M Y", strtotime(date($year.'-'.$month.'-1'))) . "<br/>";
if($month == 12) {
$year++;
echo "<br/>";
}
}
I have this code :
for ($i = 2; $i > -3; $i--)
{
$class="";
if(date('D d-m')==date('D d-m', strtotime($i . ' days ago')))
{
$class=' class="distinct"';
}
echo '<li'.$class.'>'.date('D d-m', strtotime($i . ' days ago')) . '</li>'.'<br />';
}
And it outputs this:
Wed 17-05
Thu 18-05
Fri 19-05
Sat 20-05
Sun 21-05
I was wondering how i can skip the weekends, so it only shows the 5 days monday-friday. Any ideas how to exclude this?
You can start by creating a timestamp for monday and adding enough seconds to advance a day five times.
Like so:
$monday = strtotime('last monday');
for ($i = 0; $i < 5; $i++)
{
echo date('D d-m', $monday) . '<br />';
$monday = $monday + (60 * 60 * 24); // Add a day
}
I thing getDate() is just what you need. Where you can use 'wday' to check which day of the week it is (from 0 - Sunday, to 6 - Saturday). Example:
for ($i = 2; $i > -3; $i--)
{
$date = getDate(strtotime($i . ' days ago'));
if($date['wday'] == 6 || $date['wday'] == 0) {
echo "Weekend!\n";
}
else {
echo "Regular day...\n";
}
}
And it outputs:
Regular day...
Regular day...
Regular day...
Weekend!
Weekend!
Update:
for($i = -2; $i <= 2; $i++)
{
echo date ( 'D d-m' , strtotime ( "$i weekdays" ) ) . '<br>';
}
Will output:
Wed 17-05
Thu 18-05
Fri 19-05
Mon 22-05
Tue 23-05
===================================
Old answer:
$thisMonday = date("d-M-Y", strtotime('monday this week'));
for ($i = 0; $i <= 4; $i++)
{
$class="";
$date = date('d-M-Y', strtotime("+$i days", strtotime($thisMonday)));
if(date('D d-m') == date('D d-m', strtotime($i . ' days ago')))
{
$class=' class="distinct"';
}
echo '<li'.$class.'>'. date('D d-m', strtotime($date)) . '</li>'.'<br />';
}
How to Display Next 15 Days Without Saturday, Sunday in PHP. For Next 15 Days I am Using following Code. to Display Next 15 Days
for ($i = 1; $i <= 15; $i++) {
echo date('d/m/Y', strtotime('+'.$i.' day')) ."<br />";
}
You can use if statement
for ($i = 1; $i <= 15; $i++) {
$day = date('D', strtotime('+'.$i.' day'));
if($day != 'Sun' && $day != 'Sat'){
echo date('D d/m/Y', strtotime('+'.$i.' day')) ."<br />";
}
}
I have this code
<?php
$startYear = 2011;
while ($startYear <= date('Y')) {
echo "$startYear <br>";
for ($m=1; $m<=12; $m++) {
$theDate = $startYear . $m;
if ($theDate <= date('Yn')) {
$month = date('F', mktime(0,0,0,$m, 1, $startYear));
echo $month. '<br>';
}
}
$startYear++;
};
?>
because I would like to write a list of the type
2013
January
February
March
April
May
June
July
August
September
October
November
December
until the current year and the present month
but every list of months of every year just goes until september, why?
You have to change your if statement from this:
if ($theDate <= date('Yn')) {
to this:
if ($theDate <= date('Ym')) {
//^ See here the placeholder for month with leading zero
For more information about date() see the manual: http://php.net/manual/en/function.date.php
Because 201410 > 21049. This is what happens when you use date() for date math for which it is not suited.
DateTime() and its related functions are better suited for this:
$startYear = 2011;
$start = (new DateTime())->setDate($startYear,1,1);
$end = (new DateTime())->setDate($startYear,1,1)->modify('+1 year');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F') . "\n";
}
Demo
If you're using PHP 5.5+ it is a little cleaner
$startYear = 2011;
$start = (new DateTimeImmutable())->setDate($startYear,1,1);
$end = $start->modify('+1 year');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F') . "\n";
}
Demo
i edited my code to add a leading zero to the variable who will represent the month number, edited the current date from date('Yn') to date('Ym') and works.
<?php
$startYear = 2011;
while ($startYear <= date('Y')) {
echo "$startYear <br>";
for ($m=1; $m<=12; $m++) {
$f = sprintf('%02d', $m);
$theDate = $startYear . $f;
if ($theDate <= date('Ym')) {
$month = date('F', mktime(0,0,0,$f, 1, $startYear));
echo $month. '<br>';
}
}
$startYear++;
};
?>
Thanks for the answer guys.
I'm trying to create an array of the next 5 working week days (Monday - Friday, excluding today). I know the working week varies around the world but this is not important for what I am trying to do.
So, for example, if today is a Wednesday, I want the dates for Thursday and Friday of the current week and Monday, Tuesday and Wednesday of the following week.
I thought this would work:
$dates = array();
for ($i = 1; $ < 6; $i ++)
{
$dates[] = date('Y-m-d', strtotime('+ '.$i.' weekday'));
}
But for today, it is giving me:
Monday 1st
Tuesday 2nd
Wednesday 3rd
Thursday 4th
Sunday 7th!
Any advice appreciated.
Thanks
Try this
$dates = array();
$date = new DateTime();
while (count($dates)<5)
{
$date->add(new DateInterval('P1D'));
if ($date->format('N')<6)
$dates[]=$date->format('Y-m-d');
}
function getWeekdayDatesFrom($format, $start_date_epoch, $end_date_epoch, $range) {
$dates_arr = array();
if( ! $range) {
$range = round(abs($start_date_epoch-$end_date_epoch)/86400) + 1;
} else {
$range = $range + 1; //end date inclusive
}
$current_date_epoch = $start_date_epoch;
for($i = 1; $i <= $range; $i+1) {
$d = date('N', $current_date_epoch);
if($d <= 5) { // not sat or sun
$dates_arr[] = "'".date($format, $current_date_epoch)."'";
}
$next_day_epoch = strtotime('+'.$i.'day', $start_date_epoch);
$i++;
$current_date_epoch = $next_day_epoch;
}
return $dates_arr;
}
This should work:
$dates = array();
$i = 0;
while(true) {
$i++;
$time = strtotime("+$i days");
$dayOfWeek = date('w', $time);
/* 'w' - day of week, numeric, i.e. "0" (Sunday) to "6" (Saturday) */
if( ($dayOfWeek == 0) or ($dayOfWeek == 6) ) {
continue;
}
$dates[] = date('Y-m-d', $time);
if( count($dates) >= 5 ) {
break;
}
}
You can do like this:
$now = time();
$day = 60*60*24;
$days = array();
for ($i=1; $i<=7; $i++){
$ts = $now + $day*$i;
if (date("N", $ts) < 6){
$days[] = date("l jS", $ts);
}
}
The output for the above code is:
Array(
[0] => Monday 1st
[1] => Tuesday 2nd
[2] => Wednesday 3rd
[3] => Thursday 4th
[4] => Friday 5th
)