ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Make it Small Challenge 1 (Time Output)

August 12, 2008 3:05am

Subscribe [7]
  • #16 / Aug 12, 2008 7:18pm

    James Gifford

    77 posts

    Hi all.

    Here’s my solution. I haven’t checked it for character length or time, but its as compact as I can make it.

    function times ()
    {
        for ($i=0; $i<96; $i++)
            $a[] = date('h:iA', mktime(0, $i*15));
        
        return $a;
    }
  • #17 / Aug 12, 2008 8:25pm

    Lone

    350 posts

    Awesome solution there m4rw3r - the only downside is that it is timezone dependant so if you could alter to be not so dependant that would be great.

    Updated the list at the top as well.

  • #18 / Aug 12, 2008 9:47pm

    James Gifford

    77 posts

    Found a way to strip out a few more characters (I think). I’m not sure if the function name counts as well so I shortened it too (seems like only the function contents should count to me).

    function t ()
    {
        while ($i < 96) 
            $a[] = date('h:iA', mktime(0, $i++ * 15));    
        return $a;
    }
  • #19 / Aug 12, 2008 11:42pm

    wiredesignz

    2882 posts

    I liked m4rw3r’s solution so much he should get credit for this. 😉

    public function t($s = 43200, $f = 129600, $i = 900) //start, finish, increment
    {
        while($s < $f)
        {
            $t[] = date('h:iA', $s);
            $s+=$i;
        }
    
        return $t;
    }

    EDIT: Choose your own start, finish and increment times in seconds

  • #20 / Aug 13, 2008 12:36am

    Lone

    350 posts

    Found a way to strip out a few more characters (I think). I’m not sure if the function name counts as well so I shortened it too (seems like only the function contents should count to me).

    Yup, only function contents count and it excludes variable names as well except for the ‘$’

  • #21 / Aug 13, 2008 11:22am

    m4rw3r

    647 posts

    Here is an update:

    function time_list()
    {
        for($i = 0; $i < 86401; $i += 900)
            $t[] = gmdate('h:iA');
        return $t;
    }

    It is 42 chars, except for the header, { + }, spacing and the return $t;

    AND, it is not timezone dependent! 😊

  • #22 / Aug 13, 2008 11:36am

    xwero

    4145 posts

    fastest and smallest code

    function m4rw3r(){ /* do magic */ }
  • #23 / Aug 13, 2008 11:39am

    Tom Glover

    493 posts

    fastest and smallest code

    function m4rw3r(){ /* do magic */ }

    You do need to tell him what to do, he can’t read your mind! Or can he?

  • #24 / Aug 13, 2008 11:47am

    Seppo

    483 posts

    My solution:

    function times_array()
    {
        $today = strtotime('today');
        for ($a = -1; ++$a <= 96;)
        {
            $time = date('h:iA', $today + $a * 900);
        }
    }

    But I prefer the James Gifford solution

  • #25 / Aug 13, 2008 11:55am

    m4rw3r

    647 posts

    HAHAHA!!!  😊

    Good one, xwero!

    @topic
    The performance loss of using mktime() and date() (and gmdate()) is quite big, compared to using pure string manipulations.

    The mktime() function is totally unnecessary, because it fetches the date and/or time of today if the complete date is not explicitly set.

    I think the reason date() is so slow is that it has got a lot of configuration options (the table in PHP Cookbook is about 2.5 pages long, I recall).

  • #26 / Aug 13, 2008 12:11pm

    xwero

    4145 posts

    I think the reason date() is so slow is that it has got a lot of configuration options (the table in PHP Cookbook is about 2.5 pages long, I recall).

    Then would the gmdate function be slow too because it has the same configuration options.

  • #27 / Aug 13, 2008 12:35pm

    m4rw3r

    647 posts

    using mktime() and date() (and gmdate()) is

    I did…

    But to be clear, it also applies to gmdate().

  • #28 / Aug 13, 2008 1:25pm

    Lone

    350 posts

    You do need to tell him what to do, he can’t read your mind! Or can he?

    Here’s how to tell him what to do:

    function m4rw3r($myThoughts) { /* do magic */ }


    Love the outcomes of this challenge and will start to brew up a new one in the coming days - if you have any suggestions please feel free to pm me.

  • #29 / Aug 13, 2008 2:12pm

    zdknudsen

    305 posts

    Here is an update:

    function time_list()
    {
        for($i = 0; $i < 86401; $i += 900)
            $t[] = gmdate('h:iA');
        return $t;
    }

    It is 42 chars, except for the header, { + }, spacing and the return $t;

    AND, it is not timezone dependent! 😊

    You forgot to pass the $i timestamp to gmdate().

    What an great initiative, Lone. Too bad I missed the race (sorry, can’t possibly top these), but I’m looking forward to the next challenge.

  • #30 / Aug 13, 2008 2:38pm

    m4rw3r

    647 posts

    OOps, sorry. I wrote it at work, where I don’t have access to a php server.
    The char count should be 44 (skipped variable names (only counted $), spaces, newlines, function header, {, } and “return $t” (because all functions need it 😛))

    function time_list()
    {
        for($i = 0; $i < 86401; $i += 900)
            $t[] = gmdate('h:iA', $i);
        return $t;
    }

    EDIT: My try at making a faster function failed, because I didn’t followed the specs, so the one above is the fastest I can make.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases