techman41973 Posted April 18, 2013 Share Posted April 18, 2013 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 https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/ Share on other sites More sharing options...
Psycho Posted April 18, 2013 Share Posted April 18, 2013 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 https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1425732 Share on other sites More sharing options...
Barand Posted April 18, 2013 Share Posted April 18, 2013 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 https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1425735 Share on other sites More sharing options...
techman41973 Posted April 25, 2013 Author Share Posted April 25, 2013 On 4/18/2013 at 11:47 PM, Barand said: 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 https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1426485 Share on other sites More sharing options...
alandavid Posted April 27, 2013 Share Posted April 27, 2013 Hello, I am also new in PHP development i have same issue thank you so much for sharing this post. now i got it. Link to comment https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1426826 Share on other sites More sharing options...
Barand Posted April 27, 2013 Share Posted April 27, 2013 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':''); } Link to comment https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1426837 Share on other sites More sharing options...
techman41973 Posted September 22, 2013 Author Share Posted September 22, 2013 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! Link to comment https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1450642 Share on other sites More sharing options...
Barand Posted September 22, 2013 Share Posted September 22, 2013 DateTime::diff() has an optional 2nd argument which enables you to detect negative differences http://php.net/manual/en/datetime.diff.php Link to comment https://forums.phpfreaks.com/topic/277127-need-help-displaying-time-frame-months-weeks-days-between-two-dates-in-php/#findComment-1450643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.