0

Need to modify PHP code for date countdown (want to show nothing when count reaches 0)

I'm using the following code to show a text visual countdown to a specific date, broken down into months, weeks, days. Works great. However, when the countdown reaches the actual event date and then exceeds the event date it still shows the absolute difference. I want the code instead display nothing at this point (no output to screen). I would appreciate some guidance on how to modify this code to perform as explained.

     $d1 = new DateTime();  // now
     $d2 = new DateTime('2014-01-08');  // set the date +1 to compensate for 1-day  
      error in script
     $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('Countdown To Event - ');
     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':'');}
user2398188
  • 1,417
  • 3
  • 12
  • 12

3 Answers3

1

You need to check if the difference is positive or negative. Since the diff method returns a DateInterval, you can check the invert property.

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

 $diff = $d2->diff($d1);
 if ($diff->invert == 1) // the countdown is running
 {
     list($y,$m,$d) = explode('-', $diff->format('%y-%m-%d'));
     $months = $y*12 + $m;
     $weeks = floor($d/7);
     $days = $d%7;
     printf('Countdown To Event - ');
     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':'');}
 }
 else 
 {
     // The countdown finished, do something!
 }
George Marques
  • 820
  • 7
  • 21
1

There are plenty of ways to do it...

I think the easier one is:

$FinalDate='2013-09-23';
$d1 = new DateTime();  // now     
$d2 = new DateTime($FinalDate);  // set the date +1 to compensate for 1-day  

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

if(strtotime($FinalDate)>time()){
 printf('Countdown To Event - ');
 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':'');}
 }else{
    echo "Actions AFTER the date";
 }

However that piece of code looks really bad, you could do it much better. You can do it with javascript, and put a nice seconds counter too!

Diego Sucaria
  • 314
  • 2
  • 12
0

Try this:

<?php

$d1 = new DateTime();  // now
$d2 = new DateTime('2014-01-08');  // set the date +1 to compensate for 1-day  
$diff = $d2->diff($d1);

list($y,$m,$d) = explode('-', $diff->format('%y-%m-%d'));
if ($d1 < $d2) {
    $months = $y*12 + $m;
    $weeks = floor($d/7);
    $days = $d%7;
    printf('Countdown To Event - ');
    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':'');}
}

So if the current date is bigger than the countdown date then simply don't do anythin.

Vincent
  • 555
  • 10
  • 22