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.
Related
This question already has answers here:
PHP DateTime::modify adding and subtracting months
(19 answers)
Closed 6 years ago.
This is the first time I've ever used the built-in DateTime class, but we have to do calculations based on months previous/forward and are getting some interesting results when inputting day numbers that exist in one month but not another, and I'm not sure if PHP is really doing it accurately! (BTW, I've only tested this on v5.2 legacy code which I have to work on for now)
So for instance, if I input today's date (2016-10-31), subtract 6 months (with ->modify('-6 months')), the date outputted (with ->format('Y-m-d')) is May 1st! (2016-05-01). This implies PHP is just moving up the chain to the days in the next month (so 29 for Feb in a non-leap year is Mar 1, 30 is Mar 2, 31 is Mar 3, etc).
Using this logic, I deduced May 31 minus 1 month would be May 1st, which it was when I tested it!
Is this an accurate way to add/subtract by month? I'm not sure yet if our departments calculate this way, but I'm curious if anyone else has run into this.
This is a bit broad but...
The problem with how you're thinking is that
2016-10-31 - 6 months = 2016-04-30
PHP is thinking as such
$date = new DateTime('2016-10-31');
$date->modify('-6 months'); // PHP subtracts 183 days in all the tests I ran
echo $date->format('Y-m-d'); // 2016-05-01
You're going to have to come up with your own methodology because modify is basically a guesstimate for things like months, which can be 28-31 days long. In other words, PHP does a lot of things that are "good enough" if you don't need high precision.
Hello I'm trying to check how long message is in database I'm using laravel4 framework and I have this:
date( "h", strtotime($message->created_at)) - date('h')
but it only counts hours I need to change days and month to hours and then count how long is it in the database. How can i do it?
date() returns a string, and you can't do math on strings. date('h') returns the current hour, eg: 10 now because it is 10am. If $message->created_at was Feb 3, 1978 10:01:02 then date('h', strtotime($message->created_at)) would return 10 as well.
Assuming $message->created_at is in an acceptable format:
$diff_in_hours = (time() - strtotime($message->created_at)) / 3600;
Your question is a bit unclear, but it sounds to me like you're looking for what is called a "human readable timestamp". If you've set up your database table correctly, both created_at and updated_at should be retrieved as carbon objects, giving you several different ways to display your date. It's actually very simple to achieve the desired effect:
$message->created_at->diffForHumans()
This should then print a readable time just like you see here on stackoverflow (1 hour ago, 23 minutes ago, etc...).
Check out the link for more information about carbon objects.
If I use the following code
$averageWeeks = 2.7;
$laidDate->modify('+'.$averageWeeks.' week');
I get the date 31st May 2014 returned, whereas if i use the following code
$averageWeeks = 3;
$laidDate->modify('+'.$averageWeeks.' week');
I get the date 3rd May 2014 which is right. Is there any way I can use decimal places in the date modify method?
Assuming you're starting at April 12th, then May 3rd would be 3 weeks and May 31st would be 7 weeks. So my guess is that when you pass "2.7", it is ignoring the "2." portion of that string.
You could consider calculating the number of days to add separately, such as:
$averageWeeks = 2.7;
$averageDays = floor($averageWeeks * 7);
$laidDate->modify('+'.$averageDays.' days');
Consider also that 2.7 weeks is 18.9 days. By taking the floor, We are adding 18 days. It doesn't make since to add fractional days here, unless you intend to change the time of day as well. If you did, you'd have to do some additional math, as it doesn't appear that either modify or add will support fractional values. You could do something like this:
$averageWeeks = 2.7;
$averageSeconds = floor($averageWeeks * 7 * 24 * 60 * 60);
$laidDate->modify('+'.$averageSeconds.' seconds');
But you might be fooling yourself if you think this time is accurate, since not every local day has exactly 24 hours in it, due to daylight saving time transitions.
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?
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