SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast csweb's Avatar
    Join Date
    Dec 2004
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Time Since (Timestamp) Script Problems

    I'm making a script so users can see how long it has been since something was submitted. To record the submission time I'm using a timestamp. And then this function I just made I'm using to get the days, hours, minutes and seconds since the submission.

    I put some code in to make what is returned formatted much better it turns it into a list with commas between each values and an "and" between the last two. It also emits any that are zero as you would not want it saying "0 Days, 0 Hours, 2 Minutes and 3 Seconds ago" I tested the script with the time stamp "1081127718" (some time back in march) and it returns "246 Days, 16 Hours, 9 Minutes and 27 Seconds." Perfect I thought but I then tested it with a more recent time stamp "1102443690" (from today) and I get this returned ".PHP Notice: Undefined index: 0 in time.php on line 48" The code I made is below any help would be greatly appreciated.

    PHP Code:
    <?
    function timesince($time)
    {
    $now time();
    $diff $now $time;
    $days floor($diff/86400);
    $diff $diff $days*86400;
    $hours floor($diff/3600);
    $diff $diff $hours*3600;
    $minutes floor($diff/60);
    $diff $diff $minutes*60;
    $seconds $diff;
    $timesince "";
    if (
    $days == "1" ) {
    $sections[0] = "1 Day";
    }
    if (
    $days "1" ) {
    $sections[0] = "$days Days";
    }
    if (
    $hours == "1" ) {
    $sections[1] = "1 Hour";
    }
    if (
    $hours "1" ) {
    $sections[1] = "$hours Hours";
    }
    if (
    $minutes == "1" ) {
    $sections[2] = "1 Minute";
    }
    if (
    $minutes "1" ) {
    $sections[2] = "$minutes Minutes";
    }
    if (
    $seconds == "1" ) {
    $sections[3] = "1 Second";
    }
    if (
    $seconds "1" ) {
    $sections[3] = "$seconds Seconds";
    }
    $size count($sections);
    $end $size 1;
    $andkey $size 2;
    $cycle "0";
    while (
    $size $cycle) {
    if (
    $cycle == $andkey) {
        
    $sections[$cycle] = $sections[$cycle] . " and ";
        
    $timesince $timesince $sections[$cycle];
    }
    else if (
    $cycle == $end) {
        
    $sections[$cycle] = $sections[$cycle] . ".";
        
    $timesince $timesince $sections[$cycle];
    }
    else {
        
    $sections[$cycle] = $sections[$cycle] . ", ";
        
    $timesince $timesince $sections[$cycle];
    }
    $cycle++;
    }
    return 
    $timesince;
    }
    $since timesince(1102440575);
    print 
    "$since";

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It'd help if you pointed out line 48.

    It seems that the value of $cycle isn't a valid spot in your array.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    SitePoint Enthusiast csweb's Avatar
    Join Date
    Dec 2004
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    else if ($cycle == $end) {
        
    $sections[$cycle] = $sections[$cycle] . ".";
        
    $timesince $timesince $sections[$cycle]; 
    Second line here is line 48
    Do you have any ideas why it is not a valid spot in the array? I Dont't see any reason why it should not work.

    You can use that to find numbers if you need to reffer to one for me, thanks Chris
    http://qbnz.com/highlighter/demo.php

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try debugging that part with foreach().
    Use foreach to spit out the entire arrays contents and see if the index being called ($cycle) actually exists in the array.
    PHP Code:
    foreach($array as $key => $value)
    {
         echo 
    $key " as " $value;

    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your tests, you check for ==1 and for > 1, but you never check for 0

    so, in your code
    PHP Code:
      if ($days == "1") {
      
    $sections[0] = "1 Day";
      }
      if (
    $days "1") {
      
    $section[0] = "$days Days";
      } 
    should be changed to
    PHP Code:
      if ( == $days)
      {
       
    $sections[0] = '1 day';
      }
      elseif (
    $days)
      {
        
    $sections[0] = $days ' Days';
      }
      else
      {
        
    $sections[0] = '';
      } 
    Same for the test for hours, minutes and seconds.
    You might want to write a little function to do the test.

  6. #6
    SitePoint Enthusiast csweb's Avatar
    Join Date
    Dec 2004
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I donít want to check for "0" thatís the point, as I donít want a value to be added for them as I donít want it to say 0 Days, 0 hours and 5 Seconds. I think I've fixed it. I was assigning the formatted text to a set key in the array when I actually wanted at the end Iím not using:
    PHP Code:
    array_push($sections"$days Days"); 
    So I've fixed the formatting bit with:

    PHP Code:
    $sections = array ();
    if (
    $days == "1" ) {
    array_push($sections,  "1 Day");
    }
    if (
    $days "1" ) {
    array_push($sections"$days Days");
    }
    if (
    $hours == "1" ) {
    array_push($sections"1 Hour");
    }
    if (
    $hours "1" ) {
    array_push($sections,  "$hours Hours");
    }
    if (
    $minutes == "1" ) {
    array_push($sections"1 Minute");
    }
    if (
    $minutes "1" ) {
    array_push($sections"$minutes Minutes");
    }
    if (
    $seconds == "1" ) {
    array_push($sections"1 Second");
    }
    if (
    $seconds "1" ) {
    array_push($sections"$seconds Seconds");


  7. #7
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes - definitley better to use array_push, or just []

    And here is a simpler (?) way to make a nice user readable string

    PHP Code:
       
       $sections
    [] = '4 days';
       
    $sections[] = '3 Hours';
       
    $sections[] = '27 seconds';
       
       
    // Join all the bits together with a comma
       
    $str implode(', '$sections);
       
       
    // Find last comma
       
    $pos strrpos($str',' );
       
       if (
    false !== $pos)
       {
           
    // replace last comma with the word ' and '
           
    $str substr_replace($str' and '$pos1);
       }
       
    //add final period
       
    $str .= '.';
       
       
    // display it
       
    echo 'String is ' $str '<br />'

    Hope you like this solution


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •