How to set it up a countdown PHP script and coming soon page?

How to set it up a countdown PHP script and coming soon page?

As I understand PHP manages input and a countdown script is using JavaScript?

Example:

      // COUNTDOWN script
      const oneYearFromNow = new Date()

      document.querySelectorAll('.js-countdown').forEach(item => {
        const days = item.querySelector('.js-cd-days'),
          hours = item.querySelector('.js-cd-hours'),
          minutes = item.querySelector('.js-cd-minutes'),
          seconds = item.querySelector('.js-cd-seconds')

        countdown(oneYearFromNow.setFullYear(
          oneYearFromNow.getFullYear() + 1),
          ts => {
            days.innerHTML = ts.days
            hours.innerHTML = ts.hours
            minutes.innerHTML = ts.minutes
            seconds.innerHTML = ts.seconds
          },
          countdown.DAYS | countdown.HOURS | countdown.MINUTES | countdown.SECONDS
        )
      })

LaunchDate

PHP handles things on the server end. Javascript handles things on the user’s browser end.

If you want a countdown timer to be ticking on the screen in real time, it will need to be Javascript.

Your code currently appears to be counting down 1 year from the moment the page is loaded.
I assume you actually want to set it to count down to a specific date, no matter when the page was loaded.

The number of seconds between a target date and now is new Date("targetdateinfuture") - new Date()

Note however that “targetdateinfuture” will resolve to the local user’s timezone if unspecified.

How to countdown 3 months and 6 months? It means simplicity as it does not need countdown and PHP script to calculate specific date and gradual reduction…

countdown(oneYearFromNow.setFullYear(
          oneYearFromNow.getFullYear() + 1),

Well nothing i said was about PHP. You dont need PHP for something as simple as “countdown 3 months”, but saying that implies “3 months from the moment the page was loaded. Every time the page is loaded, start at 3 months”.

As I understand we push comping soon page. 3 months from the moment the page was loaded as coming soon page within WordPress.

see, thats not “3 months”, that’s a specific date in the future. You’re counting down to the specific date.

So your code cant say “3 months”. If your code says 3 months, and you publish that page today, April 14, 2026, then your target date is July 14, 2026. And you want to put into your code July 14, 2026.

Because if I, as a user, load that page on May 1st, your target date is no longer 3 months. It’s 2 months and 17 days.

Does that make sense now? The code is evaluated at the time the USER visits it, not at the time you publish it.

Thank you for the message! I will modify above code.

Yeah, you’re right — PHP and JavaScript play different roles here. PHP works on the server, while the countdown itself should be handled with JavaScript in the browser for real-time updates.

Also, instead of “3 months from now,” it’s better to set a fixed future date, otherwise the countdown will reset every time someone reloads the page.

For a simple coming soon page, you honestly don’t even need PHP unless you’re doing something backend-related — JavaScript alone is enough for the timer :+1:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.