Coding Wordpress/PHP Problem - php

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

Related

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

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

Group/list/sort custom post type posts by date in tabs from acf datepicker field

im working with foundation tabs and i want to sort my custom posts by YEAR from datepicker field. Each tab is a year (2016 2015 etc). When users click on the tab and displays is, all the custom posts that had that event-date meta field (acf datepicker field) will then display and be listet in that tabs content. But now with my code it lists every posts in its own tab wich results in multiple tabs with the same year like this (2015-2015-2015-2016-2016) Instead of (2016-2015-2014-2013) with posts lited in tab content. This example here is what i am trying to achieve: exactly what i am after
I am not sure where i am at right now but i have done a lot and nothing does what its supposed to. Never done something like this before so i am probably a little ou of my leage here but i would like to know how this could be done and what i need to change in my code to get it done. Here is my code so far:
$currentTime = current_time('Ymd'); // getting current time
$format_in = 'd/m/Y'; // the format your value is saved in (set in the field options)
$format_out_fields = 'Y'; // the format you want to end up with
$date = DateTime::createFromFormat($format_in, get_field('event_date'));
$args = array(
'post_type' => 'cpt',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'acf_field',
'order' => 'DEC',
);
$subsQuery = new WP_Query( $args );
?>
<?php if ( $subsQuery->have_posts() ) : ?>
<?php section_open(); ?>
<h2 class="type2"><?php _e('H1 tabs', 'theme'); ?></h2>
<?php innerSection_open(); ?>
<?php ///////// TABS TITLE / BUTTONS ///////////// ?>
<ul class="tabs" data-tabs id="tabs">
<?php
$counter = 0;
while ( $subsQuery->have_posts() ) : $subsQuery->the_post();
++$counter;
$format_in = 'd/m/Y'; // the format your value is saved in (set in the field options)
$format_out = __('jS F Y', 'theme'); // the format you want to end up with
$date = DateTime::createFromFormat($format_in, get_field('acf_field'));
?>
<li class="tabs-title <?php if ( $counter == 1 ) { echo "is-active"; } ?>">
<a href="#panel-<?php the_ID(); ?>" <?php if ( $counter == 1 ) { echo 'aria-selected="true"'; } ?>>
<?php
$format_in = 'd/m/Y'; // the format your value is saved in (set in the field options)
$format_out_tabs = 'Y'; // the format you want to end up with
$date = DateTime::createFromFormat($format_in, get_field('acf_field'));
echo $date->format( $format_out_tabs );
?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php ///////// TABS CONTENT ///////////// ?>
<div class="tabs-content" data-tabs-content="tabs">
<?php
$counter = 0;
while ( $subsQuery->have_posts() ) : $subsQuery->the_post();
++$counter;
?>
<?php
$format_in = 'd/m/Y'; // the format your value is saved in (set in the field options)
$format_out = __('j.m.Y', 'theme'); // the format you want to end up with
$format_out_year = __('Y', 'theme'); // the format you want to end up with
$date = DateTime::createFromFormat($format_in, get_field('event_date'));
$date_year = DateTime::createFromFormat($format_in, get_field('event_date'));
?>
<div class="tabs-panel <?php if ( $counter == 1 ) { echo "is-active"; } ?>" id="panel-<?php the_ID(); ?>">
<span><?php echo $date->format( $format_out ); ?>:</span><br/>
<span class="bold">
<?php if( get_field( 'acf_type' ) == "event") { ?>
<?php _e('Event', 'theme'); ?>:
<?php } ?>
<?php
$theTitle = get_the_title();
echo $theTitle;
?>
</span><br/>
<?php if( get_field( 'link' )) { ?>
<?php if (isexternal(get_field('link')) == true ) { ?>
<?php _e('Read more', 'theme'); ?> <i class="fa fa-external-link" aria-hidden="true"></i>
<?php } ?>
<?php } ?>
</div>
<?php endwhile; ?>
</div>

Codeigniter Days Left mysql Query

I have a simple script that displays products. In my database I store both date_posted and end_date as fields in he products table. On my homepage I display the 20 most recent posted products.
I want to display products that have 1 or more days left. So far I've manage to do this but using the front end script and css and this isn't best practice because it won't display 20 products if some among them have ended.
Below is the script that i have:
my View =>
<?php $i=0;?>
<?php foreach($product as $p): ?>
<?php
$startDate = date('m/d/Y');
$endDate = $p['endDate'];
$seconds_left = (strtotime($endDate) - strtotime($startDate));
$days_left = floor($seconds_left / 3600 / 24);
?>
<?php $i+= 1; ?>
<td <?php //if($days_left<=0){echo "class='ended'";} ?>>
<a href="<?php echo base_url()."user/product/".$p['productID']; ?>" class="product_name"><?php echo $p['productName']; ?>
</a>
<ul class="nav">
<li><i class="icon-home"> </i> Location : <?php echo substr($p['location'],0,25); ?></li>
<li><i class="icon-tags"> </i> Budget : Tshs <?php echo number_format($p['Price']); ?></li>
<li><i class="icon-tasks"> </i> <?php if($p['CategoryID']==4){echo"Number of rooms: ";}else echo "Quantity :"?> <?php echo $p['quantity']; ?></li>
<li><i class="icon-eye-open"> </i> Bids : <?php echo $p['Bids']; ?></li>
<li><i class="icon-time"> </i> Days Left: <?php echo $days_left; ?></li>
</ul>
</td>
my Model=>
function get_product($productID){
$this->db->select()->from('products')->where('productID',$productID);
$query = $this->db->get();
return $query->first_row('array');
}
You'll need to get a date in your where clause.
Something like this should do the trick:
function get_product($productID){
$this->db->select()
->from('products')
->where('productID',$productID)
->where('end_date >=', date('Y-m-d H:i:s', strtotime('- 1 day')))
->limit(20);
$query = $this->db->get();
return $query->first_row('array');
}

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.

How include the wordpress schedule feature in my own plugin?

My plugin includes a custom content textarea and I want add the same schedule feature that wordpress posts have.
Is there some easy procedure to do it?
HTML:
<div class="misc-pub-section curtime misc-pub-section-last">
<?php
$datef = __('M j, Y # G:i');
$stamp = __('Publish <b>immediately</b>');
$date = date_i18n($datef, strtotime(current_time('mysql')));
?>
<span id="timestamp"><?php printf($stamp, $date); ?></span>
<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" tabindex='4'><?php _e('Edit') ?></a>
<div id="timestampdiv" class="hide-if-js"><?php touch_time(0, 0, 5); ?></div>
</div>
PHP:
add_action('admin_print_scripts', 'header_scripts');
function header_scripts()
{
wp_enqueue_script('nomikos_my_plugin_js_comment', site_url('wp-admin/js/comment.js'));
}

Categories