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
Related
I was wondering if there is a way to update a certain number value, per year?
Similar to how you would update a website date in the footer like this:
<?php echo date("Y"); ?>
I'm looking for a way to update a set number each year / month.
Example, say a website says they have been in business for 10 years. Then when the next year passes, I want PHP to automatically update the value to 11 years. Note, that I have to start with the number 10
Another example would be for age: John is 21. After a year passes I want PHP to update the site to say, John is 22.
Or Betty has worked here for 5 months.... After the next month passes it would update to 6, then once it hits 12 months is would change to 1 year then 2 years etc... Thats a little more involved, but you get the idea.
If someone knows how this can be achieved I would appreciate the help, or if you can point me in the right direction to solving this problem that would work too.
Yes, you need to understand two things - the current date and the date you are comparing it to. You then simply need to compare the too.
I would suggest using the PHP DateTime, DateInterval, etc. classes which greatly simply this for you.
For example:
$now = new DateTime('now');
$comparison_date = new DateTime('2005-01-01'); // roughly 10 years ago
$interval = $now->diff($comparison_date); // returns DateInterval object
$years_difference = $interval->y;
You can find your answer here:
How to calculate the difference between two dates using PHP?
By using strtotime() to convert two dates to unix time and calculate the number of seconds between them then reconvert to human readable.
To calculate the difference between to years you can use a simple sum like this:
<?php
$cur_year = date("Y");
$year = 2008;
$total_years = $cur_year - $year;
echo ($total_years);
?>
For months you could use a similar thing
Substract the initial Unix timestamp with the current timestamp (time() returns it), divide by 60 * 60 * 24 * 365, and round accordingly (you will probably want to floor() the years an user has been registered, and ceil() the years a bussiness have been working)
$start = 12345646;
$years = ceil((time() - $start) / 31536000.0); // Important: use a float
Note this uses "year" as a set of 365 days. It won't take into account leap years.
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 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");
I am writing a script to query dates "3 months ago". As I am thinking about making sure I do not miss anything, I am using this for comparison:
$date = date("Y-m-d",mktime(0,0,0,date("m")-$months_ago,date("d")));
I am realizing there is a chance I could be missing dates. For example, if I search on 11/30 for dates three months ago, I will get 8/30. But the next day is 12/1 and three months prior is 9/1. So in this script I have missed 8/31.
I guess the best method is to use days (90 days) instead on months. Is this the best practice for something like this?
Go directly with strtotime('-3 month'); you can also give negatives like -3 on the month param of the mktime and it will work like charm (better the second solution). No, it wont skip days - if "now" is 2011-06-17 it would return timestamp equivalent to 2011-03-17.
Edit:
Well, it might actually be true that you can miss days (I haven't checked your statement) but after all your unit of measurement of time is months, not days. What I'm saying is that in the Gregorian calendar month isn't constant amount of time - it could be 28, 29, 30 or 31 days.
Let's say you want to calculate months for a paid subscription period. If the user pays one month on 2011-02-15, when would his subscription expire? I would guess 2011-03-15, even though there are just 28 days between those two dates And if he pays for subscription on 2011-03-15, he would get full 31 days till 2011-04-15 and this seems perfectly fair to me as the subscription is "one month", which just happens to be different amount of days through the year.
If, in your case you don't want to get "3 months ago" but want to get constant amount of time that relatively represents "3 months", then you can use the medium month length - 88.59 days, or 88 days 14 hours and 10 minutes. That represented with code would be:
strtotime('-88 days -14 hours -10 minutes');
$when = strtotime('-3 months');
If you just need the month/year and don't want to have to calculate days:
$m = 5; // how many months ago, for example
$now = time();
$cm = date("m",$now); // current month
$yr = date("Y",$now) - intval((12 + $m - $cm)/12);
$month = (($cm + 11 - ($m % 12) ) % 12) + 1;
echo "$m months ago: $month yr: $yr\n";
I've already seen some answers in StackOverflow about how to calculate difference in time between two dates. But no answer are using the DateTime obejct or the Interval Object in PHP. I got the following code snippet from PHP Manual website: http://www.php.net/manual/en/dateinterval.format.php.
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
But the problem is that total days equals 6015 days when it should only be 31 days. I tried to access the instance variable days in the Interval object. It too shows 6015 days. But the instances for month and days intervals are correct. Can someone tell me why?
And I want to use these objects to calculate the difference in times!
Many thanks
UPDATE:
I think it was just a problem with my PHP setup
Running your code...
31 total days
1 month, 0 days
PHP setup issue, perhaps?
I ran that exact same script and got "31 total days" and "1 month, 0 days" (the expected values). Try upgrading your php maybe?..
I'm not sure what version of PHP you're on, but in my experience, I've made it a habit to always set my timezone with date_default_timezone_set('America/Los_Angeles'); where America/Los_Angeles is the timezone you want to be using. I don't know how this ultimately affects the date calculations, but I do know that PHP 5.3 will yell at you if you try and do date manipulation without specifying a default timezone.
I believe you can also set a default timezone in your php.ini- which I think by default is set to UTC.