-1

Please consider the following scenario with a simple HTML document and a JavaScript loaded in it:

<!-- doc.html -->
<!doctype html>
<html lang="en">
    <head>        
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript" src="script.js"></script>
    </head>
</html>

// script.js
window.onload = function()
{
    var d = new Date();
    d.setHours(<?php echo(date("H")); ?>);
}

It's really Quite simple, isn't it? However, while this coode works perfectly for getting the current hour (which will be returned as an INT), it doesn't if I try to receive a more complex date/time format like the one below:

var serverTime = <?php echo date("Y-m-d H:i:s"); ?>;

This should encode to 1900-01-01 00:00:00. So, cause this is not a legal expression I want to encapsulate the result with quotes:

var serverTime = "<?php echo date("Y-m-d H:i:s"); ?>";

At a first glance this should work. Unfortunately, it doesn't. serverTime contains just an empty string.

What am I doing wrong?

11
  • What gets returned when you just do echo date("Y-m-d H:i:s"); in PHP? Commented Nov 28, 2011 at 14:31
  • Nothing. It should work fine. Commented Nov 28, 2011 at 14:31
  • Don't know why this wouldn't work, but to set a time in javascript I'd use var d = Date(); d.setTime(<?php echo time() * 1000; ?>); Commented Nov 28, 2011 at 14:36
  • 1
    @Sascha by the way. that original code would not encode to 1900-01-01 00:00:00, it would show the current time and date. PHP's date does not even go back that far! Commented Nov 28, 2011 at 14:38
  • 1
    Is a bit confusing what you put at the top that the name of your file is doc.html and not a php script. Commented Nov 28, 2011 at 14:52

6 Answers 6

2

Maybe your Timezone isnt correctly setup

try

<?
    date_default_timezone_set('America/Los_Angeles');
?>

or similar - full list can be found here

http://de3.php.net/manual/en/timezones.php

1
  • Finally figured it out. script.js needs to be renamed to script.php and the default timezone must be set. Commented Nov 28, 2011 at 16:20
0

The javascript won't execute your php code. Think of it like this: When the page is rendered php "replaces your php code" with the output of the php code.

So basically this

var serverTime = '<?php echo date("Y-m-d H:i:s"); ?>';

Should be rendered as

var serverTime = '2011-11-28 xx:xx:xx';
0

I think you are wrong in the way of you insert your parameters. Acording to this I think you should do this:

// script.js
window.onload = function()
{
    var d = new Date('<?php echo(date("F d, Y H:i:s")); ?>');
    d.setHours(<?php echo(date("H")); ?>, <?php echo(date("i")); ?>, <?php echo(date("s")); ?>);    
}

The statment var d = new Date('<?php echo(date("F d, Y H:i:s")); ?>'); is to follow the example of this link.

Because I can't see why php will fail to send the result of the date() function.

2
  • Using single quotation marks results in a String containing the PHP code. Commented Nov 28, 2011 at 15:04
  • @SaschaHoll I think all of these code are in a php script, so the quotes doesn't matter at the moment in you open the <?php ?> tags. Commented Nov 28, 2011 at 15:07
0

There is no reason why var serverTime = "<?php echo date("Y-m-d H:i:s"); ?>"; shouldn't return something like:

var serverTime = "2011-05-12 05:12:56";

Check to make sure that echo date("Y-m-d H:i:s"); is working correctly.

Other than that, the code should work fine.

See here: http://codepad.org/3CNx1ch9
Output: http://codepad.org/3CNx1ch9#output

5
  • So, that's why I can't understand it! Commented Nov 28, 2011 at 15:02
  • Dreamweaver reports a syntax error on the line var serverTime .... Commented Nov 28, 2011 at 15:08
  • @SaschaHoll what does that mean? Did you try it in a browser? Commented Nov 28, 2011 at 15:10
  • Yeah, Firebug says: "missing ; before statement" Commented Nov 28, 2011 at 15:14
  • let us continue this discussion in chat Commented Nov 28, 2011 at 15:14
-1

Try using single quotes:

var serverTime = '<?php echo date("Y-m-d H:i:s"); ?>';
2
  • 1
    single quotes or double quotes should not matter in this case. Commented Nov 28, 2011 at 14:32
  • I wasn't sure if nested quotes were confusing the PHP parser (unlikely I know) Commented Nov 28, 2011 at 14:34
-1

Because you are using double quotes in your JavaScript you need to use single quotes in your php.

var serverTime = "<?php echo date('Y-m-d H:i:s'); ?>";
2
  • 1
    See comment on the answer below. the quotes should not matter. They are in two different contexts. Commented Nov 28, 2011 at 14:33
  • 1
    Why does that make a difference? The PHP should be processed before the javascript. Commented Nov 28, 2011 at 14:34

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.