Why seconds and minutes do not calculate properly in codeigniter? - php

I'm currently creating a CI project and i faced the folowing issue.
I want to show "posted "X" time ago" text and i found a script online but the seconds and minutes are not properly shown.
I've already searched all over the net but i couldn't find anything.
Here's my function:
$today = time();
$createdday = mysql_to_unix($ptime);
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
Here's the full pastebin https://pastebin.com/tzBN2gZW
Any thoughts on that?
Thanks

The error is occured because you don't reduce datediff after days counting. But I think it's more suitable to use DateTime objects for such calculating
$today = time();
$createdday = mysql_to_unix($ptime);
$today_d = new DateTime();
$today_d->setTimestamp($today);
$createdday_d = new DateTime();
$createdday_d->setTimestamp($createdday);
print_r($today_d->diff($createdday_d));
demo

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));
}

If hours over 24 don't reset

I currently made a function which tells from timestamp the hours, minutes and seconds left. But when the item is ending in more than 24 hours it should show example 48 hours and not resetting it. How can i do this?
I have days already, but i should be only in hours, minutes and seconds, ex.
76 hours, 20 minutes & 20 seconds.
$time = strtotime($row['start_date']) - time();
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = floor($time);
$time -= $seconds;
$hours = ($hours<10) ? "0" . $hours : $hours;
$minutes = ($minutes<10) ? "0" . $minutes : $minutes;
$seconds = ($seconds<10) ? "0" . $seconds : $seconds;
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
Your code removes the day part from the timestamp, that's why.

PHP < 5.3 => Difference in hours between two dates

In a current project I have hit a wall because I need to compare two dates and find the difference between the dates (in hours), this would be easy if the server was >= 5.3, can someone help me?
I have the difference in timestamp
$diff = abs(strtotime($date1)-strtotime($date2)
but I don't know what to do next...
Thank you.
In your code $diff is the difference in seconds. You can convert the seconds to hours like this:
$hours = floor($diff / (60 * 60));
Edit: To get minutes and seconds:
$minutes = floor(($diff - $hours * 60 * 60) / 60);
$seconds = floor($diff - $hours * 60 * 60 - $minutes * 60);
Try this
echo round((strtotime($date1) - strtotime($date2))/(60*60));
In your code $diff is in seconds.
There are 3600 seconds in an hour, so:
$date1 = "2014-07-15";
$date2 = "2014-07-17";
$diff_seconds = abs(strtotime($date1)-strtotime($date2));
$diff_hours = $diff_seconds/3600;
echo $diff_seconds.' '.$diff_hours;

Calculating years/months/days between dates

$date1 = "2000-01-01";
$date2 = "2011-03-14";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = ceil(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$months2 = floor(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months2 * 30 * 60 * 60 * 24)/ (60 * 60 * 24));
The answer I get is 11 years , 2 months and 14 days. Shouldn't it be 11 years, 3 months and 14 days?
I have tried quite a few different ways and I always end up with 2 months instead of 3. Does anyone know why?
Try using PHP's built-in date API instead of doing the math yourself.
Using DateTime, DateInterval and the DateTime::diff function:
$date1 = new DateTime("2000-01-01");
$date2 = new DateTime("2011-03-14");
$diff = $date2->diff($date1);
var_dump($diff);'
/* is prints:
object(DateInterval)#3 (8) {
["y"]=>
int(11)
["m"]=>
int(2)
["d"]=>
int(13)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["invert"]=>
int(1)
["days"]=>
int(4090)
}
*/
At least then you don't need to worry if you made an error (the result seems correct).
The answer that you are getting is completely right!

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