I need to determine if a date (month and day) is between two other month/days.
I've attached an image to this post that describes what I'm trying to do. Basically the example "current day" is highlighted in red, and it's between those nearest dates (Mar 15 and Nov 22). However, when I use Unix timestamps and artificially add a year to the dates, the script thinks that Nov 22 hasn't occurred yet, and therefore it suggests that Feb 1 is before Mar 15 AND Nov 22, instead of in between them. So, I need to compare dates without using a year.
Hopefully this makes sense. I'd just like to compare dates using months and days, but ignoring the year, and using a framework like the wheel I show in the image.
The problem in your example is that (in the same year) the upper bounding date is before the lower bound. In that case, Any date less than the upper bound (Jan 1 - Mar 14) or greater than the lower bound (Nov 23 - Dec 31) falls between the two.
<?php
$upperBound = new DateTime("Mar 15");
$lowerBound = new DateTime("Nov 22");
$checkDate = new DateTime("Feb 1");
if ($lowerBound < $upperBound) {
$between = $lowerBound < $checkDate && $checkDate < $upperBound;
} else {
$between = $checkDate < $upperBound || $checkDate > $lowerBound;
}
var_dump($between);
?>
Displays:
boolean true
Edit
If the date you want to check is "Feb 29" and the current year is not a leap year, then DateTime interprets it as "Mar 1".
To check if a date falls between two dates, inclusively, use:
if ($lowerBound < $upperBound) {
$between = $lowerBound <= $checkDate && $checkDate <= $upperBound;
} else {
$between = $checkDate <= $upperBound || $checkDate >= $lowerBound;
}
To solve this, you can convert your dates to 4-digit numbers of the form MMDD.
$start = '1122';
$end = '0315';
$date = '0201';
If you don't yet have your dates formatted like this, you can use date('md', $timestamp), or just calculate the number by taking $month * 100 + $day;
Then your test logic will change depending on whether you're crossing a year boundary or not. Putting it all together, it might look like this:
function isDateBetween($testM, $testD, $startM, $startD, $endM, $endD) {
$start = $startM*100 + $startD;
$end = $endM*100 + $endD;
$date = $testM*100 + $testD;
if ($start > $end) {
// crossing a year boundary
return ($date > $start) || ($date < $end);
} else {
// not crossing a year
return ($date > $start) && ($date < $end);
}
}
Here's some examples of calling this function:
// your example given above
isDateBetween(2, 1, 11, 22, 3, 15); // true
// ends the day after instead
isDateBetween(3, 16, 11, 22, 3, 15); // false
// reverse the dates in your example
isDateBetween(2, 1, 3, 15, 11, 22); // false
// ends the day after, with reversed dates
isDateBetween(3, 16, 3, 15, 11, 22); // true
Try like this.
<?php
$check_date= strtotime("31-Aug-1990");
$start_date= strtotime("10-Aug-2013");
$end_date= strtotime("30-Aug-1990");
if (date("M-d", $check_date)>date("M-d", $start_date) && date("M-d", $check_date)<date("M-d", $end_date))
echo "in the range";
else
echo "not in the range";
Working code is here
//must be 2 digit month, then 2 digit day, like mm-dd
$low = '03-15';
$high = '11-22';
$date = '02-01';
$isBetween = $date >= $low && $date <= $high;
var_dump($isBetween);
I'm making use of lexicographic ordering, which is how php compares strings. The key point is to put the largest units on the left(months), and make sure to left-pad each numeric segment to the same length, using zeros to pad.
If you don't yet have your dates as strings formatted like this, you can use date('m-d', $timestamp) function to achieve that.
Related
I have a calendar that I want to allow events to be repeated on a week day of the month. Some examples would be:
Repeat every 4th Tuesday of the month
Repeat every 2nd Friday of the month
And so on...
What I need is the ability to find out how many week days (for example Tuesday's) have passed this month so far.
I found some code that returns how many Monday's have passed.
$now=time() + 86400;
if (($dow = date('w', $now)) == 0) $dow = 7;
$begin = $now - (86400 * ($dow-1));
echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>";
This works well but how do I make it so that I can determine any week day? I cannot seem to get my head around the code to make this work.
strtotime is really useful for this kind of thing. Here are lists of the supported syntax. Using your example of repeat every 2nd Friday of the month I wrote the following simple snippet for you:
<?php
$noOfMonthsFromNow=12;
$dayCondition="Second Friday of";
$months = array();
$years = array();
$currentMonth = (int)date('m');
for($i = $currentMonth; $i < $currentMonth+$noOfMonthsFromNow; $i++) {
$months[] = date('F', mktime(0, 0, 0, $i, 1));
$years[] = date('Y', mktime(0, 0, 0, $i, 1));
}
for ($i=0;$i<count($months);$i++){
$d = date_create($dayCondition.' '.$months[$i].' '.$years[$i]);
if($d instanceof DateTime) echo $d->format('l F d Y H:i:s').'<br>';
}
?>
This can be tested at: http://www.phpfiddle.org/lite/
$beginningOfMonth = strtotime(date('Y-m-01')); // this will give you the timestamp of the beginning of the month
$numTuesdaysPassed = 0;
for ($i = 0; $i <= date('d'); $i ++) { // 'd' == current day of month might need to change to = from <= depending on your needs
if (date('w', $beginningOfMonth + 3600 * $i) == 2) $numTuesdaysPassed ++; // 3600 being seconds in a day, 2 being tuesday from the 'w' (sunday == 0)
}
Not sure if this will work, and there's probably a better way to do it; don't have the means to test it right now but hopefully this puts you on the right track! (I get tripped up on date math a bit too, especially with timezones)
I know about the unwanted behaviour of PHP's function
strtotime
For example, when adding a month (+1 month) to dates like: 31.01.2011 -> 03.03.2011
I know it's not officially a PHP bug, and that this solution has some arguments behind it, but at least for me, this behavior has caused a lot waste of time (in the past and present) and I personally hate it.
What I found even stranger is that for example in:
MySQL: DATE_ADD('2011-01-31', INTERVAL 1 MONTH) returns 2011-02-28
or
C# where new DateTime(2011, 01, 31).AddMonths(1); will return 28.02.2011
wolframalpha.com giving 31.01.2013 + 1 month as input; will return Thursday, February 28, 2013
It sees to me that others have found a more decent solution to the stupid question that I saw alot in PHP bug reports "what day will it be, if I say we meet in a month from now" or something like that. The answer is: if 31 does not exists in next month, get me the last day of that month, but please stick to next month.
So MY QUESTION IS: is there a PHP function (written by somebody) that resolves this not officially recognized bug? As I don't think I am the only one who wants another behavior when adding / subtracting months.
I am particulary interested in solutions what also work not just for the end of the month, but a complete replacement of strtotime. Also the case strotime +n months should be also dealt with.
Happy coding!
what you need is to tell PHP to be smarter
$the_date = strtotime('31.01.2011');
echo date('r', strtotime('last day of next month', $the_date));
$the_date = strtotime('31.03.2011');
echo date('r', strtotime('last day of next month', $the_date));
assuming you are only interesting on the last day of next month
reference - http://www.php.net/manual/en/datetime.formats.relative.php
PHP devs surely don't consider this as bug. But in strtotime's docs there are few comments with solutions for your problem (look for 28th Feb examples ;)), i.e. this one extending DateTime class:
<?php
// this will give us 2010-02-28 ()
echo PHPDateTime::DateNextMonth(strftime('%F', strtotime("2010-01-31 00:00:00")), 31);
?>
Class PHPDateTime:
<?php
/**
* IA FrameWork
* #package: Classes & Object Oriented Programming
* #subpackage: Date & Time Manipulation
* #author: ItsAsh <ash at itsash dot co dot uk>
*/
final class PHPDateTime extends DateTime {
// Public Methods
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Calculate time difference between two dates
* ...
*/
public static function TimeDifference($date1, $date2)
$date1 = is_int($date1) ? $date1 : strtotime($date1);
$date2 = is_int($date2) ? $date2 : strtotime($date2);
if (($date1 !== false) && ($date2 !== false)) {
if ($date2 >= $date1) {
$diff = ($date2 - $date1);
if ($days = intval((floor($diff / 86400))))
$diff %= 86400;
if ($hours = intval((floor($diff / 3600))))
$diff %= 3600;
if ($minutes = intval((floor($diff / 60))))
$diff %= 60;
return array($days, $hours, $minutes, intval($diff));
}
}
return false;
}
/**
* Formatted time difference between two dates
*
* ...
*/
public static function StringTimeDifference($date1, $date2) {
$i = array();
list($d, $h, $m, $s) = (array) self::TimeDifference($date1, $date2);
if ($d > 0)
$i[] = sprintf('%d Days', $d);
if ($h > 0)
$i[] = sprintf('%d Hours', $h);
if (($d == 0) && ($m > 0))
$i[] = sprintf('%d Minutes', $m);
if (($h == 0) && ($s > 0))
$i[] = sprintf('%d Seconds', $s);
return count($i) ? implode(' ', $i) : 'Just Now';
}
/**
* Calculate the date next month
*
* ...
*/
public static function DateNextMonth($now, $date = 0) {
$mdate = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
list($y, $m, $d) = explode('-', (is_int($now) ? strftime('%F', $now) : $now));
if ($date)
$d = $date;
if (++$m == 2)
$d = (($y % 4) === 0) ? (($d <= 29) ? $d : 29) : (($d <= 28) ? $d : 28);
else
$d = ($d <= $mdate[$m]) ? $d : $mdate[$m];
return strftime('%F', mktime(0, 0, 0, $m, $d, $y));
}
}
?>
Here's the algorithm you can use. It should be simple enough to implement yourself.
Have the original date and the +1 month date in variables
Extract the month part of both variables
If the difference is greater than 1 month (or if the original is December and the other is not January) change the latter variable to the last day of the next month. You can use for example t in date() to get the last day: date( 't.m.Y' )
Had the same issue recently and ended up writing a class that handles adding/subtracting various time intervals to DateTime objects.
Here's the code:
https://gist.github.com/pavlepredic/6220041#file-gistfile1-php
I've been using this class for a while and it seems to work fine, but I'm really interested in some peer review. What you do is create a TimeInterval object (in your case, you would specify 1 month as the interval) and then call addToDate() method, making sure you set $preventMonthOverflow argument to true. The code will make sure that the resulting date does not overflow into next month.
Sample usage:
$int = new TimeInterval(1, TimeInterval::MONTH);
$date = date_create('2013-01-31');
$future = $int->addToDate($date, true);
echo $future->format('Y-m-d');
Resulting date is:
2013-02-28
Here is an implementation of an improved version of Juhana's answer above:
<?php
function sameDateNextMonth(DateTime $createdDate, DateTime $currentDate) {
$addMon = clone $currentDate;
$addMon->add(new DateInterval("P1M"));
$nextMon = clone $currentDate;
$nextMon->modify("last day of next month");
if ($addMon->format("n") == $nextMon->format("n")) {
$recurDay = $createdDate->format("j");
$daysInMon = $addMon->format("t");
$currentDay = $currentDate->format("j");
if ($recurDay > $currentDay && $recurDay <= $daysInMon) {
$addMon->setDate($addMon->format("Y"), $addMon->format("n"), $recurDay);
}
return $addMon;
} else {
return $nextMon;
}
}
This version takes $createdDate under the presumption that you are dealing with a recurring monthly period, such as a subscription, that started on a specific date, such as the 31st. It always takes $createdDate so late "recurs on" dates won't shift to lower values as they are pushed forward thru lesser-valued months (e.g., so all 29th, 30th or 31st recur dates won't eventually get stuck on the 28th after passing thru a non-leap-year February).
Here is some driver code to test the algorithm:
$createdDate = new DateTime("2015-03-31");
echo "created date = " . $createdDate->format("Y-m-d") . PHP_EOL;
$next = sameDateNextMonth($createdDate, $createdDate);
echo " next date = " . $next->format("Y-m-d") . PHP_EOL;
foreach(range(1, 12) as $i) {
$next = sameDateNextMonth($createdDate, $next);
echo " next date = " . $next->format("Y-m-d") . PHP_EOL;
}
Which outputs:
created date = 2015-03-31
next date = 2015-04-30
next date = 2015-05-31
next date = 2015-06-30
next date = 2015-07-31
next date = 2015-08-31
next date = 2015-09-30
next date = 2015-10-31
next date = 2015-11-30
next date = 2015-12-31
next date = 2016-01-31
next date = 2016-02-29
next date = 2016-03-31
next date = 2016-04-30
I have solved it by this way:
$startDate = date("Y-m-d");
$month = date("m",strtotime($startDate));
$nextmonth = date("m",strtotime("$startDate +1 month"));
if((($nextmonth-$month) > 1) || ($month == 12 && $nextmonth != 1))
{
$nextDate = date( 't.m.Y',strtotime("$initialDate +1 week"));
}else
{
$nextDate = date("Y-m-d",strtotime("$initialDate +1 month"));
}
echo $nextDate;
Somewhat similar to the Juhana's answer but more intuitive and less complications expected. Idea is like this:
Store original date and the +n month(s) date in variables
Extract the day part of both variables
If days do not match, subtract number of days from the future date
Plus side of this solution is that works for any date (not just the border dates) and it also works for subtracting months (by putting - instead of +).
Here is an example implementation:
$start = mktime(0,0,0,1,31,2015);
for ($contract = 0; $contract < 12; $contract++) {
$end = strtotime('+ ' . $contract . ' months', $start);
if (date('d', $start) != date('d', $end)) {
$end = strtotime('- ' . date('d', $end) . ' days', $end);
}
echo date('d-m-Y', $end) . '|';
}
And the output is following:
31-01-2015|28-02-2015|31-03-2015|30-04-2015|31-05-2015|30-06-2015|31-07-2015|31-08-2015|30-09-2015|31-10-2015|30-11-2015|31-12-2015|
function ldom($m,$y){
//return tha last date of a given month based on the month and the year
//(factors in leap years)
$first_day= strtotime (date($m.'/1/'.$y));
$next_month = date('m',strtotime ( '+32 day' , $first_day)) ;
$last_day= strtotime ( '-1 day' , strtotime (date($next_month.'/1/'.$y)) ) ;
return $last_day;
}
I'm not sure how to go about this one. I'm building a calendar in PHP and need users to be able to add a repeating event that follows the following rule:
Last [DOW] of the month (so Last [Mon/Tues/Wed/etc] of the week)
I've got the rule itself stored, I'm just not sure how best to extrapolate the last Mon/Tue/Wed of the month in PHP? I fell like i'm making this more complicated than it needs to be.
Assuming you have variables for $month=4, $dow=3 and $year=2011 how would I best do this?
For everything date-related that you can express in proper English but have a hard time expressing using numbers, strtotime is your best friend.
echo strtotime("last Monday of June 2011");
This returns a timestamp that you can use as the second parameter to date and the likes to get a proper, human-readable date. Since it's a built-in function written in C, this solution is also much faster than almost anything else you could come up with written in PHP (though I'm quite sure it wouldn't matter much in a real-world scenario).
So assuming you have $month=4, $dow=3 and $year=2011, you'll need an array mapping $month values to their English textual representations and another array mapping $dow values to their textual representations.
Here's an alternative:
<?
function lastDayOfMonth($month, $year) {
switch ($month) {
case 2:
# if year is divisible by 4 and not divisible by 100
if (($year % 4 == 0) && ($year % 100) > 0)
return 29;
# or if year is divisible by 400
if ($year % 400 == 0)
return 29;
return 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
function lastDayOfWeek($month, $year, $dow) {
$d = new DateTime();
#Finding the last day of month
$d = $d->setDate($year, $month, lastDayOfMonth($month, $year));
#Getting the day of week of last day of month
$date_parts = getdate($d->getTimestamp());
$diff = 0;
#if we can't find the $dow in this week... (cause it would lie on next month)
if ($dow > $date_parts['wday']) {
# ...we go back a week.
$diff -= 7;
}
return $date_parts['mday'] + $diff + ($dow - $date_parts['wday']);
}
# checking the algorithm for this month...
for ($i=0; $i < 7; $i++) {
echo lastDayOfWeek(6,2011,$i) . "<br>";
}
?>
I have a generic function for you to calculate the nth day of a month. Hope this could help you to resolve your issue.
function get_Nth_dow($dow, $occurence, $m, $y)
{
$numdays = date('t', mktime(0, 0, 0, $m, 1, $y));
$add = 7 * ($occurence - 1);
$firstdow = date('w', mktime(0, 0, 0, $m, 1, $y));
$diff = $firstdow - $dow;
$day_of_month = 1;
if ($diff > 0)
{
$day_of_month += ($add - $diff);
}
elseif ($diff < $numdays)
{
$day_of_month -= ($diff - $add);
}
return $day_of_month;
}
$DOW = day of the week (0 = Sunday, 6 = Saturday).
$X = occurence (1 = first, 2 = third, etc..). If the given month does
not have the occurrence, then it will return the last. For example, if
you ask for the 7th occurrence of Friday, it will return the last
Friday of the month.
$M = month
$Y = year
Example, get_Nth_DOW(2,3,7,2009) will return the third Tuesday of 7th 2009.
How do I go about getting all the work days (mon-fri) in a given time period (let's say, today till the end of the next month) ?
If you're using PHP 5.2+ you can use the library I wrote in order to handle date recursion in PHP called When.
With the library, the code would be something like:
$r = new When();
$r->recur(<start date here>, 'weekly')
->until(<end date here>)
->wkst('SU')
->byday(array('MO', 'TU', 'WE', 'TH', 'FR'));
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
This sample does exactly what you need, in an quick and efficient way.
It doesn't do nested loops and uses the totally awesome DateTime object.
$oDateTime = new DateTime();
$oDayIncrease = new DateInterval("P1D");
$aWeekDays = array();
$sStart = $oDateTime->format("m-Y");
while($oDateTime->format("m-Y") == $sStart) {
$iDayInWeek = $oDateTime->format("w");
if ($iDayInWeek > 0 && $iDayInWeek < 6) {
$aWeekDays[] = clone $oDateTime;
}
$oDateTime->add($oDayIncrease);
}
Try it here: http://codepad.org/wuAyAqnF
To use it, simply pass a timestamp to get_weekdays. You'll get back an array of all the weekdays, as timestamps, for the rest of the current month. Optionally, you can pass a $to argument - you will get all weekdays between $from and $to.
function get_weekdays ($from, $to=false) {
if ($to == false)
$to = last_day_of_month($from);
$days = array();
for ($x = $from; $x < $to; $x+=86400 ) {
if (date('w', $x) > 0 && date('w', $x) < 6)
$days[] = $x;
}
return $days;
}
function last_day_of_month($ts=false) {
$m = date('m', $ts);
$y = date('y', $ts);
return mktime(23, 59, 59, ($m+1), 0, $y);
}
I suppose you could loop through the dates and check the day for each one, and increment a counter.
Can't think of anything else off the top of my head.
Pseudocode coming your way:
Calculate the number of days between now and the last day of the month
Get the current day of the week (i.e. Wednesday)
Based on the current day of the week, and the number of days left in the month, it's simple calculation to figure out how many weekend days are left in the month - it's going to be the number of days remaining in the month, minus the number of Sundays/Saturdays left in the month.
I would write a function, something like:
daysLeftInMonth(daysLeftInMonth, startingDayOfWeek, dayOfWeekToCalculate)
where:
daysLeftInMonth is last day of the month (30), minus the current date (15)
startingDayOfWeek is the day of the week you want to start on (for today it would be Wednesday)
dayOfWeekToCalculate is the day of the week you want to count, e.g. Saturday or Sunday. June 2011 currently has 2 Sunday, and 2 Saturdays left 'til the end of the month
So, your algorithm becomes something like:
getWeekdaysLeft(todaysDate)
...getWeekdaysLeft is something like:
sundaysLeft = daysLeftInMonth(lastDayOfMonth - todaysDate, "Wednesday", "Sunday");
saturdaysLeft = daysLeftInMonth(lastDayOfMonth - todaysDate, "Wednesday", "Saturday");
return ((lastDayOfMonth - todaysDate) - (sundaysLeft + saturdaysLeft));
This code does at least one part you ask for. Instead of "end of next month" it simply works with a given number of days.
$dfrom = time();
$fourweeks = 7 * 4;
for ($i = 0; $i < $fourweeks; $i ++) {
$stamp = $dfrom + ($i * 24 * 60 * 60);
$weekday = date("D", $stamp);
if (in_array($weekday, array("Mon", "Tue", "Wed", "Thu", "Fri"))) {
print date(DATE_RSS, $stamp) . "\n";
}
}
// Find today's day of the month (i.e. 15)
$today = intval(date('d'));
// Define the array that will hold the work days.
$work_days = array()
// Find this month's last day. (i.e. 30)
$last = intval(date('d', strtotime('last day of this month')));
// Loop through all of the days between today and the last day of the month (i.e. 15 through 30)
for ( $i = $today; $i <= $last; $i++ )
{
// Create a timestamp.
$timestamp = mktime(null, null, null, null, $i);
// If the day of the week is greater than Sunday (0) but less than Saturday (6), add the timestamp to an array.
if ( intval(date('w', $timestamp)) > 0 && intval(date('w', $timestamp)) < 6 )
$work_days[] = mktime($timestamp);
}
The $work_days array will contain timestamps which you could use this way:
echo date('Y-m-d', $work_days[0]);
The code above with work in PHP 4 as well as PHP 5. It does not rely on the functionality of the DateTime class which was not available until PHP 5.2 and does not require the use of "libraries" created by other people.
I am trying to explain the details so that this is easy to understand.
I want a list of month and year based on the difference of month and year.
I am implementing search functionality based on start month with year and end month with year.
So!
start Selection - 01(month)-2009 (Yr)
End selection 10(month)-2009 (Yr)
What I want from MySQL is:
Month Year
JAN 2009
FEB 2009
MAR 2009
APR 2009
MAY 2009
JUN 2009
JUL 2009
AUG 2009
SEP 2009
OCT 2009
FractalizeR answer is the right one.
Just let me expand by defining the functions:
function GetMonthsFromDate($myDate) {
$year = (int) date('Y',$myDate);
$months = (int) date('m', $myDate);
$dateAsMonths = 12*$year + $months;
return $dateAsMonths;
}
function GetDateFromMonths($months) {
$years = (int) $months / 12;
$month = (int) $months % 12;
$myDate = strtotime("$years/$month/01"); //makes a date like 2009/12/01
return $myDate;
}
PS: tried to post as a comment but the formating got screwed.
(Of course this functions could be rewritten as one liners but wanted to be more readable)
You need to write some functions to convert your dates to a number of months passed since certain date and back. For example, since Jan 1980.
Jan 1980 = 0;
Dec 1980 = 12;
Jan 1981 = 13 etc.
Then you just do simple "for" loop:
for ($i = GetMonthsFromDate($StartDate), $i <= GetMonthsFromDate($StopDate), $i++) {
echo(GetDateFromMonths($i));
}
Although FractalizeR answer is the correct one.
There is another option.
Taking advantage from the fact that that strtotime('2009/08/01 - 1 month') will do the right thing and delete 1 month.
<?php
$startDate = strtotime("$startYear/$startMonth/01");
$endDate = strtotime("$endYear/$endMonth/01");
$currentDate = $endDate;
while ($currentDate >= $startDate) {
echo date('Y/m',$currentDate);
$currentDate = strtotime( date('Y/m/01/',$currentDate).' -1 month');
}
Again a list of months
Here is the final answer which is worked very great
$startMonth= $_POST['startmonth'];
$startyear= $_POST['startyear'];
$cYear = $startyear;
$endMonth= $_POST['endmonth'];
$endyear= $_POST['endyear'];
$sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")";
$queryRS = $db->query($sql);
$tmonthsarray = $db->fetchRow($c_jobsVat);
$totalmonths=tmonthsarray[0];
for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++)
{
$processYear = $startyear + intval( ( $count - 1 ) / 12 );
$processMonth= (( $count - 1 ) % 12 + 1);
$processMonthName= date('F', mktime(0,0,0,$count));
}
I wrote that function based on you guys's input. Check this out:
function CountTheMonth($startDate,$endDate,$order)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$ASC_Month = $startDate;
$DESC_Month = $endDate;
$Y_Axis = Array();
if($order == 'DESC')//Big to small
{
while ($DESC_Month >= $startDate)
{
$Y_Axis[] = date('F-Y',$DESC_Month);
$DESC_Month = strtotime( date('Y-m-d',$DESC_Month).' -1 month');
}
return $Y_Axis;
}
elseif($order == 'ASC')//Small to big
{
while ($ASC_Month <= $endDate)
{
$Y_Axis[] = date('F-Y',$ASC_Month);
$ASC_Month = strtotime( date('Y-m-d',$ASC_Month).' +1 month');
}
return $Y_Axis;
}
}
Your logic works great to get a list of months. But not sure how we can deal with years in this case.
Here is my code:
$startMonth= $_POST['startmonth'];
$startyear= $_POST['startyear'];
$endMonth= $_POST['endmonth'];
$endyear= $_POST['endyear'];
$sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")";
$queryRS = $db->query($sql);
$tmonthsarray = $db->fetchRow($c_jobsVat);
$totalmonths= $tmonthsarray[0];
for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++)
{
echo "<BR>==>".date('F', mktime(0,0,0,$count)) ; // Months
// what comes here in case of listing year
}
I definitely agree with the solution of #FractalizeR and the addition by #elviejo , it helped me forward. But I believe I encountered some issues, and they might of help to others: (+ please correct me if I am wrong)
Important to note: both $StartDate and $StopDate are of type timestamp, not actually the date-type (1). So I changed the function parameters to $myTimestamp in the hope of not confuse others.
For example:
$StartDate = strtotime("Sept 2010");
$StopDate = time(); // current timestamp
For the functions of #elviejo:
typecasting: a simple (int) didn't work for me (PHP5), so I used intval() instead
working with % gives results ranging from 0 to 11, while months are in the range of 1 to 12 (2). So in the function GetDateFromMonths($months), we have to +1 the result
Because we add 1 to the % in the function GetDateFromMonths($months), we will have to substract 1 from the $dateAsMonths in the function GetMonthsFromDate($myTimestamp).
This gives me the following code:
function GetMonthsFromDate($myTimestamp)
{
$year = (int) date('Y',$myTimestamp);
$months = (int) date('m', $myTimestamp);
$dateAsMonths = 12*$year + $months - 1;
return $dateAsMonths;
}
function GetDateFromMonths($months)
{
$years = intval($months / 12);
$month = intval($months % 12) + 1;
$myTimestamp = strtotime("$years/$month/01"); //makes a date like 2009/12/01, and translate it to a timestamp
return $myTimestamp;
}
Started by the following for-loop:
for ($i = GetMonthsFromDate($StartDate); $i <= GetMonthsFromDate($StopDate); $i++)
{
echo(date('d M Y',GetDateFromMonths($i))."</br>");
}
Which gave the wanted result. Do my remarks make sense?
Notes:
(1) I don't even think date exists as a type in PHP, but I might be wrong.
(2) January is 1, February is 2, ... November is 11 but December is 0. This will not give the wanted result.