0

In my data base I have start_date and end_date for my course pack and I want to show my div in between course of start date and date end date.

In my current course start date and end date have,

start_date = 2019-08-01
end_date   = 2019-09-01 

My code:

<?php
        date_default_timezone_set('Asia/Kolkata');
        $date = date('Y-m-d', time());

foreach ($fetchyotubedetail as $fetchyoutubedetails) :
        $currentData = date_create($fetchyoutubedetails['start_date']);
        $expiryDate1   = date_create($fetchyoutubedetails['end_date']);

        if ($date <= $currentData && $date <=$expiryDate1){
          echo 'hi';
        }else{ 
          ?>
          <?php
          ?>

  <div class="col-lg-4 col-md-4 col-sm-6 " style="position: relative;">
   <iframe id="<?php echo $fetchyoutubedetails['id'];?>"  class="yt_players" width="972" height="284" src="<?php echo $fetchyoutubedetails['video_links']?>?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture">

   </style> allowfullscreen></iframe>
              <h3 class="category-name"><a href="#">Category:<?php echo $fetchyoutubedetails['course_title'];?></a></h3><!-- /.category-name -->
   <h2 class="entry-title"><a href="<?php echo base_url();?>MainController/detail?id=<?php echo $fetchyoutubedetails['Category']?>" target="_blank"><?php echo $fetchyoutubedetails['video_title'];?></a></h2>
  <span><i class="fa fa-clock-o"></i> <time datetime="PT5M">Cource Duration : <?php echo $fetchyoutubedetails['cource_duration'] ?> Month</time></span>
</div>

     <?php
        }
        ?>
<?php endforeach; ?>

I do not know where I am wrong in my if ($date <= $currentData && $date <=$expiryDate1) condition.

If my course pack date is in between start and end date then only show my course div other wise show only echo 'hi';

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

1

The main problem is, you compare apples and oranges here...

date returns a string, formatted according to the given format

date_create on the other hand is an alias for DateTime::construct() which returns a DateTime Object.

So in your specific case the following should work:

$objDateNow = new DateTime();
$objDateNow->setTimezone(new DateTimeZone('Asia/Kolkata'));

foreach($fetchyotubedetail as $fetchyoutubedetails)
{
    $objDateStart = DateTime::createFromFormat('Y-m-d', $fetchyoutubedetails['start_date']);
    $objDateStart->setTimezone(new DateTimeZone('Asia/Kolkata'));
    
    $objDateEnd = DateTime::createFromFormat('Y-m-d', $fetchyoutubedetails['end_date']);
    $objDateEnd->setTimezone(new DateTimeZone('Asia/Kolkata'));
    
    if ($objDateNow >= $objDateStart && $objDateNow <= $objDateEnd)
    {
          echo 'hi';
    }
    else
    {
        //...
    }
    
}

For more information take a look at the PHP official docs

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/de/function.date-create.php

Community
  • 1
  • 1
Atural
  • 5,389
  • 5
  • 18
  • 35