How do I put "Time Ago" on Wordpress text area? - php

I’d like to add a text line saying that “The latest event has been ended 3 days ago”
And the part “3 days ago” will automatically change as the day goes by, e.g. 1 month ago and so on…. Is there any shortcode plugin I can use for this? Or any method?
I found this plugin https://wordpress.org/plugins/the-time-ago/ but it seems to work with post/page/comment time. Not for a text line.
Best.

If you want to use a custom timestamp for the event and know how to write basic php code, you can use the php's DateTime::diff method or better, use Carbon to get the difference between your custom timestamp and today's date.
Examples in the linked docs should suffice in understanding and to echo the difference between the dates. Now, use that inside WordPress, write that code in a function in your theme's functions.php file and return the value to create a shortcode for the difference.
function eventdiff_func( $atts ) {
// write code which calculates the difference here and store it in $diff
return $diff;
}
add_shortcode( 'event_diff', 'eventdiff_func' );
Now you can use the [event_diff] shortcode whereever you'd like to output the difference that you calculated.

You can use human_time_diff function for the difference is returned in a human readable format. you can read more about human_time_diff click here.

Related

Silverstripe 3.3: how to translate date month variable in a template?

On my page I have a date variable. I wish to translate it's shortened month name by locale (in my case Lithuanian).
I've set up the translations in langed/lang/lt_LT.yml:
Month:
Jan: 'Sau'
Feb: 'Vas'
Mar: 'Kov'
Apr: 'Bal'
...
And in my template when I put
$Date.Format(M d)
It gives the month and day always in English, no matter the locale (ex. "Apr 18", I need "Bal 18" in this case).
I have attempted to try to put the variable of Month into translation quotes of .ss template:
<%t Month.$Date.Format('M') %>
But it doesn't work. It throws an error:
"[User Error] Uncaught SSTemplateParseException: Parse error in template on line 16. Error was: Malformed opening block tag t. Perhaps you have tried to use operators?"
Can anyone please explain to me how am I doing this wrong?
You can use $Date.FormatI18N('%b') which utilizes strftime() which in turn needs your locale installed on the server to translate it. If not you just get the english (or default) locale.
See this gist for an extension to get the current locale in your controller.
The way I've implemented this is put a function on the controller:
public function MonthForVar($var) {
return _t("Month.$var");
}
and then you can call $MonthForVar($Date) in your template. Don't forget if you're doing it in a loop to go $Up.MonthForVar($Date)
** Should add that wmk's answer is better for date format however I was lead here from Google when I'm trying to translate something non-date related, in which case this should work.

What sort of date is this in PHP

I have a date I need to replicate in PHP. I just want to know if there is some sort of shortcode for it.
2013-05-09T15:23:59.802Z
It is ISO 8601. In PHP you would make it using:
echo date('c'); // as of PHP 5
See date()
This one liner achieves what you are asking.
echo date_format(date_create("#".$unix_timestamp, timezone_open('UTC')), 'Y-m-d\TH:i:s.000\Z');
Output:
2013-05-09T15:23:59.000Z

How would you combine this wordpress shortcode template code?

I am using a plugin that is generating my price value and pulling it from an array:
<?php echo __('Price: ', 'event_espresso'); ?></span> <?php echo
$org_options['currency_symbol'].$event->event_cost; ?>
I want to convert this generated value into my visitors own currency depending on the country their in with the plugin 'Worldcurrency'
However you have to input a value in the short code like so:
[worldcurrency curr="EUR" value="25"]
in united states will show:
(~30$ USD)
Now I know how to use a shortcode in a template php file but I don't know if its possible to insert my array value and currency symbol into this shortcode. Rather than using value="25" i need to use:
value="<?php echo $org_options['currency_symbol'].$event->event_cost; ?>"
Is this possible?
I'm not sure whether it's possible to use PHP code inline in a shortcode - and I suspect not, however an easy enough way to implement this would be to write a shortcode of your own that then called the other shortcode.
Your shortcode would generate the text you want (eg '[worldcurrency curr="EUR" value="25"]'), and call "do_shortcode($content)" which would then cause the other plugin to do the currency lookup for you.
You could put this in a plugin file and it would probably amount to less than 15 lines of code.
The other option is to modify the currency conversion plugin you are using to produce the output you want.

Wordpress apply get_excerpt formatting to a string

I would like to apply wordpress' get_excerpt format to a string I pass to it.
I know the standard excerpt only works within the loop - but I'm hoping there is a function or something I'm missing that generates the excerpt when you don't manually define one.
Fixed answer:
You need wp_trim_words(). Yes - you're right. wp_trim_excerpt() doesn't play nice with passed strings.
http://codex.wordpress.org/Function_Reference/wp_trim_words
wp_trim_words( "I would like to apply wordpress' get_excerpt format to a string I pass to it. I know the standard excerpt only works within the loop - but I'm hoping there is a function or something I'm missing that generates the excerpt when you don't manually define one.", 5, '[...]' ); returns "I would like to apply[...]"
Previous answer:
WordPress uses wp_trim_excerpt() to generate excerpts.
http://codex.wordpress.org/Function_Reference/wp_trim_excerpt
You probably want some filtering on there too before you output (depending on the source of your text).

How do I get the_time as one string of numbers in wordpress?

I'm using wordpress. I remember messing with the code of <?php the_time('d.m.y') ?> and getting something like "2011161457" (2011 16-1 4:57) I don't remember how I did it. I want just the numbers anyone knows how to do it?
Look at the PHP date() man page. Wordpress's time_time() is basically just a wrapper around that and uses the exact same formating options.

Categories