http:\/\/php.net\/manual\/en\/function.time.php<\/a><\/em><\/cite><\/blockquote>\n\n\n\nThe visitor might see the page after the date of the event. We don't want to show them negative time, so we add this next:<\/p>\n\n\n\n
if ($time_remaining < 0) {\n $time_remaining = 0;\n}<\/code><\/pre>\n\n\n\nThat will show them 0 Weeks, 0 Days<\/em>.<\/p>\n\n\n\nSplit the time into segments<\/h2>\n\n\n\n First we want to calculate the total number of days between \"now\" and the event:<\/p>\n\n\n\n
$total_days_remaining = ceil($time_remaining \/ (60 * 60 * 24));<\/code><\/pre>\n\n\n\nThe (60 * 60 * 24)<\/em> is because $time_remaining<\/em> is in seconds. We need to convert it into days.<\/p>\n\n\n\nWe use ceil()<\/em> to round the fraction up to the nearest whole number. That way, if the event is tomorrow, it will show as one day remaining.<\/p>\n\n\n\nNext we split the days into weeks and days, if there are more than 7 days remaining, or just into days, if there are fewer.<\/p>\n\n\n\n
if ($total_days_remaining >= 7) {\n $weeks_remaining = sprintf(\"%02d\", floor($total_days_remaining \/ 7));\n $days_remaining = sprintf(\"%02d\", $total_days_remaining % 7);\n} else {\n $weeks_remaining = sprintf(\"%02d\", 0);\n $days_remaining = sprintf(\"%02d\", $total_days_remaining);\n}<\/code><\/pre>\n\n\n\nUnder $weeks_remaining<\/em>, notice we use floor()<\/em> after calculating how many weeks are left. This will round the result down to the nearest whole number. That way, it won't say there are two weeks and one day remaining if there are only eight days left.<\/p>\n\n\n\nWe use sprintf(\"%02d\", $total_days_remaining)<\/em> to turn the number returned from the calculations into a string. %02d<\/em> is the format for numbers like 03, rather than 3. If you prefer it to be like the latter, you'd use \"%d\"<\/em>.<\/p>\n\n\n\nStyle and echo the result<\/h2>\n\n\n\n Finally, we echo the result, formatted the way we want:<\/p>\n\n\n\n
echo \"<div class='next-event'>\n $weeks_remaining Weeks, $days_remaining Days\n <\/div>\";<\/code><\/pre>\n\n\n\nThis is the html that will actually be outputted to the front end.<\/p>\n\n\n\n
If you want to get the look from the example at the top of this tutorial, you can wrap the variables in <span> tags, formatting them to be larger.<\/p>\n\n\n\n
Notice that the whole thing is wrapped in double quotation marks. That's what allows us to use html and input the variables directly.<\/p>\n\n\n\n
Add the shortcode, passing in the time you're counting down to as a shortcode attribute<\/h2>\n\n\n\n Finally, we add this shortcode wherever we need it to display:<\/p>\n\n\n\n
[next_event date=\"1 July 2019\"]<\/code><\/pre>\n\n\n\nThe next_event<\/em> is set in add_shortcode()<\/em>.<\/p>\n\n\n\nIf you want to use something other than date=\"\"<\/em>, then make sure you change the references to !array_key_exists(\"date\", $atts)<\/em> and $atts['date']<\/em> in the next_event_shortcode<\/em> function.<\/p>\n\n\n\nThe beauty of doing it this way is that you can use whatever format of date works for you.<\/p>\n\n\n\n
Some formats you can use:<\/p>\n\n\n\n
m\/d\/y (American style)<\/li> d-m-y (everyone else)<\/li> 1 July 2019<\/li> 1 Jul 19<\/li> Jul 1, 2019<\/li> 2019-07-01<\/li><\/ul>\n\n\n\nNotice that if you want to use the American style m\/d\/y, you need to use slashes (\/) in between, not hyphens (-). This is how the system differentiates something like 1\/7\/19 (7 January) from 1-7-19 (1 July). See the strtotime() notes<\/a> for more discussion on this.<\/p>\n\n\n\nI hope you've found this tutorial useful. This won't animate the countdown like some of the fancier versions, but every time a visitor refreshes or visits the page, they will see the correct number of days til the event.<\/p>\n","protected":false},"excerpt":{"rendered":"
Countdown timers are very useful on things like sales pages or event listings. Most of the decent page builders have great looking countdown timers, but sometimes you just want something that looks a specific way and does exactly what you need. Fortunately it’s pretty easy to create a countdown timer in WordPress without a plugin:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"yoast_head":"\n
How To Create A Countdown Timer In WordPress Without A Plugin<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n