0

The javascript countdown code I am using requires me to use this format: "year, month - 1, day, hour, minute, second". I have the following code, which works, but I'm wondering if there's a simpler method of getting it to work (one with less lines, one that might be quicker?)

$year = date('Y',$date);
$month = date('n',$date);
$day = date('j',$date);
$hour = date('g',$date);
$minute = date('i',$date);
$second = date('s',$date);
$js .= "var newYear = new Date();
        newYear = new Date(".$year.", ".$month." - 1, ".$day.", ".$hour.", ".$minute.", ".$second.");
        $('#timer').countdown({
            until: newYear,
            layout: '{hn} h {mn} m {sn} s'
        });";
3
  • 4
    none of the php is needed, js has date functions to Commented Apr 3, 2014 at 22:17
  • @Dagon Well, some PHP is needed, to transport the variable $date, which has presumably been calculated elsewhere, into an appropriate JS value. Commented Apr 3, 2014 at 22:33
  • Using the date function six times and then concocting the results is unnecessary, to do it all with one date function call: date("Y, n, y, g, i, s", $date). Commented Apr 3, 2014 at 23:08

2 Answers 2

1

You should avoid generating Javascript via PHP. Your Javascript code should not change per-user or per page render, and you should simply include it from a static file. Any data should be accessed over Ajax or stored locally in that page via a DOM node:

<input type="hidden" id="someAppData" value="1242525242"/>
$(function () {
    // Grab the data from the DOM node
    var newYear = $('#someAppData').val();

    $('#timer').countdown({
        until: newYear,
        layout: '{hn} h {mn} m {sn} s'
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

This way you can include an external JS file and not have to worry about making your web server parse .js files with the PHP interpreter.
0

You don't need any PHP code for this besides passing along the $date value to your HTML page. Consider passing over a timestamp value.

HTML/(PHP >5.2.0) Code:

<script>
    var newYear = new Date(<?= (new DateTime($date))->getTimestamp() * 1000 ?>);
    $('#timer').countdown({
        until: newYear,
        layout: '{hn} h {mn} m {sn} s'
    });";
</script>

Basically you're passing over the timestamp from PHP (in seconds) to your Javascript (in milliseconds) into their respective Date objects. If you don't have short tags enabled, you can replace what's in the angle brackets with <?php echo (new DateTime($date))->getTimestamp() * 1000; ?>

4 Comments

Why (new DateTime($date))->getTimestamp() * 1000 when you could just do time()*1000?
OP didn't ask for current time. This could be a value that's been passed in through a form or query string.
strtotime($date) then ;)
Whiehever, I figure that this would be most accurate, and respective to timezones if done right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.