simple php javascript countdown to DATETIME

2019-08-27 19:51发布

I have a DATETIME date in a mysql field, and i'd like the simplest timer displayed on my PHP page counting down to that. The timer will never be over a few minutes.

I'm guessing some kind of javascript php hybrid is needed?

Eg "40 secs left"

Note: I know nothing about javascript, but half decent at PHP.

2条回答
Melony?
2楼-- · 2019-08-27 20:40

you can use jQuery Countdown for that, so you just need to provide a date in correct format described there

查看更多
三岁会撩人
3楼-- · 2019-08-27 20:42

Take a look at Keith Woord's jQuery Countdown plugin. It is very easy to use and you can pass it your DATETIME on page load via PHP echo / print.

I hope this helps!

EDIT:

To implement this solution you could do the following:

<head>
...

<!-- Include jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){

    // Activate Countdown //   
    $('timerDiv').countdown({ 
        until: new Date(<?= $date ?>), // Date need to be in "YYYY, Start - End, M" Format like this: "2012, 8 - 1, 8"
        format: 'HMS' // H = Hours, M = Minutes, S = Seconds. Remove letter to omit it
    });
</script>

...
</head>
<body>
    <div id="timerDiv"></div>
</body>
查看更多
登录 后发表回答