Jump to content

Recommended Posts

Im a complete nubin when it comes to php. Can some one point me as to what im doing wrong (probably alot..lol)

 

 

$percent =(echo date('Y-m-d H:i:s') - $beginProductionTime)/($endProductionTime - echo date('Y-m-d H:i:s')*100);

Untested but should get you close...

<?php

// three inputs
$start = '2009-01-01';
$end = '2008-01-01 12:00:48';
$percentage = .3;

// order them correctly
$start = strtotime( $start );
$end = strtotime( $end );
if( !$start || !$end ) {
  throw new Exception( 'Invalid dates' );
}
$min = min( $start, $end );
$max = max( $start, $end );

// $min is the begin date in seconds, $max is the end date in seconds
echo date( 'Y-m-d H:i:s', $min + ($percentage * ($max - $min)) );
?>

I'd like to say thank you all for your time so far and you are all brilliant.

 

roopurt18 I tried that code but not sure what is wrong.

 

I changed the date to the following

 

$start = '2009-05-29';
$end = '2009-06-01 12:00:48';

 

The output displays

 

2009-05-30 01:12:14 and not a % ??

 

 

My code takes two dates, calculates the number of seconds between them, multiplies by a percentage, and then adds that percentage to the first date.  Finally it outputs the result as a date.

 

Otherwise what you are asking for makes no sense mathematically so you'll need to be more clear.

I think what he means that if start is 2009-05-28, current is 2009-05-29 and end is 2009-05-30 then the result should be 50%.

 

I.e. something like this:

<?php
$start = '2009-05-28';
$end = '2009-05-30';

$start = strtotime($start);
$end = strtotime($end);
if (!$end || !$start) {
throw new Exception('Invalid dates.');
}
else if ($start > $end) {
throw new Exception('Start date is larger than end date.');
}

$diff = $end - $start;

$current = time();
$cdiff = $current - $start;

if ($cdiff > $diff) {
$percentage = 1.0;
}
else if ($current < $start) {
$percentage = 0.0;
}
else {
$percentage = $cdiff / $diff;
}

printf('%.2f%%', $percentage * 100);

OK I have the following code:-

 

$start = '2009-05-29 12:00:48';
$end = '2009-06-01 12:00:48';
$start = strtotime($start);
$end = strtotime($end);
if (!$end || !$start) {
throw new Exception('Invalid dates.');
}
else if ($start > $end) {
throw new Exception('Start date is larger than end date.');
}

$diff = $end - $start;

$current = time();
$cdiff = $current - $start;

if ($cdiff > $diff) {
$percentage = 1.0;
}
else if ($current < $start) {
$percentage = 0.0;
}
else {
$percentage = $cdiff / $diff;
}

 

and i have placed the following in the table to show the results.

 

printf('%.2f%%', $percentage * 100);

 

But i get the following output

 

0.12%5

Well, those two date inputs are all in the past. You need to explain what you are trying to do. Otherwise we cannot really help you. We have all been trying to guess what you are trying to do, but all the times you simply just come back and say you aren't getting the correct results. That's like walking in saying that you're trying to get the number 5. I can provide you millions of ways you can get the number 5, but unless you tell me how you plan on getting to that number I can't give you the solution you're looking for.

That's something you're outputting somewhere else.

 

Right now

<?php
$start = '2009-05-29 12:00:48';
$end = '2009-06-01 12:00:48';
$start = strtotime($start);
$end = strtotime($end);
if (!$end || !$start) {
   throw new Exception('Invalid dates.');
}
else if ($start > $end) {
   throw new Exception('Start date is larger than end date.');
}

$diff = $end - $start;

$current = time();
$cdiff = $current - $start;

if ($cdiff > $diff) {
   $percentage = 1.0;
}
else if ($current < $start) {
   $percentage = 0.0;
}
else {
   $percentage = $cdiff / $diff;
}

printf('%.2f%%', $percentage * 100);

 

outputs 1.88%, but that will of course change.

If I understand correctly and if you know where they currently are in the date range, you could use logic similar to:

 

a = start
b = end
c = current

percent = (c - a) / (b - a) * 100

 

this is what you want. All you have to do is convert your vars into timestamps and insert where appropriate.

 

edit: oops my bad. I didn't realize there was another page to this thead.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.