PHP Date Calculator Returning Wrong Days - php

I'm using the below calculator to determine the years, months, and days between a set date and the current date. I thought it was working fine, but then it came up on the year mark and i noticed it was not working properly. Tomorrow is actually when the one year mark would be, but it is currently returning 11 months, 34 days. Could anyone tell me whats wrong? It should be 11 months, 30 days.
function relationshipTimer($functionDate)
{
$date1 = $functionDate;
$date2 = date("Y-m-d");
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($years > 0) {echo $years . " Year";}
if ($years > 1) {echo "s ";}
if ($months > 0) {echo " " . $months . " Month";}
if ($months > 1) {echo "s ";}
if ($date1 == $date2) {echo "1 Day ";}
if ($days > 0) {echo $days . " Day";}
if ($days > 1) {echo "s ";}
}
And this is where $functionDate comes from:
relationshipTimer("2018-04-28");

When you calculate $days, you are assuming that all months are 30 days long, which is obviously wrong. Therefore you get a year that is 11 months plus 35 days (36 days for leap years).
Processing dates is complicated. You should always use specialized tools like PHP's DateTime::diff()
For example, with:
$date1 = new DateTime("2018-04-28");
$date2 = new DateTime("2019-04-27");
$diff = $date2->diff($date1);
print $diff->format("%y years %m months %d days\n");
... you get (because April has 30 days):
0 years 11 months 29 days

Related

Removing seconds from time and rounding the minutes

I'm storing time entries to a variable called duration. So right now If I log a time entry, it will be in the standard format: ex) 12:30:00
What I want to do is remove the seconds part of the time entry and round the minutes to every 15th minute. Also I'de like to remove the 0 in the front of if the times before 10.
So 09:00:00 would become 9
09:30:00 would be come 9:30
So 12:30:00 would be 12:30.
12:08:00 would be 12:15
12:34:00 would be 12:30 ect ect.
Here's the code I was using:
$duration = '';
if ($seconds < 0) {
$duration = '-';
$seconds = abs($seconds);
}
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
return $duration . sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
}
and here's a sample output:
1 => "12:20:00"
Try this.
$currentTime = strtotime('12:34:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:30
If this
$currentTime = strtotime('12:08:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:15
check demo. demo
For round, you may check with minutes zero condition like.
$currentTime = strtotime('9:00:00');
if(date('i',ceil($currentTime / (15 * 60)) * (15 * 60)) == 00){
echo date('H',ceil($currentTime / (15 * 60)) * (15 * 60));
}

PHP Time difference only display when not zero

I'm calculating the difference between two dates (the current date and a date in the database). I want to display the difference in days and hours between the dates, but when the day-difference is 0 I don't want to display it. This is my code:
<?php
$to_time = new \DateTime($database_time);
$from_time = new \DateTime();
echo $from_time->diff($to_time)->format("%d %H");
?>
Output should be, for example:
> 50 days and 4 hours
> 1 day and 7 hours //and not 1 dayS
> 6 hours //and not 0 days and 6 hours
There are two issues with my own code: the first one is that it always display the number of days. The second one is that it is just a format. For example when the difference is 2 months, 6 days and 2 hours it displays: 6 days and 2 hours. But it should be 68 days and 2 hours (because of the two months).
Can't get it work with other codes I found. Thanks in advance!
Try this..
$seconds = strtotime("2012-10-10 02:40:03") - strtotime("2010-12-25 05:15:02");
echo $days = floor($seconds / 86400);
echo "</br>";
echo $hours = floor(($seconds - ($days * 86400)) / 3600);
echo "</br>";
echo $minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
echo "</br>";
echo $seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));

i want 2008-12-13 10:42:00 show second Minute and date in php

