I'm looking for a way to count the months that has passed, since a specific date till today. For example, from januari till today (june) would be '5'
Keep it mind, it should work if its a year later, so the year has to be included.
Use date_diff()
$df = date_diff(date_create('01/08/2012'),date_create('05/08/2012'));
echo $df->format("Month: %M");
Related
I failed to find a proper solution to this issue. As you see in Example #3 in the PHP documentation, they state that one must beware when adding months using the DateInterval in DateTime::add.
There's not really any explanation for why the method's behavior is as such and what I can do to avoid this, which I find to be an error at first sight.
Anyone have some insight into this?
The issue is that each month can have a different number of days in them. The question is what you're doing when you want to increment a date by 1 month. Per the PHP documentation if you're on January 31st (or 30th) and you add 1 month, what is the expected behavior?
February only has 29 days in it. Do you want to be set to the last day of the month? You're generally safer incrementing by a set number of days if that's what you're looking for, or a static date based on the current date. Without knowing what you're trying to accomplish when you increment your month, it's tough to say how to watch for an error.
EDIT:
As someone mentions in the similar post commented by Mike B above, you probably want to do something where you (in pseudocode):
1) Use cal_days_in_month() for the next month and save that number to a variable x
2) If x >= current billing DOB, increment and be done
3) DateTime::modify('last day') (havent used this before but something along these lines) to set the date to the last date of the next month (set it to the 1st of the next month, then last day?)
Worth noting is that if you use the variable here as the new billing value, you'll wipe out your original value. I would save an extra DB value that's "first billing date" or just "billing_day_of_month" or something, and use that to figure out the day of month that you should be looking at
If your goal is to strictly increment by user-friendly months (thus, 3 months from January 21st should be April 21st), with the exception that shorter membership months get shortened (thus, 1 month from January 31st is February 28th/29th), then you only need to go back a few days if you crossed over into the next month:
function addMonths($date,$months) {
$orig_day = $date->format("d");
$date->modify("+".$months." months");
while ($date->format("d")<$orig_day && $date->format("d")<5)
$date->modify("-1 day");
}
$d = new DateTime("2000-01-31");
addMonths($d,1);
echo $d->format("Y-m-d"); // 2000-02-29
I am well versed in using strtotime or the date modify functions within PHP to find NEXT or PREVIOUS days/months/years, or to add or subtract dates. There is the ability to do something like strtotime("first day of last month") but there is not the ability to use a specific month name (let's say July) as a parameter...so it seems I cannot say strtotime("first day of last July").
My question is NOT
"can you write me some code to give me the date"
but rather
"is there a streamlined 1 or 2 line approach using strtotime() or
something else that will enable me to reach the same output with
something more compact, tidy, and clear?"
I am trying to streamline some fairly clunky code to figure out the date (really just the year) of July 1 two instances ago...not the most recent past July 1, but the one a year prior to that (so it could be a date almost 2 full years in the past...or could be as recent as 1 year and 1 day in the past).
For example:
Assume today is February 26, 2014. I am trying to output 2012-07-01.
However a bit later this same year... let's say on July 2 of 2014...
the output would now be 2013-07-01
So, essentially I need to figure out if July 1 has already happened in the current year...and if not then subtract 2 from the current year...and if yes, then subtract 1 from the current year.
A clunky version looks like this:
$CurrentDate = date("Y-m-d");
$CurrentYear = date("Y");
$ThisYearCutOff = $CurrentYear.'-07-01';
if($CurrentDate > $ThisYearCutOff){
echo ($CurrentYear-1).'-07-01';
}
else
{
echo ($CurrentYear-2).'-07-01';
}
Any thoughts on how to do this in a relatively streamlined bit of php code?
So I'm just making a basic calendar for each month, just to play with the date function in PHP. I use something very simple code that I was thinking of throwing into a loop and populate some cells in a table:
public function getDayDate(){
$month = "January";
$day="1";
$year="2014";
$theW = "$month $day $year";
//First day of the week on a month
echo date("D", strtotime($theW));
//# of days in a month
echo date("t", strtotime($theW));
}
But it came to my mind about leap year and all other kinds of calendar events that may effect the number of days in a month. And i was wondering if this basic setup automatically factors these things in with the data here. Cause I figured I can have start on a particular cell like Wednesday and loop it 28-31 times to add the day to each cell until it completes.
Is this wrong? I tried searching for about a day, and most of the questions are more specific for finding the leap year and/or event, instead of it automatically just giving the end result, which is the number of days in the month and what day of the week it starts on.
I appreciate your help!
Yes, PHP's DateTime class does. You can even check if it is a leap year with the L formatter.
$date = date_create();
$isLeapYear = $date->format('L');
var_dump($isLeapYear);
You can easily test it. Calculate the diff between Feb 27th and March 2nd for example in a leap year and in a non leap year and you will see that they are different.
I recommend you use DateTime::createFromFormat for transforming your string into a date.
I'm looking for a way to filter for events that happened between previous September (1st) and upcoming September (1st) every year. Since the filter is already part of an existing piece of code I can ony add a fixed date (timestamp) or use a relative date that runs through php strtotime. I can't run any php in the field.
Because a timestamp is fixed for a set year, I want to use relative dates to make this filter work every year.
I have tried to build this filter using simple things like [last/next] September, but that is invalid strtotime syntax.
From there I tried things like 9/1 [last/this/next] year. They were valid, but since I can't use logic to determine which one to use, it is not good.
For dates prior to this year September 1st the filter would be 9/1
last year>9/1 this year
For dates past September 1st this year it is 9/1 this year>9/1 next year.
So to summarize, I am looking for a relative date string (strtotime) to get the previous September 1st and a string for the upcoming September first. (If they exist.)
i guess you want dates in between 2012/09/01 - 2013/09/01.
you can use string in link if that's what your talking about, but you need years.
$this_year='2013/09/01';
$last_year = date("Y-m-d",strtotime("$this_year - 1 year"));
$next_year = date("Y-m-d",strtotime("$this_year + 1 year"));
echo
" last year
this year
next year
";
if(strtotime($this_year) <= strtotime($filter_date) and strtotime($filter_date) >= strtotime($last_year))
{
// dates in between 2012/09/01 - 2013/09/01
}
if(strtotime($this_year) >= strtotime($filter_date) and strtotime($filter_date) <= strtotime($next_year))
{
// dates in between 2013/09/01 - 2014/09/01
}
if you are getting data from database then use code below as its easier.
SELECT * FROM table_name WHERE 'date_field' BETWEEN ('$this_year') and ('$last_year ')
I am experiencing a rather strange problem using PHP 5.3's date diff function to calculate the difference in days between two dates. Below is my code:
$currentDate = new DateTime(); // (today's date is 2012-1-27)
$startDate = new DateTime('2012-04-01');
$diff = $startDate->diff($currentDate);
$daysBefore = $diff->d;
echo $daysBefore;
The above code displays 4 as the value of the $daysBefore variable.
Why is PHP displaying a difference of 4 days between the dates 27th Jan 2012 and 1st April 2012, when clearly there are many more days between these dates.
Am I doing something wrong?
DateInterval::$d is the days part of the interval, not the total number of days of the difference. For that, you want DateInterval::$days, so:
$daysBefore = $diff->days;
When creating a DateInterval through the DateTime::diff method, it populates not just days, but hours, minutes, seconds, months and even years in the single character properties. You're checking single-character d for days, which will be the days left over once years and months are calculated.
Try looking at the days property, which only actually gets populated when you use diff.
Behavior here is wildly inconsistent. Check out the DateInterval::format manual page for some interesting information about what happens when you create a DateInterval through various means.
The d property is the number of days as in "3 months, 4 days". If you want the total number of days, use the days property.
4 days, and a couple months...
Use $diff->days for total number of days.
http://www.php.net/manual/en/class.dateinterval.php