Jump to content

Need help displaying time frame (months, weeks, days) between two dates in PHP


techman41973

Recommended Posts

I'm looking to develop PHP code for the following application, similar to a countdown timer.
  I want to compute the difference between the current date and the date of a future event (happens to be 1/7/2014).
  Easy to do. But I want to display the difference or time between the current date and this future event as follows
    ----  Event is x months and y weeks and z days from today
   I believe the best approach is to start with the datediff function, but it's the display output portion that gets tricky
   I would appreciate some help or direction. Thanks      
 

 

Link to comment
Share on other sites

Here's a function I had lying around:

 

//Function to determine a textual representation of the time between two events
function time_since ($startTimeStr, $allUnits=false, $endTimeStr=false)
{
    //Set start and end times in seconds
    $endTimeInt = (!$endTimeStr) ? time() : strtotime($endTimeStr);
    $startTimeInt = strtotime($startTimeStr);

    if($endTimeInt===false || $startTimeInt===false) { return false; }
    //Calculate the difference
    $timeDiff = $endTimeInt - $startTimeInt; //Get the difference in seconds
    $measurements = array (
        'year'   => 31536000,
        'month'  => 2592000,
        'week'   => 604800,
        'day'    => 86400,
        'hour'   => 3600,
        'minute' => 60,
        'second' => 1
    );

    $returnValues;
    foreach ($measurements as $label => $measurement)
    {
        if ($timeDiff >= $measurement)
        {
            $units = floor($timeDiff/$measurement);
            $plural = ($units>1) ? 's' : '';
            $returnString = $units . ' ' . $label . $plural;
            if(!$allUnits) { return $returnString; }
            $timeDiff -= $units * $measurement;
            $returnValues[] = $returnString;
        }
    }
    if(!count($returnValues)) { return false; }
    return implode(', ', $returnValues);
}

## USAGE
$postedDateTime = '2012-09-24 13:14:02';

echo 'This post was updated ' . time_since($postedDateTime) . ' ago';
// This post was updated 2 days ago

echo '<br><br>This post was updated ' . time_since($postedDateTime, true) . ' ago';
// This post was updated 2 days, 19 minutes, 33 seconds ago
Link to comment
Share on other sites

try

$d1 = new DateTime();  // now
$d2 = new DateTime('2014-07-01');

$diff = $d2->diff($d1);

list($y,$m,$d) = explode('-', $diff->format('%y-%m-%d'));

$months = $y*12 + $m;
$weeks = floor($d/7);
$days = $d%7;

printf('%d months %d weeks %d days', $months, $weeks, $days);    // 14 months 1 weeks 4 days

Edited by Barand
Link to comment
Share on other sites

Barand and Psycho, I greatly appreciate you both sharing your scripts.

I tried them both and I've decided on Barand's as its simpler and a bit more accurate for my application.

One more question, as I'm still developing my PHP skills.

  I'd like the printf statement  to display  "week" when weeks = 1 instead of "weeks" - for gramatacal correctness.

    the same also for months

  I'd also like the printf statement to not display the word weeks if weeks = 0

     same for months.

    I would greatly appeciate some direction on the syntax to do this.

   Thanks again to you boith.

 

 

 

 

 

 

try

$d1 = new DateTime();  // now
$d2 = new DateTime('2014-07-01');

$diff = $d2->diff($d1);

list($y,$m,$d) = explode('-', $diff->format('%y-%m-%d'));

$months = $y*12 + $m;
$weeks = floor($d/7);
$days = $d%7;

printf('%d months %d weeks %d days', $months, $weeks, $days);    // 14 months 1 weeks 4 days

 

Link to comment
Share on other sites

  • 4 months later...
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.