Skip Navigation

[Résolu] Days to go for future date calculation (Future Date – Now).

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Date counter on a custom user field of current user.
Solution:
you will try with wordpress function get_user_meta(), for example:

/** Custom Countdown Shortcode
 */
  
function custom_date_countdown_shortcode ( $atts ) {
   $timestamp = get_user_meta( get_current_user_id(), 'wpcf-wedding-date', true );
   $timeleft = $timestamp - mktime();
   return round ($timeleft/(60*60*24));
}
add_shortcode( 'wedding_countdown', 'custom_date_countdown_shortcode' );

Relevant Documentation:
http://codex.wordpress.org/Function_Reference/get_user_meta

This support ticket is created Il y a 8 années et 3 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 réponses, has 2 voix.

Last updated by lindsayH Il y a 8 années et 3 mois.

Assisted by: Luo Yang.

Auteur
Publications
#361467

I created a shortcode in my functions.php and added it to views. Then on a page I added the shortcode itself as [wedding_countdown users='[wpv-current-user info="id"]'] so that only looking at the current user... but my calculations are huge... so my maths must be off or have I got something else wrong.

I want to calculate between a custom user field (wedding date) and today's date. The custom field will be in the future so I'm counting down in days.

i.e
Wedding Date = 30/01/16
Today's Date = 23/01/16
Result = Only 7 days to go!

/** Custom Countdown Shortcode
 */

function custom_date_countdown_shortcode ( $atts ) {
   global $post;
 
   $timestamp = get_post_meta( $post->ID, 'wpcf-wedding-date', true );
   $timeleft = $timestamp - mktime();
   return round ($timeleft/(60*60*24));
}
add_shortcode( 'wedding_countdown', 'custom_date_countdown_shortcode' );
#361677

Dear lindsay,

As you mentioned above, you are going to output the value of user field, you will try with wordpress function get_user_meta(), for example:

/** Custom Countdown Shortcode
 */
 
function custom_date_countdown_shortcode ( $atts ) {
   $timestamp = get_user_meta( get_current_user_id(), 'wpcf-wedding-date', true );
   $timeleft = $timestamp - mktime();
   return round ($timeleft/(60*60*24));
}
add_shortcode( 'wedding_countdown', 'custom_date_countdown_shortcode' );

More help:
get_user_meta
Retrieve a single meta field or all fields of user_meta data for the given user. Uses get_metadata()
http://codex.wordpress.org/Function_Reference/get_user_meta

#361704

Wow - brilliant, seems so obvious now. Thank you so much for your help though... I will learn from it.

#362005

Just added for completeness.

For anyone googling this may be of further help... as once the date has passed would not want to display a minus figure. This will check if the wedding date is after today's date, if it is it will return 'Congratulations...' - if not it will return a message, also containing the number of days to go before the wedding date.

/** Custom Countdown Shortcode
 */

function custom_date_countdown_shortcode ( $atts ) {
   $timestamp = get_user_meta( get_current_user_id(), 'wpcf-wedding-date', true );
   $timeleft = $timestamp - mktime();
   
	$wt_time    =   strtotime($timestamp);
	$cur_time   =   strtotime(now);


	if($wt_time > $cur_time ) {
   		return 'Congratulations to you both.';
	}
		else {
			// do the time left stuff
			return 'Only '. round ($timeleft/(60*60*24)). ' more days to go before your very special day!';
			} 
	}

add_shortcode( 'wedding_countdown', 'custom_date_countdown_shortcode' ); 
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.