Solved

ebay style countdown php function

Posted on 2009-07-09
3
358 Views
Last Modified: 2013-12-13
Hi there, i am looking for a prebuilt php function that displays the time left or time since a mysql time that is passed to it. In terms of days, hours and mins only.

for example a time 2 weeks 4 hours and 32 mins in the future would output:

14d 4h 32m

and so on... This should be pretty simple but im restrained for time so any prebuilt functions provided would be great!




that would be great thanks!
0
Comment
Question by:cookiejest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
BrianMM earned 250 total points
ID: 24812660
Try this:
$diff = Timesince(strtotime($MySQL));
 
<?php
function Timesince($original) {  
     // array of time period chunks  
     $chunks = array(  
     array(60 * 60 * 24 * 365 , 'year'),  
     array(60 * 60 * 24 * 30 , 'month'),  
     array(60 * 60 * 24 * 7, 'week'),  
     array(60 * 60 * 24 , 'day'),  
     array(60 * 60 , 'hour'),  
     array(60 , 'min'),  
     array(1 , 'sec'),  
     );  
   
     $today = time(); /* Current unix time  */  
     $since = $today - $original;  
   
     // $j saves performing the count function each time around the loop  
     for ($i = 0, $j = count($chunks); $i < $j; $i++) {  
   
     $seconds = $chunks[$i][0];  
     $name = $chunks[$i][1];  
   
     // finding the biggest chunk (if the chunk fits, break)  
     if (($count = floor($since / $seconds)) != 0) {  
         break;  
     }  
     }  
   
     $print = ($count == 1) ? '1 '.$name : "$count {$name}s";  
   
     if ($i + 1 < $j) {  
     // now getting the second item  
     $seconds2 = $chunks[$i + 1][0];  
     $name2 = $chunks[$i + 1][1];  
   
     // add second item if its greater than 0  
     if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {  
         $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";  
     }  
     }  
     return $print;  
 }  
?>

Open in new window

0
 
LVL 9

Assisted Solution

by:xBellox
xBellox earned 250 total points
ID: 24812720
If you want in days, hours, minutes and secons try this (you can erase the seconds part if you want):

<?
     function timeBetween($start_date,$end_date)  
     {  
         $diff = $end_date-$start_date;  
         $seconds = 0;  
         $hours   = 0;  
         $minutes = 0;  
   
         if($diff % 86400 <= 0){$days = $diff / 86400;}  // 86,400 seconds in a day  
         if($diff % 86400 > 0)  
         {  
             $rest = ($diff % 86400);  
             $days = ($diff - $rest) / 86400;  
             if($rest % 3600 > 0)  
             {  
                 $rest1 = ($rest % 3600);  
                 $hours = ($rest - $rest1) / 3600;  
                 if($rest1 % 60 > 0)  
                 {  
                     $rest2 = ($rest1 % 60);  
                 $minutes = ($rest1 - $rest2) / 60;  
                 $seconds = $rest2;  
                 }  
                 else{$minutes = $rest1 / 60;}  
             }  
             else{$hours = $rest / 3600;}  
         }  
   
         if($days > 0){$days = $days.' days, ';}  
         else{$days = false;}  
         if($hours > 0){$hours = $hours.' hours, ';}  
         else{$hours = false;}  
         if($minutes > 0){$minutes = $minutes.' minutes, ';}  
         else{$minutes = false;}  
         $seconds = $seconds.' seconds'; // always be at least one second  
   
         return $days.''.$hours.''.$minutes.''.$seconds;  
     }  
 
	 
	 $date_now = date("U");
	 $date_future = date("U", strtotime("07/10/2009 09:30:00"));
	 
	 echo timeBetween($date_now, $date_future).'';
	 
?>

Open in new window

0
 

Author Closing Comment

by:cookiejest
ID: 31601536
thanks both solutions were good!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Link failure 16 70
warning code: class php incomplete message 6 38
Survey branching tutorial 11 45
Make an array show the subkey and put it in a query 2 29
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question