SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Date Counter

    Hello all,

    I'm attempting to make a PHP date counter for days, hours, and minutes. I have it somewhat working, but it doesn't want to add the hours into days for some reason.

    While I continue to work on this, would someone be able to look at it for me and see if there's any obvious solution that I'm missing?

    Thanks!

    --thesprucegoose

    PHP Code:
    <?php
            
            
            
                
            
                $valueOne 
    45;
                
    $valueTwo 115;
                
    $valueThree 12;
            
                
    $total $valueOne $valueTwo $valueThree;
            
            
                
    $wiiMinutes $total;
                
    $wiiHours 0;
                
    $wiiDays 0;
                
                
                for (
    $i=0$i <= $total$i++) {
                

                
                if (
    $wiiMinutes == 60) {
                
                    
    $wiiMinutes 0;
                    
    $wiiHours $wiiHours 1;
                
                }
                
                if (
    $wiiMinutes 60) {
                
                    
    $wiiMinutes $wiiMinutes 60;
                    
    $wiiHours $wiiHours 1;
                    
                
                }
                
                
                
                if (
    $wiiHours == 24) {
                
                    
    $wiiHours 0;
                    
    $wiiDays $wiiDays 1;
                
                } else if (
    $wiiHours 24) {
                
                    
    $wiiHours $wiiHours 24;
                    
    $wiiDays $wiiDays 1;
                
                }
                
                
                
                
                if (
    $wiiMinutes 10) {
                
                    
    $wiiMinutes "0" $wiiMinutes;
                }
                
                if (
    $wiiHours 10) {
                
                    
    $wiiHours "0" $wiiHours;
                }
                
                if (
    $wiiDays 10) {
                
                    
    $wiiDays "0" $wiiDays;
                }
            
            }
        
                echo 
    $wiiDays ":" $wiiHours ":" $wiiMinutes;
            
            
            
            
            
            
            
    ?>

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,840
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:
      define('jj''<br />');

      
    $total 4511512;

      echo 
    jj'Days: '  intval$total/ (24*60*60) ); 
      echo 
    jj'Hours: ' intval($total/ (60*60)) &#37; 24; 
      
    echo jj'Mins: '  $total  60

     
    // Result :
    //   Days: 52
    //   Hours: 5
    //   Mins: 52 

    .
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all what is this total value?
    PHP Code:
    $valueOne 45
    $valueTwo 115
    $valueThree 12;
    $total $valueOne $valueTwo $valueThree
    That means $total=4511512? Is it the total number of seconds or something else? Where do the values come from? And can you tell us the purpose (goal)? If that is the total number of seconds then try what John proposed.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey all,

    I guess I was working too late last night...heh.

    Those . should be + signs.

    The goal is to keep a running counter of how long I exercise each day. I created a custom field in Wordpress called "exercise-time" where I would enter the amount of minutes I exercised for the day.

    I'm using a Wordpress loop to dump all of the custom fields called "exercise-time" into a single number then to take that number and arrange it accordingly (days, hours, minutes).

    So if I have three entries with times of 60, 63, and 10:
    60 + 63 + 10 = 133

    Then the script would convert that to:
    00:02:13

    Here's the Wordpress loop:
    PHP Code:
    <?php
    $total 
    0;
    while (
    have_posts()) : the_post();
        
    $postmeta get_post_custom($post->ID);
        echo 
    "Apples = {$postmeta['apples'][0]}";
        
    $total += (int) $postmeta['apples'][0];
    endwhile;
    echo 
    "Total Apples: $total";
    ?>

    I'll give those scripts a try in a bit (once I get to work) and get back to everyone.

    Thanks so much guys!

    --thesprucegoose

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That means the $total is in minutes? Then try out the following until there is a more precise PHP built in function:
    PHP Code:
    function formatTime($total){
        
    $days $hours $minutes 0;
        
    // 1440 = number minutes in a day.
        
    if($total 1440){
             
    $days intval($total 1440);
             
    $total $total &#37; 1440;
        
    }
        if(
    $total 60){
            
    $hours intval($total 60);
            
    $minutes $total 60;
        }
        return 
    "$days:$hours:$minutes";
    }
    $total 60 63 10;
    echo 
    formatTime($total); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I got an error initially, but replaced the &#37; with % and it worked like a charm!

    You're amazing! Thank you so much!

    --thesprucegoose


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
  •