Jump to content

How can I set php dates 10 years into the future?


liquid2g

Recommended Posts

Hi everyone,

 

I'm writing a science fiction wordpress blog set 10 years into the future.

I basically need all posts to display as if they are 10 years from now.

 

Eg. posts needs to display as:

February 7, 2021 instead of

February 7, 2011

 

This will be for every post that I write.

 

 

How can I automatically add 10 years to every post date?

(And where would I put that code?)

 

Currently, the wordpress php is calling the date with `<?php the_time(__('M j, Y')) ?>`

 

I realise it's a bit of an odd request, but it's necessary for this particular project. I know it's possible - but I'm brand new to php and am not sure how. =)

 

I hope someone out there can help. It would be very much appreciated.

 

many thanks in advance

 

Luke

Link to comment
Share on other sites

You can do exactly what your wanting INSIDE of wordpress. No programming required. Go to the post you want to set a date for, and go to edit mode.

Over on the right hand side you'll see all of the publish options. The publish date is listed there as well. Edit that, and change the year to 10 years in the future. That

will show the date you have entered all throughout the site for that blog posts.  Rinse/Repeat for any new or old blog posts that you want to post into the system.

 

Very simple, no programming experience required.

Link to comment
Share on other sites

Thanks Phppaper,

I tried doing as you said, changing teh date in single.php to

<?php the_time(__('M j, Y+10')) ?>

but it ends up displaying at 2011+10 instead of 2021.

What am I doing wrong?

 

 

businessman332211, thanks for the suggestion, but setting a post date 10 years into the future within wordpress simply schedules the post to appear in 10 years time, rather than having it appear now with the future date.

Any other suggestions?

Link to comment
Share on other sites

Thanks silkfire.... this is getting close to what I need but

echo date('M j, Y', strtotime('+10 years'));

gives me today's date 10 years into the future,

 

wheras I need it to be the article/post date 10 years into the future. I tried

echo the_time('M j, Y', strtotime('+10 years'));

but it doesn't add the 10 years when I do this...

 

Any ideas that would get me over the finish line?

 

thanks in advance!

Link to comment
Share on other sites

Another approach would be to ignore all of the helpful comments here and attach a filter (using Wordpress's add_filter() in a plugin)  to the_time to do any manipulation of the date/time that you want to do (including, adding 10 years).

Link to comment
Share on other sites

Thanks Salathe. Your alternate suggestion sounds good, but I'm brand new to php and am unsure how to attach a filter (using Wordpress's add_filter() in a plugin)  to the_time...

 

And, thanks again to Silkfire -

I tried

echo date('M j, Y', strtotime('+10 years', strtotime(the_time('Y-m-d H:i'))));

as you suggested, but it displays on the frontend as: "2011-02-07 16:18JAN 1, 1980"

It seems that it's close to working but it has transported us back into 1980  :) Is there anything you can think of that I might change to get it working?

 

thanks again in advance

Link to comment
Share on other sites

QuickOldCar, I would love to jump in the DeLorean, but it's in at the shop getting fixed at the moment, so all of my future travel needs to be done via php =)

 

Thanks lalnfl, I tried adding this to single.php:

 

<?php 
$year = date("Y");
$ten_years = $year + 10;
$future = mktime(0,0,0,$month,$day,$ten_years);
$future_date = date("F j, Y", $future);
echo $future_date;
?>

 

but it just returned the error "WARNING: MKTIME() EXPECTS PARAMETER 4 TO BE LONG, "

any thoughts?

 

thanks again

Link to comment
Share on other sites

Aha! Finally got there with...

<?php
$date = get_the_date();
$daymonth = date("M j", strtotime($date)); # the day and the month
$year = date("Y", strtotime($date)) + 10; # the year
print "$daymonth $year";
?

 

I've implemented it across almost all dates on my wordpress site, but can't work out how to change the date in the 'Archives' widget sidebar. I tried archives.php but couldn't find what I needed to change... Does anyone know?

 

thanks again!

 

Link to comment
Share on other sites

Thanks Salathe. Your alternate suggestion sounds good, but I'm brand new to php and am unsure how to attach a filter (using Wordpress's add_filter() in a plugin)  to the_time...

 

And, thanks again to Silkfire -

I tried

echo date('M j, Y', strtotime('+10 years', strtotime(the_time('Y-m-d H:i'))));

as you suggested, but it displays on the frontend as: "2011-02-07 16:18JAN 1, 1980"

It seems that it's close to working but it has transported us back into 1980  :) Is there anything you can think of that I might change to get it working?

 

thanks again in advance

Strange my function should return February 07, 2021. I don't have WordPress but I substituted the_time() with date() and it worked.

Link to comment
Share on other sites

If you hooked a little plugin into your Wordpress which alters what the_time() outputs, you would not have to change any of your theme files.

 

I know that you said you're new to that, but it would be way easier than what it looks like you're currently trying to do!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.