Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-03-22 10:25:09

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

PHP date() for a given time *next* year

I want to output a date in an article. I’d like it to be midnight on January 1st of next year, and not have to update it, or bodge expiry timestamps etc.

Essentially: take the current year, add a year to it, and dress up the output with a more verbose date() construct. This is the format for the output:

Sat, 01 Jan 2022 00:00 +0000

…and this is how I’m currently doing it:

<txp:php>$date = date('Y') . '-01-01';echo date('D, d M Y', strtotime("+12 months $date")) . ' 00:00 +0000';</txp:php>

Is there a cleaner / better way of doing it?

Thanks in advance for any advice.

Last edited by gaekwad (2021-03-22 10:25:28)

Offline

#2 2021-03-22 10:42:06

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: PHP date() for a given time *next* year

There is nothing wrong with your code, imo, but you can also try strtotime('1st January next year').

Offline

#3 2021-03-22 11:11:31

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Re: PHP date() for a given time *next* year

etc wrote #329393:

There is nothing wrong with your code, imo, but you can also try strtotime('1st January next year').

Thank you, etc – I appreciate your advice!

I’ve merged that in and settled on <txp:php>echo date('D, d M Y H:i', strtotime('1st January next year')) . ' +0000';</txp:php> for now, that seems to be the cleanest route.

Offline

#4 2021-03-22 11:36:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,269
Website GitHub

Re: PHP date() for a given time *next* year

On a general side note, depending on application: sometimes it’s prudent to use 12pm instead of midnight. That helps things stay more stable around DST/timezones because 12pm +/- an 11-hour shift still yields ‘today’, almost regardless of where you are in the world. Only a few stray 12/13-hour timezones compared to where the server is located will be affected and may return an hour or so into yesterday or tomorrow.

Last edited by Bloke (2021-03-22 11:38:26)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2021-03-22 11:46:12

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Re: PHP date() for a given time *next* year

Bloke wrote #329395:

On a general side note, depending on application: sometimes it’s prudent to use 12pm instead of midnight. That helps things stay more stable around DST/timezones because 12pm +/- an 11-hour shift still yields ‘today’, almost regardless of where you are in the world.

Thanks, Bloke – I was wondering about this. I don’t know of any DST trigger dates around Jan 1, and the server is set to UTC (best practice) year round to avoid wiggle or duplicated cron tasks.

Offline

#6 2021-03-22 11:47:10

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Re: PHP date() for a given time *next* year

No secrets here, I’m getting textpattern.com/security.txt ready for business. See securitytxt.org for more info.

Last edited by gaekwad (2021-03-22 11:47:24)

Offline

Board footer

Powered by FluxBB