Jump to content
#StayAtHome ×

Archived

This topic is now archived and is closed to further replies.

techman41973

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

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      
 

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Hello,

 

I am also new in PHP development i have same issue thank you so much for sharing this post. now i got it.

Share this post


Link to post
Share on other sites

try

if ($months) {
    printf('%d month%s ', $months, $months>1?'s':'');
}
if ($weeks) {
    printf('%d week%s ', $weeks, $weeks>1?'s':'');
}
if ($days) {
    printf('%d day%s ', $days, $days>1?'s':'');
}

Share this post


Link to post
Share on other sites

Another related question, when the countdown reaches zero, how do I prevent it from showing a negatve number.

Once the countdown reaches zero, I want to show a blank line or some specific text. Thanks again!

Share this post


Link to post
Share on other sites

×
×
  • 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.