I want 2008-12-13 10:42:00 show second Minute and date in php
$days = floor($row['TimeSpent'] / (60 * 60 * 24));
$remainder = $row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d Y', $row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "few seconds ago";
elseif($days == 0)
echo $minutes.' minutes ago';
else
echo "few seconds ago";
Use PHP DateTime like this:
$datetime1 = new DateTime('2008-12-13 10:42:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years') . "<br>";
echo $interval->format('%m months') . "<br>";
echo $interval->format('%d days') . "<br>";
echo $interval->format('%h hours') . "<br>";
echo $interval->format('%i minutes') . "<br>";
echo $interval->format('%s seconds ago');
The output would be something like:
6 years
4 months
4 days
6 hours
32 minutes
56 seconds ago
This is so common usecase these days so lot of developers have already solved this issue, one of such solution is using php Carbon library found here
http://carbon.nesbot.com/
with Carbon library installed you can just do it like this
echo Carbon::createFromFormat('Y-m-d H:i:s', '2008-12-13 10:42:00')->diffForHumans();
You can find more about this usage here
http://carbon.nesbot.com/docs/#api-humandiff
Remember most of the problems we face in development is already solved, so do a little research I am sure you will find good solutions.

Display date/time in readable and brief format

I stored my dates and times in unix timestamp format (d1=1387721302, d2=1311343703) and would like to view date differences(past, present and future) in say.
2 Weeks ago
15 Days ago
2 Minutes ago
3 Months ago
1 Year ago
9 Months From Now
4 Days From Now
etc.
..hope you catch the drift?
instead of "0 Years, 4 Months, 4 Days"
Would appreciate a function or some sort.
This is actually what I use now
$date1 = "2007-03-24";
$date2 = "2009-06-26";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
Thank you
This is what I tried doing but not accurate but as a guide.
Anybody pls help.
if ($years >= 1)
{
$dis_date = printf("%d years\n", $years);
}elseif ($years <= 0 && $months >= 0)
{
$dis_date = printf("%d months, %d days\n", $months, $days);
}elseif ($years <=0 && $months <= 0 && $days >= 0)
{
$dis_date = printf("%d days\n", $days);
}else{
$dis_date = printf("%d years, %d months, %d days\n", $years, $months, $days);
}
echo $dis_date;
function f1 ($time)
{
$rel = time() - $time;
if ($rel < 60 * 60)
$rel .= ' min ago';
elseif ($rel < 60 * 60 * 24)
$rel .= ' hours ago';
elseif ($rel < 60 * 60 * 24 * 30)
$rel .= ' days ago';
....
return $rel
}
You have years, months and days. You need nothing else. Just check if years is 0, then month is 0, then days is 0. Then print the data accordingly.
Having that kind of control is good actually, you may want to write "60 years" instead of "60 years 2 months 3 days"

Finding the difference between 2 dates

I made this and needed some help tweaking it so that it gives the proper outcome
function daysDifference($end){
//$start = "2007-03-24";
//$end = "2009-06-26";
$now = date("Y-m-d");
$e = (is_string($end) ? strtotime($end) : $end);
$diff = abs($e - strtotime($now));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24)/ (60 * 60 *24));
return ($years == 0 ? '' : ($years == 1 ? $years . ' year ' : $years . ' years ')) . ($months == 0 ? '' : ($months == 1 ? $months . ' month ' : $months . ' months ')) . ($days == 0 ? '' : ($days == 1 ? $days . ' day ' : $days . ' days '));
}
$end is being pulled from my database so there is checks to see if its a string or a date already.
$e now can be used, but when I tried to subtract $now from $e I get funny results
for instance:
$now being today the 13th and $e being an end date for a project, it's suppose to give me what I need... right?
Where I'm suppose to get say 12 days remaining, I get 1 year 12 days.
and where $e = 0000-00-00 (in case the user didn't input an end date), I get 40 years 10 months and 26 days remaining.
I tried alot of different variations to my calculations but I keep getting nowhere.
Why would you reinvent the wheel? Use date_diff:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
Store real dates, not strings, and you can just ask the database for the difference.
SELECT DATEDIFF(CURRENT_DATE, end) FROM table
If you just go dividing things by 365 you won't get accurate results. Not every year has 365 days.

Categories