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
		
		
		
			
		
			$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;
		
		
		
		
		
		
		?>


Try this:


  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

.

First of all what is this total value?


$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.

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
$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

That means the $total is in minutes? Then try out the following until there is a more precise PHP built in function:


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);

Thanks!

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

You’re amazing! Thank you so much!

–thesprucegoose