I want to compare two date with current date in if condition - php

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">Category:<?php echo $fetchyoutubedetails['course_title'];?></h3><!-- /.category-name -->
<h2 class="entry-title"><?php echo $fetchyoutubedetails['video_title'];?></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';

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

Related

Extract the correct month from timestamp with PHP

got stuck in extracting the correct month from timestamp...Here's my Code
$sql = "SELECT id,notice,description,DAY( `posted_date` ) AS DAY, MONTH(`posted_date` ) AS MONTH, YEAR( `posted_date`) AS YEAR, enable FROM n_notice";
$result=mysqli_query($con,$sql);
while ($run = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$notice = $run['notice'];
$description = $run['description'];
$day =$run['DAY'];
//$month=$run['MONTH'];
$month=date("M",$run['MONTH']);
$year=$run['YEAR'];
?>
<div class="media">
<div class="media-left media-middle">
<div id="date">
<div class="mon"><?php echo $month;?></div>
<div class="day"><?php echo $day;?></div>
<div class="year"><?php echo $year;?></div>
</div>
</div>
<div class="media-body">
<h5 class="media-heading"><?php echo $notice; ?></h5>
<?php echo $description; ?>
</div>
</div>
<?php
}
// Free result set
mysqli_free_result($result);
?>
</div>
DB timestamp column
How to get the correct month in "M" format? All the Months are shown as "Jan"!!!
$month=date("M",$run['MONTH']);
Try to change "M" to "F".
F - A full textual representation of a month (January through December)

How to put a Div inside an if statement

