Results 1 to 4 of 4

Thread: Another date question (countdown)

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Another date question (countdown)

    Hello once again, sorry for posting so many questions but I both love php and this place

    I store dates in my db for example: 2007-04-23 19:30:01
    And I want it to show the time left until it hit zero (a countdown) but I cant figure out how to do this.

    The date is separated so its.
    Year = 2007
    Month = 04
    Day = 23
    and so on..

    I know I can get the current GMT with gmdate but i need some way to get the time between the time from the db and the current time.
    Im not looking for an actual script that countdown automatic I just want it to countdown once when I reload the page.
    Something like, 3 days and 10 hours left.
    Last edited by Dennis_Gull; 04-21-2007 at 08:15 PM.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Dennis_Gull View Post
    I store dates in my db for example: 2007-04-23 19:30:01
    And I want it to show the time left until it hit zero (a countdown) but I cant figure out how to do this.

    I know I can get the current GMT with gmdate but i need some way to get the time between the time from the db and the current time.
    Using the mktime (and gmmktime) function, one can convert a specific date and time into a timestamp. The time function returns a timestamp for the current date and time. Subtracting one from the other will yield an integer that indicates the interval (in seconds) between those two moments.

    If you haven't been directed there yet, have a look at the Date and Time reference which lists the time-related functions available in PHP.

    Mike

  3. #3
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply, my site was down for about 2 days so couldnt work with it.. anyways I have a function that use mktime and found out how to get it to work.. although I can only display "days left" yet but will probably find some good code if try to search some more, this is what I use now:

    Code:
    $target = sprintf(tep_date_left($product_info['products_date_available'])) ;
    $today = time () ;
    $difference =($target-$today) ;
    $days =(int) ($difference/86400) ;
    echo ' <font color="#605f5f">(' . $days . ' days left)</font>';
    PS:
    What does (int) mean?


    Edit:
    Made my own code that will get the the gmt time left in days and hours and minutes it looks like this atm: (hope I didnt do something unnecessary )
    Code:
     
    $target = sprintf(tep_date_left($product_info['products_date_available'])) ;
          $hour=gmdate('H');
          $min=gmdate('i');
          $sec=gmdate('s');
          $month=gmdate('n');
          $day=gmdate('d');
          $year=gmdate('Y');
          $stamp=mktime ($hour,$min,$sec,$month,$day,$year);
          $left=$target-$stamp;    
          $days_left=floor($left/(24 * 60 * 60));
          $hours_left=floor($left/(60 * 60 ));	
    	  
    	  $fix_hour=$days_left*86400;
    	  $hour=$left-$fix_hour;
    	  $hours_left=floor($hour/(60 * 60 ));
    	  
    	  $fix_minute=$fix_hour+$hours_left*3600;
    	  $minute=$left-$fix_minute;
    	  $minute_left=floor($minute/60);
    	  
    	  if ($days_left == 1) {
    	  $real_d = $days_left . ' day ';
    	  } else {
    	  $real_d = $days_left . ' days ';
    	  }
    	  if ($hours_left == 1) {
    	  $real_h = $hours_left . ' hour ';
    	  } else {
    	  $real_h = $hours_left . ' hours ';
    	  }
    	  if ($minute_left == 1) {
    	  $real_m = $minute_left . ' minute';
    	  } else {
    	  $real_m = $minute_left . ' minutes';
    	  }
    	    
    	  $time_left = $real_d . $real_h . $real_m;
    
       	echo ' <font color="#605f5f">(' . $time_left . ')</font>';
    Last edited by Dennis_Gull; 04-23-2007 at 02:03 PM.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Dennis_Gull View Post
    $target = sprintf(tep_date_left($product_info['products_date_available'])) ;
    I'm curious why you're using the sprintf function. Its purpose is to format a string with the addition of arguments that are inserted into the string. Calling the function with only one string argument would seem to be quite redundant. What exactly does tep_date_left return, and what's the value obtained from the $product_info associative array?

    $today = time () ;
    $difference =($target-$today) ;
    It's also somewhat odd to use sprintf if your intention is to perform arithmetic as the function returns a string, not a number.

    echo ' <font color="#605f5f">(' . $days . ' days left)</font>';[/code]
    Don't use the font element. Ever. If you must use an in-line element - if you can't style the containing element - use a span element:

    Code:
    .countdown {
        background: /* an appropriate background colour */;
        color: #605f5f;
    }
    HTML Code:
    <span class="countdown">(<?php echo $days; ?> days left)</span>
    What does (int) mean?
    It is a cast. It forces a variable to be treated as a particular type; an integer, in this case. The PHP manual briefly describes casting in the type juggling section. The effects of casting varies based on the cast, and the type being cast. For example, when casting to an integer, booleans will become one (1) or zero (0), and real (floating-point) numbers will be truncated (rounded down). The treatment of strings and the other remaining types are more complicated - see the manual for more information.

    $hour=gmdate('H');
    $min=gmdate('i');
    $sec=gmdate('s');
    $month=gmdate('n');
    $day=gmdate('d');
    $year=gmdate('Y');
    $stamp=mktime ($hour,$min,$sec,$month,$day,$year);
    This is unnecessary. Just obtain the timestamp from the time function. The timestamp is in local time, just as the one returned by mktime will be.

    If there ever is a time when you need to make several calls to obtain information about a particular moment in time, be sure to record that time first and act on that stored timestamp; though it's unlikely, it's possible for the system time to change between calls resulting in inaccurate data. In the example above, the gmdate function takes a timestamp as a second argument. However, even that is excessive: the getdate function will return an associative array containing information about the current local time, or some other specific moment.

    Here's an alternative version of your code. The argument, $target, is assumed to be an integer timestamp. The second, optional argument will be returned if the target time has been reached, or has already passed; this defaults to null. If less than sixty seconds remain, the return value is the string "less than a minute".
    PHP Code:
    define('SECONDS_PER_DAY'86400);
    define('SECONDS_PER_HOUR'3600);
    define('SECONDS_PER_MINUTE'60);

    function 
    constructCountdown($target$reached null) {
        
    $remainingTime $target time();
        if (
    $remainingTime <= 0) return $reached;
        else if (
    $remainingTime SECONDS_PER_MINUTE) return 'less than a minute';

        
    $remainingDays = (int) ($remainingTime SECONDS_PER_DAY);
        
    $temp $remainingTime - ($remainingDays SECONDS_PER_DAY);
        
    $remainingHours = (int) ($temp SECONDS_PER_HOUR);
        
    $temp -= $remainingHours SECONDS_PER_HOUR;
        
    $remainingMinutes = (int) ($temp SECONDS_PER_MINUTE);

        if (
    $remainingDays 0) {
            
    $result "$remainingDays day";
            if (
    $remainingDays != 1$result .= 's';
        } else {
            
    $result '';
        }
        if ((
    $remainingHours 0) || (strlen($result) > 0)) {
            if (
    strlen($result) > 0$result .= ', ';
            
    $result .= "$remainingHours hour";
            if (
    $remainingHours != 1$result .= 's';
        }
        if ((
    $remainingMinutes 0) || (strlen($result) > 0)) {
            if (
    strlen($result) > 0$result .= ', and ';
            
    $result .= "$remainingMinutes minute";
            if (
    $remainingMinutes != 1$result .= 's';
        }
        return 
    $result;

    Mike

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
  •