How to make countdown timer communicate with a mysql database

How do i make it so that once the countdown timers reach 0 a PHP file is executed that for example deletes a database?

timer.php







<?php

    date_default_timezone_set('America/Los_Angeles');
    $rawDateStart = date('F j, Y H:i:s', date_timestamp_get(DateTime::createFromFormat('F j, Y H:i:s', 'January 7, 2016 07:00:00')));
    $rawDateCurrent = date('F j, Y H:i:s');
?>

<script>
    var printSeconds, printMinutes, printHoursDaily, printHoursWeekly, printDays;

    // Get strating and current date
    var startDate = new Date("<? echo $rawDateStart; ?>");
    var currentDate = new Date("<? echo $rawDateCurrent; ?>");

    $(function(){    

        function calculateTime()
        {
            // Increment current time
            currentDate.setSeconds(currentDate.getSeconds() + 1);

            // Calcualte time passed since the start
            var timePassed = currentDate.getTime() - startDate.getTime();
            var secondsSinceStart = Math.floor(timePassed / 1000 );
            var minutesSinceStart = Math.floor(secondsSinceStart / 60);
            var hoursSinceStart = Math.floor(minutesSinceStart / 60);
            var daysSinceStart = Math.floor(hoursSinceStart / 24);

            // Calculate time since last daily (18h) reset
            var dailySecondsElapsed = secondsSinceStart % 60;
            var dailyMinutesElapsed = minutesSinceStart % 60;
            var dailyHoursElapsed = hoursSinceStart % 18;

            // Calculate time since last weekly (7d) reset
            var weeklyHoursElapsed = hoursSinceStart % 24;
            var weeklyDaysElapsed = daysSinceStart % 7;

            // Calculate remaining time until next daily (18h) reset
            var remainingSeconds = 59 - dailySecondsElapsed;
            var remainingMinutes = 59 - dailyMinutesElapsed;
            var remainingHoursDaily = 17 - dailyHoursElapsed;

            // Calculate remaining time until next weekly (7d) reset
            var remainingHoursWeekly = 23 - weeklyHoursElapsed;
            var remainingDays = 6 - weeklyDaysElapsed;

            // If any of the variables is only a single digit number, then add 0 before
            printSeconds = (remainingSeconds > 9) ? remainingSeconds : '0' + remainingSeconds;
            printMinutes = (remainingMinutes > 9) ? remainingMinutes : '0' + remainingMinutes;
            printHoursDaily = (remainingHoursDaily > 9) ? remainingHoursDaily : '0' + remainingHoursDaily;
            printHoursWeekly = (remainingHoursWeekly > 9) ? remainingHoursWeekly : '0' + remainingHoursWeekly;
            printDays = (remainingDays > 9) ? remainingDays : '0' + remainingDays;
        }

        setInterval(function() {
            calculateTime();

            // Print
            jQuery('#daily p').html(printHoursDaily+':'+printMinutes+':'+printSeconds)
            jQuery('#weekly p').html(printDays+':'+printHoursWeekly+':'+printMinutes+':'+printSeconds)
        }, 1000);
    });
</script>

<div id="daily"><p ></p></div>
<div id="weekly"><p ></p></div>

</bod

make an ajax request to a php file that does whatever it is you want.

just be aware anyone with access to the site can run that command any time they want by manually sending a request

i’d need some help with that or atleast a better explanation. sorry

Sooooo what is the goal rather than how you are trying to approach it? Deleting a database is not something you should really do programmatically to begin with, so I question the why on this.

Sounds like you should be using a cron job/schedule task on the server to accomplish this.

That would be the route I would take, but deleting a database seems like something is really wrong with the design, unless you are wiping an entire system…

Sponsor our Newsletter | Privacy Policy | Terms of Service