Science, Tech, Math › Computer Science Use PHP Mktime to Create a Countdown Share Flipboard Email Print GlobalStock/E+/Getty Images Computer Science PHP Programming Tutorials MySQL Commands Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Angela Bradley Angela Bradley Computer Science Expert B.A, History, Eastern Oregon University Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms. Learn about our Editorial Process Updated on January 09, 2019 Because the ist_dst parameter used in this example was deprecated in PHP 5.1 and removed in PHP 7, it is not safe to rely on this code to deliver accurate results in current versions of PHP. Instead, use the date.timezone setting or the date_default_timezone_set() function. If your webpage focuses on a specific event in the future such as Christmas or your wedding, you may want to have a countdown timer to let users know how long it is until the event occurs. You can do this in PHP using timestamps and the mktime function. The mktime() function is used to artificially generate the timestamp for a selected date and time. It works the same as the time() function, except it is for a specified date and not necessarily today's date. How to Code the Countdown Timer Set a target date. For example, use February 10th, 2017. Do that with this line, which follows the syntax : mktime(hour,minute,second,month,day,year: ist _dst). $target = mktime(0, 0, 0, 2, 10, 2017) ;Establish the current date with this line: $today = time () ;To find the difference between the two dates, simply subtract: $difference =($target-$today) ;Since the timestamp is measured in seconds, convert the results into whatever units you want. For hours, divide by 3600. This example uses days so divide by 86,400—the number of seconds in a day. To make sure the number is an integer, use the tag int. $days =(int) ($difference/86400) ;Put it all together for the final code: <?php $target = mktime(0, 0, 0, 2, 10, 2017) ; $today = time () ; $difference =($target-$today) ; $days =(int) ($difference/86400) ; print "Our event will occur in $days days"; ?> Cite this Article Format mla apa chicago Your Citation Bradley, Angela. "Use PHP Mktime to Create a Countdown." ThoughtCo, Feb. 16, 2021, thoughtco.com/use-mktime-to-create-countdown-2693921. Bradley, Angela. (2021, February 16). Use PHP Mktime to Create a Countdown. Retrieved from https://www.thoughtco.com/use-mktime-to-create-countdown-2693921 Bradley, Angela. "Use PHP Mktime to Create a Countdown." ThoughtCo. https://www.thoughtco.com/use-mktime-to-create-countdown-2693921 (accessed March 21, 2023). copy citation Featured Video