Hi i have a code that displays title, date and location. I want it to put on a div, unfortunately there is some wrong in my code and it loops my div.
I just want all may data to be inside "may-container" i just don't know how to create this container.
Hope you could help me with this. Thanks
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
Here is my code
$counter = 0;
while ( $startdate <= $enddate) {
if ( date("F",strtotime($result['date'])) == "May" && date("m, Y",strtotime($result['date'])) >= date("m, Y") )
{
if ($counter < 1)
{
echo "<div class='May-container'>";
$counter++;
}
echo "<div class= 'May'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
}
$startdate = strtotime("+120 day", $startdate);
}
if your code date("F",strtotime($result['date'])) produces a month in words then why do you have to put if and else?
why not:
while ( $startdate <= $enddate) {
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
EDIT: to answer your comment, you can try this code:
This code is only applicable if your Data is sorted out by date
$last_month = '';
$is_first = true;
while ( $startdate <= $enddate) {
if($last_month != date('F',strtotime($result['date']))){
echo '</div>';
$is_first = true;
}
if($is_first){
$last_month = date('F',strtotime($result['date']));
echo "<div class='". strtolower($last_month) . "-container'>";
$is_first = false;
}
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
if the code above code runs as what i expected(sorry i didnt run it for i dont have enough time to) it will yields something like:
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
<div class="june-container">
<div class="June">
title
date
location
</div>
<div class="June">
title
date
location
</div>
</div>

Timer Countdown (Confused)

Basically, I am trying to make a timer or countdown in PHP. This will take the time saved in the threads (date of post) and then show how long ago that is in comparison to the current time.
Anybody know how to do this?
Code:
<?php foreach($this->updated as $thread){ ?>
<section>
<div class="body">
<?php if($this->threads[$thread]['image']!='') { ?>
<div>
<a href="<?php echo $this->threads[$thread]['image']; ?>">
<img src="<?php echo $this->threads[$thread]['image']; ?>">
</a>
</div>
<?php } ?>
<?php echo $this->threads[$thread]['post']; ?>
</div>
<div class="small text-right">
<?php echo count($this->threads[$thread]['posts']); ?> Replies
|
<?php echo date("Y-m-d H:i:s", $this->threads[$thread]['time']); ?> EST
|
View
</div>
</section>
<?php } ?>
I don't want it to be an exact timer, more like it would say for example "less than a minute ago", "1 minute ago", "2 minutes ago", etc...
I have tried to make 2 different variables and then did $current->diff($date); Then just echo $diff->h for example for the hour. But I was confused by the matter.
You can use DateTime::diff function:
<?php
date_default_timezone_set('Europe/Moscow');
$timestamp = 1421253112;
$date = new DateTime;
$date->setTimestamp($timestamp);
$now = new DateTime;
$interval = $now->diff($date);
echo $interval->format('%i minutes ago');
Output:
7 minutes ago

Jquery and calendar next and previous month buttons

I am using wordpress, a plugin called The Events Calendar (great plugin btw), and I'm trying to create a little widget with a small navigational element in it. The navigation would show the previous month, the current month, and the next month. For example, today the navigation would show "Apr May Jun".
What should happen is when you click "Jun" it should load the events that occur in June. I want to load these via jquery/ajax magic into a specified div.
I kinda have it working, but i have a bug and I'm not sure how to fix it. When i click one of the months that is displayed initial, it works. But then when i click another month, I'm losing my jquery function (probably due to the page not reloading completely.)
So, I'm wondering if there is a way to re-initialize the navigation so i can get more clicks.
On with the code:
My sidebar.php file contains the following code:
Some HTML
<div id="upcoming-events-box">
<div class="upcoming-events-block-inner">
<div class="upcoming-events-content">
<div id="EventLoader">
<?php get_template_part( 'content', 'the-events' ); ?>
</div>
</div>
</div>
</div>
And some Jquery:
<script type="text/javascript">
jQuery(function() {
jQuery('#upcoming-events-months li a').click(function() {
var toLoad = jQuery(this).attr('href')+' #EventLoader';
jQuery('#EventLoader').hide('fast',loadContent);
jQuery('#load').remove();
jQuery('.upcoming-events-content').append('<span id="load">LOADING...</span>');
jQuery('#load').fadeIn('normal');
function loadContent() {
jQuery('#EventLoader').load(toLoad,'',showNewContent);
}
function showNewContent() {
jQuery('#EventLoader').show('normal',hideLoader);
}
function hideLoader() {
jQuery('#load').fadeOut('normal');
}
return false;
});
});
</script>
content-the-events.php which is called into the sidebar contains the following code:
<div id="upcoming-events-months">
// Some php
global $post;
$current_page_URL = $_SERVER["SERVER_NAME"];
if (isset($_GET['setmonth'])) {
$current_month = $_GET['setmonth'];
} else {
$current_month = date('M'); // This gets the current month
}
$next_month = date('M', strtotime('+1 month', strtotime($current_month)));
$prev_month = date('M', strtotime('-1 month', strtotime($current_month)));
echo "<ul>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$prev_month."'
id='".$prev_month."'>".$prev_month."</a></li>";
echo "<li>".$current_month."</li>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$next_month."'
id='".$next_month."'>".$next_month."</a></li>";
echo "</ul>";
</div>
<div id="content"></div>
<div class="upcoming-event-scroller">
<?php
$all_events = tribe_get_events(array(
'eventDisplay'=>'all',
'posts_per_page'=>-1,
'start_date'=>'01 '.$current_month.' 2012',
'end_date'=>'31 '.$current_month.' 2012'
));
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="row <?php echo (++$j % 2 == 0) ? 'even' : 'odd'; ?>">
<p><?php the_title(); ?></p>
<p class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'M j, Y'); ?> - <?php echo tribe_get_end_date($post->ID, true, 'M j, Y'); ?></p>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
</div>
Maybe I've just gotten myself really confused trying to figure this out. haha. Any help would be greatly appreciated.

Coding Wordpress/PHP Problem

Im having a bit of a problem with wordpress. On my site there is a bit that displays voucher now what i want is when that voucher expires i want the voucher details div not too show.
Here is the code from single.php
<?php if(have_posts()) : while (have_posts()) : the_post();
$image = get_post_meta($post->ID, 'image',true);
$writer = get_post_meta($post->ID, 'writer',true);
$code = get_post_meta($post->ID,'vcode',true);
$store = get_post_meta($post->ID,'store',true);
$expiry = strtotime(get_post_meta($post->ID,'expiry',true));
$desc = get_post_meta($post->ID,'description',true);
$datetoday = strtotime(date('Y/m/d'));
?>
this brings all the data from custom fields
heres the voucher div
<?php if($expiry > $datetoday){?>
<div class="voucherDetails">
<h2>Voucher Details</h2>
<div class="left">
<ul>
<li>Code: <?php echo $code;?></li>
<li>Expires: <?php echo $expiry;?></li>
<li><a class="more-link" href="<?php echo $store;?>" title="Visit Store">Visit Store!</a></li>
</ul>
</div>
<div class="desc right">
<h4>Description</h4>
<p><?php echo $desc;?></p>
</div>
</div>
<?php }?>
Now when i run this with the expiry with any value the voucher doesn't show The value in store is DD/MM/YYYY
Can't work it out, all help appreciated,
Thanks
Joe
Change
$datetoday = strtotime(date('d/m/Y'));
to be
$datetoday = strtotime(date('Y/m/d'));
Try run the following as an example of how it differs:
$datetoday = strtotime(date('d/m/Y'));
var_dump($datetoday);
var_dump(date("F j, Y, g:i a", $datetoday));
$datetoday = strtotime(date('Y/m/d'));
var_dump($datetoday);
var_dump(date("F j, Y, g:i a", $datetoday));
Edit: Alternatively use strtotime("now") if you want the precise timestamp to the very minute

Categories