Leonidas Dosas
asked on
Presenting past 6 months bug
I have write this code that produce 6 past months <li>:
December 2016
December 2016
November 2016
November 2016
August 2016
July 2016.
I debug the file in my NetBeans editor and it shows me that the $previous_month and $previous_month_arithmetic variables for some reason not discount to the previous month (for this example to September and October). How i can fix this?
Thanks a lot for your effort.
<ul>
<?php
date_default_timezone_set('Europe/Athens');
//Δημιουργεί την λίστα με τους προηγούμενους 6 μήνες.
$current_month= date('M-y');
$months=array(1,2,3,4,5,6);
foreach ($months as $month_id) {
$previous_month=date('F-Y', strtotime("-$month_id months"));
$previous_month_arithmetic=date('m-Y', strtotime("-$month_id months"));
$mines_eng=array('January','February','March','April','May','June','July','August','September','October','November','December');
$mines_ell=array('Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάιος','Ιούνιος','Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος');
$previous_month= str_ireplace($mines_eng, $mines_ell, $previous_month);
echo "<li><a href='previous_posts.php?source=$previous_month_arithmetic&page=1'>$previous_month</a></li>";
}
?>
</ul>
In this code i create two arrays so to replace the months in my nation language (Greek). The problem is that the code works perfect until the present day goes to 31th day. Then the code produce 6 items list but the last 4 are duplicate. EgDecember 2016
December 2016
November 2016
November 2016
August 2016
July 2016.
I debug the file in my NetBeans editor and it shows me that the $previous_month and $previous_month_arithmetic
Thanks a lot for your effort.
ASKER
I cannot replicate the issue with your code.
Can you give exact steps to reproduce.
The following
Can you give exact steps to reproduce.
The following
<ul>
<?php
date_default_timezone_set('Europe/Athens');
//Δημιουργεί την λίστα με τους προηγούμενους 6 μήνες.
$current_month= date('M-y', strtotime('2017-01-31'));
echo $current_month;
$months=array(1,2,3,4,5,6);
foreach ($months as $month_id) {
$previous_month=date('F-Y', strtotime("-$month_id months"));
$previous_month_arithmetic=date('m-Y', strtotime("-$month_id months"));
$mines_eng=array('January','February','March','April','May','June','July','August','September','October','November','December');
$mines_ell=array('Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάιος','Ιούνιος','Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος');
// $previous_month= str_ireplace($mines_eng, $mines_ell, $previous_month);
echo "<li><a href='previous_posts.php?source=$previous_month_arithmetic&page=1'>$previous_month</a></li>";
}
?>
</ul>
GeneratesJan-17
January-2017
December-2016
November-2016
October-2016
September-2016
August-2016
ASKER
Julian. The code producing correct the past 6 months as you tested it at the days<>31th. But yesterday that we had 31 Jan it generated the following:
I had the similar problem a month ago (31 December). This bug is very weird.....
December-2016
December-2016
November-2016
November-2016
August-2016
July-2016
I had the similar problem a month ago (31 December). This bug is very weird.....
Line 5 of my code (replicated below) forces the date to yesterday
If you run my code exactly - do you get an error?
EDIT
As can be seen from the code above - all dates are fed input values through strtotime so the actual current date should have no bearing on the results.
$current_month= date('M-y', strtotime('2017-01-31'));
I still don't see the error.If you run my code exactly - do you get an error?
EDIT
As can be seen from the code above - all dates are fed input values through strtotime so the actual current date should have no bearing on the results.
The term "month" is ambiguous in date calculations. These articles explain the issue and give some ideas for dealing with months that might have as few as 28 days or as many as 31 days. Search the articles for Ambiguity of the term "Month"
Procedural
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
Object-oriented
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
This example seems to create sensible output.
https://iconoun.com/demo/temp_leonidas.php
Procedural
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
Object-oriented
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
This example seems to create sensible output.
https://iconoun.com/demo/temp_leonidas.php
<?php // demo/temp_leonidas.php
/**
* https://www.experts-exchange.com/questions/28999370/Presenting-past-6-months-bug.html
*
* https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
*/
error_reporting(E_ALL);
$zone = new DateTimeZone('Europe/Athens');
$month = new DateInterval('P1M');
$dto = new DateTime('Today', $zone);
$num = 6;
$months = [];
while ($num)
{
$months[$num] = $dto->format('Y-m');
$num --;
$dto->sub($month);
}
var_dump($months);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$previous_month=date('F-Y'
by;
$previous_month=date('F-Y'