Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter's timespan: curious code
#1

[eluser]Denzel[/eluser]
I was roaming about the CodeIgniter core just to familiarize myself and I came across some code in date_helper.php, specifically in the timespan function, that I just cannot wrap my head around:
Code:
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
  $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
}

$seconds -= $months * 2628000;
}
That's just one piece of it. Why are there two if statements? It can be condensed into one, albeit senseless, statement:
Code:
if ($years > 0 OR $months > 0 AND $months > 0)
In its current state, I don't even know why $years is involved, unless the author was going for a short-circuit statement in which case this would be the best option:
Code:
if ($years > 0 AND $months > 0)
Can anyone shed some light on this situation? I've looked it over a few times, maybe I'm missing something.
#2

[eluser]InsiteFX[/eluser]
It's for like blog posts etc where it shows the time since the post was created

So if you created a post say 1 hour ago then thats what this function will display.

They call it on most other sites time ago.
#3

[eluser]Denzel[/eluser]
[quote author="InsiteFX" date="1335398631"]It's for like blog posts etc where it shows the time since the post was created

So if you created a post say 1 hour ago then thats what this function will display.

They call it on most other sites time ago.
[/quote]
I appreciate the response InsiteFX, but sadly you missed the point. I know what the function is for and its uses. Maybe I didn't make that clear. What I'm wondering is why the function is implemented in such an apparently inefficient way.

I'm probably in the minority, but I am calling this function a lot for my current website, so I decided to implement the functionality myself and came up with a method that is 45% faster. Of course it may be partially specific to my problem.

Furthermore, the language file is loaded every single time this function is called. Why not cache it somewhere? That reduced the time spent in this a lot for me, in fact, that was one of the main hotspots.
#4

[eluser]Aken[/eluser]
The function could use some improvement. You're welcomed to make a pull request on Github, and others will happily provide their own feedback. Smile
#5

[eluser]InsiteFX[/eluser]
To be honest I never use it, like you have my own that I wrote.

Sorry for the mis-understanding and as Aken mentioned I am sure that it would need improvement.
#6

[eluser]Denzel[/eluser]
Fair enough, I guess I'll sign up for github. Thanks guys I appreciate the feedback Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB