ACF pro - return day with date picker - wordpress

I use ACF pro for my wordpress.
My datepicker field is a repeater field.
I need to return only the day.
my code :
<?php
if( have_rows('dates') ):
while ( have_rows('dates') ) : the_row();
echo get_sub_field('date')."</br>";
endwhile;
else :
echo __( 'No dates available.','mywebsite' );
endif;
?>

You can set the return value of the date field.
EDIT
Ok, so if you want to display two dates, one full and one is only day, you first need the return value to be the full date, for this example lets day it d/m/Y
$full_date = get_sub_field('date'); // the full date (format d/m/Y);
$day_from_date = DateTime::createFromFormat('d/m/Y', $full_date)->format('d'); // will get the day from the $full_date
This will get you the result you need.
See DateTime::createFromFormat for more information about the method

The get_sub_field() function would typically return a string depending on the field type. This instance it will return a timestamp as as string eg '2021-06-28 10:28:00'
If you want to return the day only you could use PHP's strtotime function which will then return you a datetime epoch integer - this can then be used in conjunction with php's date function to print as the day. Here is a list of the formats you can use for the date function : PHP DateTime::format
Example :
<?php
if (have_rows('dates')):
while (have_rows('dates')) : the_row();
$dateTime = strtotime(get_sub_field('date'));
echo("Day : " . date('l', $dateTime) . "</br>");
endwhile;
else :
echo __('No dates available.', 'mywebsite');
endif;
?>

As another solution which is within your setup rather than your code - change the 'Return Format' of the actual field.
See : ACF Date Time Picker Docs
Examples would be :
'd' - Day of the month, 2 digits with leading zeros
'j' - Day of the month without leading zeros
'D' - A textual representation of a day, three letters
'l' - A full textual representation of the day of the week
Then your original code will output the correct format :
<?php
if( have_rows('dates') ):
while ( have_rows('dates') ) :
the_row();
echo('Day: ' . get_sub_field('date') . "</br>"); //This will now output your 'Return Format' in ACF setup
endwhile;
else :
echo __( 'No dates available.','mywebsite' );
endif;
?>

Related

How display the spell out of an amount with currency details in WooCommerce

I am trying to spell out WooCommerce order total amount, in my invoice.php template file can reach order total amount.
First I tried:
$total = $order->get_total();
<?php echo ( $total ); ?> -
<?php
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($total); ?>
The order total displayed is 225.00 and the spell out display is: two hundred twenty-five
Edit:
I found the following solution:
<?php $number = $order->get_total() ;
$formatter = new NumberFormatter('tr', NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
echo $formatter->format($number); ?>
But the result shows like that: two hundred twenty-five
The desired display should be: two hundred twenty-five turkish liras , zero penny.
How can i do this ?
The NumberFormatter SPELLOUT constant doesn't handle the decimals.
You can use the following custom function to display a float number amount spell out like (with the currency details):
function wc_spellout_amount( $amount, $country_code = 'tr' ) {
$formatter = new NumberFormatter($country_code, NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
$amounts = explode('.', (string) $amount); // Separating decimals from amount
$output = $formatter->format($amounts[0]);
$output .= ' ' . _n('turkish lira', 'turkish liras', $amounts[0], 'woocommerce');
$output .= ', ' . $formatter->format($amounts[1]);
$output .= ' ' . _n('penny', 'pennies', ( $amounts[1] > 0 ? $amounts[1] : 1 ), 'woocommerce');
return $output;
}
Code goes in functions.php file of the active child theme (or active theme).
Usage: Then you will use it as follows in your code:
echo wc_spellout_amount( $order->get_total() );
Tested and works.

Using system date format

I have WordPress theme and I am using get_the_date function to get the publish date of post :
<?php echo get_the_date( 'M-d-y' ); ?>
But it seems it will be better to use get_option( 'date_format' ).
How can I display post publish date using get_option function?
Doesn't need to use get_option for getting post published date. You can get that by using get_the_date function properly.
Here is the solution:
Make the date appear as Monday January 11, 2017, use
$post_date = get_the_date( 'l F j, Y' ); echo $post_date;
To make the date appear as Wed Jan 9, use
$post_date = get_the_date( 'D M j' ); echo $post_date;
Read more about get_the_date and check date formatting options here, in wordpress docs.

Show updated date only if different from published date in Wordpress

Trying to show the published date, and the update date if it differs from published date, I tried this code in my wordpress template:
<p class="card-text">Published <?php the_date();?>.
<?php
if ( the_date( 'U' ) !== the_modified_date( 'U' ) ) {
echo "Updated " . the_modified_date('F j, Y');
}
?>
However i do net get any output. Possibly simple, but still I only get a very long number as output.
You should use get_the_date and get_the_modified_date because they return the value and not printing it out.
if ( get_the_date( 'U' ) !== get_the_modified_date( 'U' ) ) {
echo "Updated " . get_the_modified_date('F j, Y');
}

Print WooCommerce products list sorted by SKU

My client has asked "I would like to be able to print a listing of all products by SKU (sorted alphanumerically) with description, and by description (alpha) with the associated SKU." I think the closest I could get for him is to have the SKU sorted alphanumerically but not both, though I could be wrong.
Looking at this question: Woocommerce: get a list for all sku product, I wonder if I could build a table instead of a list and pull the description in next to the SKU?
Here's the code I'm thinking of based on the link above (without sorting):
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
query_posts( $args );
if( have_posts() ):
echo '<table>';
while ( have_posts() ) : the_post();
echo '<tr><td>'. $product->get_sku() . ' - ' . $product->get_title() . '</td><td>' . $product->get_description() . '</td></tr>';
endwhile;
echo '</table>';
endif;
I don't think the description is the correct way to call it, but I didn't see one to call in the short description.Also, I'm not sure how I would have it sort by SKU. My PHP knowledge is limited.
Even if that code worked, I'm not sure where I would put it to call this in. Can I make it on an admin page or would it be better to create a regular page and use a different template and put this directly into the PHP of that template file?
Any suggestions would be great!

Comparing timestamps in WP_Query

I can't figure out how to compare timestamps in the WP_query. On the site, classes/events are entered as posts with meta added for the date they begin. In calendar_functions.php, $begtime is defined as the timestamp for the date the class/event starts (not the date the post is created) and $today is defined as today's timestamp. I've tested both of those and they do return correct values.
The below code works fine, it displays a random class/event... except that past classes/events show up. I want to be able to only display items that happen from today on. Its not an option to go in and delete classes/events that have already happened.
Not sure how to handle the >= part.
<?php include('calendar_functions.php'); ?>
<?php $my_query = new WP_Query('"$begtime >= $today"&posts_per_page=1&cat=5,43&orderby=rand');
if (have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I will also need to be able to compare two sets of dates, as some classes have two sessions, ex. $begtime >= $today -or- $begtime2 >= $today. ($begtime2 is also already defined).
you need something like this.
function filter_where( $where = '' ) {
// posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

Resources