how can i display date and time minus six hours in a post. For example now it's 23/02/2016 14:54, I need to display 23/02/2016 8:54. I'm using this in function.php of my theme:

function displaydate(){
    return date('d/m/Y G:i');
add_shortcode('date', 'displaydate');

and then I add [date] in post, but how can I display six hours back? Thanks a lot.

  • 3
    I'd recommend prefixing your shortcode, as date is a very generic name, and is likely to clash. Also, if your -6 hours is to adjust to your timezone, there are better ways of doing this using WordPress APIs rather than hardcoding the difference
    – Tom J Nowell
    Feb 23 '16 at 14:22

This is not a WordPress question, but to help you out:

function displaydate() {
    return date( 'd/m/Y G:i', strtotime( '-6 hours' ) );
add_shortcode('date', 'displaydate');

The shortcode itself should work fine.

  • Thanks fischi, it works fine. Now I have this code in my archive.php: <?php echo date_i18n('d/m/Y H:m', time()); ?>, how can I obtain same effect (six hours back)?
    – Klod
    Feb 23 '16 at 18:14
  • 1
    just replace time() with strtotime( '-6 hours' )
    – fischi
    Feb 23 '16 at 21:38
  • Mightn't it also be useful to include a call to date_default_timezone_set? Otherwise, if/when you move the site to a server in a different timezone, you might not be getting the result you want.
    – Jeff Cohan
    Feb 25 '16 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.